主要内容如下, dpr很简单, exports就成.
鬼知道是什么类型的磁卡机. 别人只给了我它的格式.
#27+'0':初始化
#27+']':待读
#27+'t':写卡
unit comdevice;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls,async32, forms;
type
TComDevice = class(TWinControl)
public
fparenthandle: Integer;
fCom : TComm32;
FFinished: boolean;
buff: tstringlist;
constructor create(AOwner: TComponent); override;
destructor Destroy; override;
procedure OnGetData(Sender: TObject; Count: Integer);
end;
function InitPort(APort: Word): boolean; stdcall; export;
function ClosePort: boolean; stdcall; export;
function WriteString(Str: Pchar): boolean; stdcall; export;
var
Com_dev: TComdevice;
implementation
function InitPort(APort: word): boolean;
var
initstr : string ;
begin
result := true;
try
if not assigned(Com_Dev) then
Com_Dev := TComDevice.create(Application);
With Com_Dev do
begin
fcom.Close;
fcom.DeviceName := 'COM'+inttostr(Aport);
fcom.BaudRate := cbr9600;
ffinished := false;
fcom.Open;
initstr := #27'0';
fcom.Write(initstr[1], 2);
initstr := #27']';
fcom.write(initstr[1],2);
end;
except
raise Exception.Create('COM port error!');
result := false;
end;
end;
function ClosePort: boolean;
var
s: string;
begin
result := true;
if assigned(com_dev) then
begin
s := #27'0';
com_dev.fcom.write(s[1],2);
com_dev.free;
com_dev := nil;
end;
end;
function WriteString(Str: Pchar): boolean;
var
s: string;
begin
result := false;
if not assigned(com_dev) then
begin
exit;
end;
s := #27'0';
com_dev.fcom.write(s[1], 2);
s := #27']';
com_dev.fcom.write(s[1], 2);
s := #27't'+stringofchar(' ', strlen(str))+#29#27'/';
move(str^, s[3], strlen(str));
com_dev.fcom.write(s[1], length(s));
result := true;
end;
constructor Tcomdevice.create(AOwner: TComponent);
begin
inherited create(AOwner);
fcom := TComm32.Create(self);
fcom.OnRxCharSignal := OnGetData;
end;
destructor Tcomdevice.Destroy;
var
s: string;
begin
fcom.close;
fcom.free;
inherited;
end;
procedure Tcomdevice.OnGetData(Sender: TObject; Count: Integer);
var
i : Integer;
buff: string;
s: string;
begin
fparenthandle := getfocus;
buff := stringofchar(#0, count);
fcom.read(buff[1], count);
i := 1;
while i <= length(buff) do
begin
if buff=#$1b then
inc(i)
else if (i<length(buff)) and (buff = #$3f) and (buff[i+1]=#$1c) then
begin
inc(i);
s := #27'0';
fcom.write(s[1], 2);
s := #27']';
fcom.write(s[1], 2);
exit;
end
else
postmessage(fparenthandle, WM_CHAR, ord(buff), 1);
inc(i);
end;
end;
end.