主要程序如下,请指教.
commint.pas和commObjs.pas 为async32控件中源代码.
library IC_DLL;
uses
SysUtils,
Classes,
ic in 'ic.pas' {Form1},
CommInt in '../async32/CommInt.pas',
CommObjs in '../async32/CommObjs.pas';
exports
RW_OPEN,RW_CLOSE,W_MIM,R_MIM,RW_REQUEST,RW_RESET;
begin
end.
unit ic;
interface
uses
Windows,Messages,SysUtils, Classes,Controls,Forms,CommInt;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure HandleException(Sender: TObject; E: Exception);
public
{ Public declarations }
end;
var
Form1: TForm1;
comm1:TComm;
function RW_OPEN(ID:Integer):Integer;export;stdcall;
function RW_CLOSE(ID:Integer):Integer;export;stdcall;
function W_MIM(ID:integer;address:Integer;number:Integer;m_string:string):integer;export;stdcall;
function R_MIM(ID:integer;address:Integer;number:Integer;var text:string):integer;export;stdcall;
function RW_REQUEST(ID:integer;var Chipn:string):integer;export;stdcall;
function RW_RESET(ID:Integer):integer;export;stdcall;
function BYTE_CONVERT(S_INPUT:string;number:integer):string;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnException := HandleException;
end;
procedure Tform1.HandleException(Sender: TObject; E: Exception);
begin
// if E is ECommError then
// with E as ECommError do
// ShowMessage('serial communication ' + Message);
end;
function RW_OPEN(ID:Integer):Integer;
begin
Comm1.Open;
RW_OPEN:=0;
end;
function RW_CLOSE(ID:Integer):Integer;
begin
comm1.close;
RW_CLOSE:=0;
end;
function RW_REQUEST(ID:integer;var Chipn:string):integer;
type
CharBuf = array[0..9999] of Char;
var
Buffer: ^CharBuf;
Bytes,p,p1: Integer;
text:string;
s:string;
count:Integer;
linedata:string;
begin
RW_REquest:=1;
s:=chr($06);
s:=s+chr($10);
s:=s+chr($00);
s:=s+chr($01);
s:=s+chr($04);
s:=s+chr($09);
s:=s+chr($1A);
count:=length(s);
Count := Comm1.Write(S[1], Count);{0x06, 0x10, 0x00, 0x01, 0x04, 0x09, 0x1A}
if Count <> -1 then
begin
Bytes:=0;
P1:=0;
while Bytes<14 do
begin
try
GetMem(Buffer, Comm1.ReadBufSize);
Fillchar(Buffer^, Comm1.ReadBufSize, 0);
Bytes :=Bytes+ Comm1.Read(Buffer^, 15);
if Bytes<>P1 then
begin
for P := P1 to Bytes - 1 do
begin
LineData := LineData + CharBuf(Buffer^)[P-P1];
end;
end;
P1:=Bytes;
finally
end;
end;
text:='';
text:=byte_convert(linedata,15);
if copy(text,3,4)='1005' then
begin
chipn:=text;
RW_REQUEST:=0;
end
else
begin
chipn:='';
RW_REQUEST:=1;
end
end;
end;
function W_MIM(ID:integer;address:Integer;number:Integer;m_string:string):integer;
type
CharBuf = array[0..9999] of Char;
var
Buffer: ^CharBuf;
Bytes,p,P1,i,sum: Integer;
text:string;
s,s1:string;
CurrChr:char;
count,s2:integer;
linedata:string;
begin
W_MIM:=1;
s:=chr(($05)+number);
s:=s+chr($40);
s:=s+chr($00);
s:=s+chr(address);
s:=s+chr(number);
s2:=($40)xor(address)xor(number)xor(($05)+number);
i:=1;
sum:=0;
while i<(2*number) do
begin
s1:=copy(M_string,i,2);
CurrChr:=s1[1];
case CurrChr of
'0'..'9':sum:=TRUNC(StrToInt(CurrChr)*16);
{'a'..'z':sum:=TRUNC((ord(CurrChr)-87)*16); }
'A'..'F':sum:=TRUNC((ord(CurrChr)-55)*16);
end;
CurrChr:=s1[2];
case CurrChr of
'0'..'9':sum:=sum+TRUNC(StrToInt(CurrChr));
{'a'..'z':sum:=sum+TRUNC((ord(CurrChr)-87)); }
'A'..'F':sum:=sum+TRUNC((ord(CurrChr)-55));
end;
s:=s+chr(sum);
s2:=(sum)xor(s2);
i:=i+2;
end;
s:=s+chr(s2);
count:=length(s);
Count := Comm1.Write(S[1], Count);{0x06, 0x10, 0x00, 0x01, 0x04, 0x09, 0x1A}
if Count <> -1 then
begin
Bytes:=0;
P1:=0;
while Bytes<10 do
begin
try
GetMem(Buffer, Comm1.ReadBufSize);
Fillchar(Buffer^, Comm1.ReadBufSize, 0);
Bytes := Bytes+Comm1.Read(Buffer^, 10);
if Bytes<>P1 then
begin
for P := P1 to Bytes - 1 do
begin
LineData := LineData + CharBuf(Buffer^)[P-P1];
end;
end;
P1:=Bytes;
finally
end;
end;
text:='';
text:=byte_convert(linedata,10);
if copy(text,3,4)='3000' then
begin
W_MIM:=0;
end
else
begin
W_MIM:=1;
end
end;
end;
function R_MIM(ID:integer;address:Integer;number:Integer;var text:string):integer;
type
CharBuf = array[0..9999] of Char;
var
Buffer: ^CharBuf;
Bytes,p,P1: Integer;
s:string;
count:integer;
linedata:string;
begin
R_MIM:=1;
s:=chr($05);
s:=s+chr($20);
s:=s+chr($00);
s:=s+chr(address);
s:=s+chr(number);
s:=s+chr(($25)xor(address)xor(number));
count:=length(s);
Count := Comm1.Write(S[1], Count);{0x06, 0x10, 0x00, 0x01, 0x04, 0x09, 0x1A}
if Count <> -1 then
begin
count:=count+number;
Bytes:=0;
p1:=0;
while Bytes<count do
begin
try
GetMem(Buffer, Comm1.ReadBufSize);
Fillchar(Buffer^, Comm1.ReadBufSize, 0);
Bytes :=Bytes+Comm1.Read(Buffer^, Count);
if Bytes<>P1 then
begin
for P := P1 to Bytes - 1 do
begin
LineData := LineData + CharBuf(Buffer^)[P-P1];
end;
end;
P1:=Bytes;
finally
end;
end;
text:=byte_convert(linedata,6+number);
if copy(text,3,4)='2000' then
begin
R_MIM:=0;
end
else
begin
text:='';
R_MIM:=1;
end
end;
end;
function RW_RESET(ID:Integer):integer;
type
CharBuf = array[0..9999] of Char;
var
Buffer: ^CharBuf;
Bytes,p,P1: Integer;
text:string;
s:string;
count:integer;
linedata:string;
begin
RW_RESET:=1;
s:=chr($03);
s:=s+chr($14);
s:=s+chr($01);
s:=s+chr($16);
count:=length(s);
Count:= Comm1.Write(S[1], Count);
if Count <> -1 then
begin
Bytes:=0;
P1:=0;
while Bytes<count do
begin
try
GetMem(Buffer, Comm1.ReadBufSize);
Fillchar(Buffer^, Comm1.ReadBufSize, 0);
Bytes :=Bytes+ Comm1.Read(Buffer^, Count);
if Bytes<>P1 then
begin
for P := P1 to Bytes - 1 do
begin
LineData := LineData + CharBuf(Buffer^)[P-P1];
end;
end;
P1:=Bytes;
finally
end;
end;
text:='';
text:=Byte_convert(linedata,4);
if copy(text,3,4)='1401' then
begin
RW_RESET:=0;
end
else
begin
RW_RESET:=1;
end
end;
end;
function BYTE_CONVERT(S_INPUT:string;number:integer):string;
var
P,i,j:Integer;
datachar:char;
text:string;
begin
for p:=1 to number do
begin
datachar:=S_INput[p];
i:=ord(datachar);
j:=i;
i:=(i and $F0);
i:=i div $10;
if i<=9 then
i:=i+$30
else
i:=i+$37;
text:=text+chr(i);
j:=(j and $0F);
if (j<=9) then
j:=j+$30
else
j:=j+$37;
text:=text+chr(j);
end;
BYTE_CONVERT:=text;
end;
end.