我的程序:
procedure TFormReal.FormShow(Sender: TObject);
var
Bool : boolean;
TimeOut : _COMMTIMEOUTS;
//wNum : Cardinal; //LPDWORD;
//ErrorCode: longint;
//Pol : Overlapped;
begin
SuccessBool := true;
TabBpGst.Active := true;
SecondCount := 0;
hComm:=CreateFile('COM2',GENERIC_READ or Generic_Write,0,nil,
Open_Existing,File_Attribute_Normal or File_Flag_Overlapped,0);
if (hComm = INVALID_HANDLE_VALUE) then
begin
ShowMessage('串口无法打开!');
application.Terminate;
end;
//设置串口缓冲区
Bool := SetupComm(hComm,4096,4096);
if Bool = false then
begin
ShowMessage('设置串口失败');
application.Terminate;
end;
GetCommState(hComm,lpDCB);
lpDCB.BaudRate := CBR_9600;
lpDCB.ByteSize := 8;
lpDCB.Parity := NoParity;
lpDCB.StopBits := 0;
Bool := SetCommState(hComm,lpDCB);
if Bool = false then
begin
ShowMessage('设置串口失败');
application.Terminate;
end;
Bool := GetCommTimeouts(hComm,TimeOut);
if Bool = false then
begin
ShowMessage('设置串口失败');
application.Terminate;
end;
TimeOut.ReadTotalTimeoutConstant := 12000;
Bool := SetCommTimeouts(hComm,TimeOut);
if Bool = false then
begin
ShowMessage('设置串口失败');
application.Terminate;
end;
TimerInterval.Enabled := true;
end;
写串口是在一个一秒钟触发一次的定时中做的,是不是定时触发后需要先关闭,处理完再开启定时?请各位大侠发表高见