3。异步通讯,sleep延时
var
Form1: TForm1;
piCOM:integer;
hComm:thandle;
chrin:array [0..7] of byte;
Connected : Boolean;
inbuff: array[0..1] of byte;
osread,oswrite
verlapped;
implementation
{$R *.dfm}
function OpenComm(com:integer):Boolean;
var
CommTimeOut : TCOMMTIMEOUTS;
cc:TCOMMCONFIG;
Temp:string;
begin
case com of
1:Temp:='COM1';
2:Temp:='COM2';
3:Temp:='COM3';
end;
hComm:=CreateFile(PChar(Temp), GENERIC_READ or GENERIC_WRITE,
0, nil, OPEN_EXISTING, file_flag_overlapped, 0); // 打开COM1
if (hComm = INVALID_HANDLE_VALUE) then begin // 如果COM 未打开
MessageBox (0, '打开通信端口错误!!','',MB_OK);
result:=False;
end
else
begin
CommTimeOut.ReadIntervalTimeout := MAXDWORD;
CommTimeOut.writeTotalTimeoutConstant := 5000;
setupcomm(hcomm,1024,1024);
SetCommTimeouts(hComm, CommTimeOut);
GetCommState(hComm,cc.dcb); // 得知目前COM 的状态
cc.dcb.BaudRate:=9600; // 设置波特率为9600
cc.dcb.ByteSize:=8; // 字节为 8 bit
cc.dcb.Parity:=evenPARITY; // Parity 为 None
cc.dcb.StopBits:=twoSTOPBITs; // 1 个Stop bit
fillchar(osread,sizeof(osread),0);
fillchar(oswrite,sizeof(oswrite),0);
osread.hEvent:=createevent(nil,true,false,nil);
oswrite.hEvent:=createevent(nil,true,false,nil);
if not SetCommState(hComm, cc.dcb) then
begin // 设置COM 的状态
MessageBox (0, '通信端口设置错误!!!','',MB_OK);
CloseHandle(hComm);
result:=False;
end
else
result:=True;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if opencomm(1)=true then
showmessage('端口创建成功。');
form1.Timer1.Enabled:=false;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
re:integer;
begin
chrin[0]:=1;
chrin[1]:=255; //34
chrin[2]:=255;
chrin[3]:=0;
chrin[4]:=1;
chrin[5]:=255;
chrin[6]:=255;
chrin[7]:=0;// 51 204
send(chrin);
sleep(150);
readbuff;
end;
function TForm1.Send_msg(Sndstr: array of byte):integer;
var
lrc:dword;
cs:TCOMSTAT;
i,j:integer;
Connected:boolean;
chout:array[0..1] of byte;
nBytesRead, dwEvent, dwError:LongWORD ;
FirstTickCount:longint;
begin
PurgeComm(hComm, PURGE_RXCLEAR and PURGE_TXCLEAR); // 清除COM 数据
//mainform.Delay(80) ;
for i:=0 to 7 do
begin
WriteFile(hComm,sndstr
,Length(sndstr),lrc,nil); // 送出数据
end;
PurgeComm(hComm, PURGE_RXCLEAR and PURGE_TXCLEAR);
sleep(200);
ClearCommError(hComm,dwError,@CS); //取得状态
if(cs.cbInQue>0) then
begin
ReadFile(hComm,chout,cs.cbInQue,nBytesRead,nil); // 接收COM 的数据
if chout[0]=chout[1] then
begin
result:=chout[1];
end;
form1.Memo1.Lines.Add(inttostr(chout[1]));
end
else
begin
result:=100;
end;
end;
procedure TForm1.timeDelay(MSecs: Integer);
var
FirstTickCount:longint;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages;
until ((GetTickCount-FirstTickCount) >= Longint(msecs));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
form1.Timer1.Enabled:=false;
Connected := FALSE;
CommThread.Terminate;
form1.Caption:='端口关闭!';
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
re:integer;
doorchrout:array[0..1]of byte;
begin
chrin[0]:=0;
chrin[1]:=255;
chrin[2]:=255;
chrin[3]:=85;
chrin[4]:=0;
chrin[5]:=255;
chrin[6]:=255;
chrin[7]:=85;//
send(chrin);
sleep(350);
readbuff;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
form1.Timer1.Enabled:=false;
end;
//**********************读取模块***************************************
function TForm1.readbuff: integer;
var
i:integer;
cs:comstat;
readstat:boolean;
dwerror,byteread,dwerrorflages,dwlength:dword;
chout:array[0..1] of byte;
begin
clearcommerror(hcomm,dwerrorflages,@cs);
dwlength:=cs.cbInQue;
if dwlength>0 then
begin
readstat:=readfile(hcomm,chout,dwlength,byteread,@osread);
if (not readstat) and (getlasterror=error_io_pending) then
begin
while(not getoverlappedresult(hcomm,osread,byteread,true)) do
begin
dwerror:=getlasterror();
if dwerror=error_io_incomplete then
continue
else
break;
end;
end;
for i:=0 to 1 do
begin
form1.Memo1.Lines.Add(inttostr(chout));
end;
end
end;
//****************************************************************
//*************************发送模块*******************************
function TForm1.send(senddata: array of byte): boolean;
var
fwritestat:boolean;
dwerrorflages,dwlength,dwbyteswritten:dword;
cs:comstat;
freadstat:boolean;
i:integer;
begin
purgecomm(hcomm,purge_txclear and purge_rxclear);
for i:=0 to 7 do
begin
fwritestat:=writefile(hcomm,senddata,length(senddata),dwbyteswritten,@oswrite);
if (not fwritestat) and ( getlasterror()=error_io_pending) then
begin
while (not getoverlappedresult(hcomm,oswrite,dwbyteswritten,false)) do
begin
dwerrorflages:=getlasterror();
if dwerrorflages=error_io_incomplete then
continue
else
showmessage('can not continue!');
end;
end;
end;
end;
//***************************************************************************
end.