以下是我初始串口的程序:
function TForm1.Init_RS232(com_name
Char):boolean; //初始化RS232
var
lpdcb:TDCB;
begin
hCom:=CreateFile(com_name,generic_read or
generic_write,0,nil,open_existing,
file_flag_overlapped,0);//打开串行口
if hCom=invalid_handle_value then
begin
ShowMessage('Can not Open COM!');
CloseHandle(hCom);
Result:=false;
exit;
end;
successflag:=SetupComm(hCom,1024,1024); //设置COM输入,输出缓冲区皆为4096字节
if not successflag then
begin
ShowMessage('Can not setup COM!');
CloseHandle(hCom);
Result:=false;
exit;
end;
successflag:=GetCommState(hCom,lpdcb); //获取DCB当前默认设置
if not successflag then
begin
ShowMessage('Can not get DCB!');
CloseHandle(hCom);
Result:=false;
exit;
end;
lpdcb.baudrate:=9600;
lpdcb.ByteSize:=8;
lpdcb.Parity:=NoParity; //偶校验
lpdcb.StopBits:=OneStopBit;
successflag:=SetCommState(hCom,lpdcb);
if not successflag then
begin
ShowMessage('Can not setup DCB!');
CloseHandle(hCom);
Result:=false;
exit;
end;
successflag:=SetCommMask(hCom,ev_rxchar and ev_txempty); //指定串行口事件为接收到字符;
if not successflag then
begin
ShowMessage('Can not setup Event!');
CloseHandle(hCom);
Result:=false;
exit;
end;
PurgeComm(hCom,PURGE_RXCLEAR or PURGE_TXCLEAR);
Result:=true;
end;
下面是写串口的程序:
successflag:=WriteFile(hCom,outbuffer,Length(outbuffer),nBytesWrite,lpol);
在W98下successflag为True,而在W2K下却为False;
是不是在W2K下初始串口和W98有区别?