Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Application for a card reader

How do I develop a program that can sense when a card is inserted into a card reader connected to a COM port? Does DELPHI have any components which can be configured to achieve this? Or are there any third party components available for this purpose?

Comments

  • injektiloinjektilo Member Posts: 113
    [b][red]This message was edited by injektilo at 2003-9-7 6:29:55[/red][/b][hr]
    this is what i do:

    i have a small service running to read incoming com-events. (example code is available at www.torry.net . i use a multiple threaded component to read and write the com-ports.

    this little service-program parses and writes legal com-data to a mapped file. i wrote a small component to get acces to this mapspace:

    [code]
    unit Mapper;

    interface

    uses Classes, Windows;

    type
    TMapper = class(TComponent)
    private
    FName: string;
    FSize: Integer;
    FHandle: Integer;
    FMemory: Pointer;
    FFree: Boolean;
    function GetPointer: Pointer;
    protected
    procedure Loaded; override;
    public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function GetSize: Integer;
    procedure ReadBuffer(var Buffer; const Size: Integer);
    procedure WriteBuffer(const Buffer; const Size: Integer);
    published
    property Caption: string read FName write FName;
    property Size: Integer read FSize write FSize;
    property DoFree: Boolean read FFree write FFree;
    end;

    procedure Register;

    implementation

    procedure Register;
    begin
    RegisterComponents('Tom', [TMapper]);
    end;

    constructor TMapper.Create(AOwner: TComponent);
    begin
    inherited;
    FName := 'YOUR_MAP_NAME_FOR_ALL_YOUR_APPS_THAT_USE_IT';
    FSize := 1024;
    FMemory := nil;
    FFree := False;
    end;

    destructor TMapper.Destroy;
    begin
    if (FMemory <> nil) and (FFree = True) then UnmapViewOfFile(GetPointer);
    if (FHandle <> 0) and (FFree = True) then CloseHandle(FHandle);
    inherited;
    end;

    procedure TMapper.Loaded;
    begin
    inherited;
    FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, FSize + 4, PChar(FName));
    if FHandle <> 0 then
    begin
    FMemory := MapViewOfFileEx(FHandle, FILE_MAP_WRITE, 0, 0, 0, nil);
    if FMemory <> nil then
    begin
    PInteger(FMemory)^ := FSize;
    FMemory := Ptr(Integer(FMemory) + 4);
    end;
    end;
    end;

    function TMapper.GetSize: Integer;
    begin
    Result := 0;
    if FMemory <> nil then
    Result := PInteger(GetPointer)^;
    end;

    procedure TMapper.ReadBuffer(var Buffer; const Size: Integer);
    begin
    if FMemory <> nil then
    Move(FMemory^, Buffer, Size);
    end;

    procedure TMapper.WriteBuffer(const Buffer; const Size: Integer);
    begin
    if FMemory <> nil then
    if FSize <= GetSize then
    Move(Buffer, FMemory^, Size);
    end;

    function TMapper.GetPointer;
    begin
    if FMemory <> nil then
    Result := Ptr(Integer(FMemory) - 4)
    else
    Result := nil;
    end;

    end.
    [/code]

    you can extend it to send a windows message each time you write data to the map, so other applications know when to read it.

    in the main app you catch the windows message and read the map (using the same component) to get the string that has been written. this is an example on how to create your own message handle:

    [code]
    var
    YOUR_COMMAND: Cardinal;

    in code:
    creation:
    YOUR_COMMAND := RegisterWindowMessage(PChar('YOUR_COMMAND'));
    writing:
    PostMessage(??BROADCAST, YOUR_COMMAND, ALMPAR, AWMPAR);
    [/code]

    use the TApplicationEvents OnMessage event to catch it

    hope it helps.

    ps: go check out www.torry.net there are lots of com-components. rewrite them for your needs.



Sign In or Register to comment.