关于串口在2000下不能用的问题.(0分)

  • 主题发起人 主题发起人 caowei
  • 开始时间 开始时间
C

caowei

Unregistered / Unconfirmed
GUEST, unregistred user!

请将我的MyComm.pas中的InitComm函数改写为:
(MyComm请在http://avsuper.126.com下载)
(我已经测试过,2000下确实能用,如果有问题,欢迎大家与我联系)

function TComm.InitComm(ThisCommName: string; BaudRate,DataByte,StopByte,ParityByte: integer): Boolean;
var
dcb: Tdcb;
begin
//Are we already doing comm?
if hCommFile<>0 then
begin
InitComm := False;
exit;
end;

hCommFile := CreateFile( PChar(ThisCommName),
GENERIC_READ or GENERIC_WRITE,
0, {not shared}
nil, {no security ??}
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

if hCommFile = INVALID_HANDLE_VALUE then
begin
hCommFile := 0;
InitComm := False;
exit;
end;

//Is this a valid comm handle?
if GetFileType(hCommFile)<>FILE_TYPE_CHAR then
begin
CloseHandle(hCommFile);
hCommFile := 0;
InitComm := False;
exit;
end;

if not SetupComm(hCommFile, INQUESIZE, OUTQUESIZE) then
begin
CloseHandle(hCommFile);
hCommFile := 0;
InitComm := False;
exit;
end;

//purge any information in the buffer
PurgeComm(hCommFile, PURGE_TXABORT or PURGE_RXABORT or
PURGE_TXCLEAR or PURGE_RXCLEAR);

//Querying then setting the comm port configurations.
GetCommState(hCommFile, dcb);

dcb.BaudRate := BaudRate;
dcb.ByteSize := DataByte; //number of bits/byte, 4-8
dcb.Parity := ParityByte; //0-4=no,odd,even,mark,space
dcb.StopBits := StopByte; //0,1,2 = 1,1.5,2

dcb.Flags := 1; // Enable fBinary
if not SetCommState(hCommFile, dcb) then
begin
CloseHandle(hCommFile);
hCommFile := 0;
InitComm := False;
exit;
end;

InitComm := True;
bOpened := True;
CommName := ThisCommName;
end;
 
接受答案了.
 

Similar threads

后退
顶部