Y
ytdxl
Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi3.0在win98下写了一段串口通讯的程序,可以正常使用,可是同样的,
在win2000下,却不能正常运行,请各位高手指正:
打开串口:
function TOperateCOM.InitCOM :BooLean;
var
HandleID word;
SFileName:String; iUseWhatCOM:integer;
tTIniFile:TIniFile;
begin
sFileName:='c:/'+S21Dir+'/ZdSystem.ini';
tTIniFile:=TIniFile.Create(sFileName);
iUseWhatCOM:=tTIniFile.Readinteger('COM','COM',1 );
HandleID:=-1;
CASE iUseWhatCOM Of
1: HandleID :=createfile('COM1',generic_read or
generic_write,0,nil,open_existing,
file_attribute_normal or
file_flag_overlapped,0);
2: HandleID :=CreateFile('COM2', GENERIC_READ or GENERIC_WRITE,
0, Nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,
0);
END;
tTIniFile.Free;
If HandleID =-1
Then begin
ShowCOMError;
result :=FALSE;
End
Else begin
SetCommMask( HandleID, EV_RXCHAR) ;
SetupComm(HandleID, 4096, 4096 ) ;
PurgeComm(HandleID, PURGE_TXABORT or PURGE_RXABORT or
PURGE_TXCLEAR or PURGE_RXCLEAR );
GetCommTimeouts(HandleId,COMMTIMEOUTS);
COMMTIMEOUTS.ReadIntervalTimeout:=10;
COMMTIMEOUTS.ReadTotalTimeoutMultiplier:=12;
COMMTIMEOUTS.ReadTotalTimeoutConstant:=10;
SetCommTimeouts(HandleId,COMMTIMEOUTS);
GetCommState(HandleID, dcb );
dcb.DCBlength :=sizeof( DCB );
Dcb.BaudRate :=CBR_4800;
Dcb.ByteSize :=8;
Dcb.Parity :=2;
Dcb.Stopbits :=0;
SetCommState(HandleID, dcb);
OperateCOM.COMHandle :=HandleID;
result :=TRUE;
ComBusy :=False;
End;
end;
写出数据:
function TOperateCOM.WriteBytesToCom( ByteNum word; buf :array of byte) :BooLean;
var
dwBytesWritten, i word ;
overlp OverLapped; //OverLapped
Label EXCEPTIONEND;
begin
While ComBusy=True do;
ComBusy :=True;
Result:=TRUE;
dwBytesWritten:=0;
For i :=0 to ByteNum-1 do
Begin
Try
//result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);
result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, NIL);
Except
ShowCOMError;
End;
If Result=False
Then Begin
ShowCOMError;
Goto EXCEPTIONEND;
End;
End;
EXCEPTIONEND:
ComBusy :=False;
end;
总是
Result=False
我用
result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);
lpol是我定义的全局变量 POverLapped;
也是Result=false;
用com1和com2两种打开方式,结果是一样的,但二者的句柄不一样。
有什么问题,请告诉指正。ytdxl@21cn.com 谢谢!
在win2000下,却不能正常运行,请各位高手指正:
打开串口:
function TOperateCOM.InitCOM :BooLean;
var
HandleID word;
SFileName:String; iUseWhatCOM:integer;
tTIniFile:TIniFile;
begin
sFileName:='c:/'+S21Dir+'/ZdSystem.ini';
tTIniFile:=TIniFile.Create(sFileName);
iUseWhatCOM:=tTIniFile.Readinteger('COM','COM',1 );
HandleID:=-1;
CASE iUseWhatCOM Of
1: HandleID :=createfile('COM1',generic_read or
generic_write,0,nil,open_existing,
file_attribute_normal or
file_flag_overlapped,0);
2: HandleID :=CreateFile('COM2', GENERIC_READ or GENERIC_WRITE,
0, Nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,
0);
END;
tTIniFile.Free;
If HandleID =-1
Then begin
ShowCOMError;
result :=FALSE;
End
Else begin
SetCommMask( HandleID, EV_RXCHAR) ;
SetupComm(HandleID, 4096, 4096 ) ;
PurgeComm(HandleID, PURGE_TXABORT or PURGE_RXABORT or
PURGE_TXCLEAR or PURGE_RXCLEAR );
GetCommTimeouts(HandleId,COMMTIMEOUTS);
COMMTIMEOUTS.ReadIntervalTimeout:=10;
COMMTIMEOUTS.ReadTotalTimeoutMultiplier:=12;
COMMTIMEOUTS.ReadTotalTimeoutConstant:=10;
SetCommTimeouts(HandleId,COMMTIMEOUTS);
GetCommState(HandleID, dcb );
dcb.DCBlength :=sizeof( DCB );
Dcb.BaudRate :=CBR_4800;
Dcb.ByteSize :=8;
Dcb.Parity :=2;
Dcb.Stopbits :=0;
SetCommState(HandleID, dcb);
OperateCOM.COMHandle :=HandleID;
result :=TRUE;
ComBusy :=False;
End;
end;
写出数据:
function TOperateCOM.WriteBytesToCom( ByteNum word; buf :array of byte) :BooLean;
var
dwBytesWritten, i word ;
overlp OverLapped; //OverLapped
Label EXCEPTIONEND;
begin
While ComBusy=True do;
ComBusy :=True;
Result:=TRUE;
dwBytesWritten:=0;
For i :=0 to ByteNum-1 do
Begin
Try
//result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);
result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, NIL);
Except
ShowCOMError;
End;
If Result=False
Then Begin
ShowCOMError;
Goto EXCEPTIONEND;
End;
End;
EXCEPTIONEND:
ComBusy :=False;
end;
总是
Result=False
我用
result :=WriteFile(COMHandle, buf, 1, dwBytesWritten, lpol);
lpol是我定义的全局变量 POverLapped;
也是Result=false;
用com1和com2两种打开方式,结果是一样的,但二者的句柄不一样。
有什么问题,请告诉指正。ytdxl@21cn.com 谢谢!