Z
zhangyuntian
Unregistered / Unconfirmed
GUEST, unregistred user!
小弟初学串口通讯,在调用API函数setcommstate的时候返回时总是 false
不知道什么原因,请各位大侠帮我看一下。感谢!!!
在formcreate中我先调用为OPENCOMM函数。函数如下:
procedure TForm1.opencomm;
var
temp:string;
begin
temp:='COM1';
hcomm:=createfile(pchar(temp),GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,0,0);
if hcomm=INVALID_HANDLE_VALUE then
begin
messagebox(0,'打开通信端口错误!!','',mb_ok);
exit;
end;
end;
然后我在一按钮的单击事件中调用setport。函数如下:
procedure TForm1.setport;
var
cc:tcommconfig;
flags:longbool;
flagg:longbool;
begin
SetupComm(hcomm,2048,2048);
flagg:=getcommstate(hcomm,cc.dcb);
if not flagg then
begin
messagebox(0,'不能取得端口信息!!','',mb_ok);
exit;
end;
cc.dcb.BaudRate:=cbr_1200;
cc.dcb.ByteSize:=8;
cc.dcb.StopBits:=one5stopbits;
cc.dcb.Parity:=oddParity;
cc.dcb.Flags:=1;
flags:=SetCommState(hcomm,cc.dcb);//////////此处返回假!!!为什么呀?????
if not flags then
begin
messagebox(0,'通信端口设置错误!!','',mb_ok);
exit;
end;
end;
不知道什么原因,请各位大侠帮我看一下。感谢!!!
在formcreate中我先调用为OPENCOMM函数。函数如下:
procedure TForm1.opencomm;
var
temp:string;
begin
temp:='COM1';
hcomm:=createfile(pchar(temp),GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,0,0);
if hcomm=INVALID_HANDLE_VALUE then
begin
messagebox(0,'打开通信端口错误!!','',mb_ok);
exit;
end;
end;
然后我在一按钮的单击事件中调用setport。函数如下:
procedure TForm1.setport;
var
cc:tcommconfig;
flags:longbool;
flagg:longbool;
begin
SetupComm(hcomm,2048,2048);
flagg:=getcommstate(hcomm,cc.dcb);
if not flagg then
begin
messagebox(0,'不能取得端口信息!!','',mb_ok);
exit;
end;
cc.dcb.BaudRate:=cbr_1200;
cc.dcb.ByteSize:=8;
cc.dcb.StopBits:=one5stopbits;
cc.dcb.Parity:=oddParity;
cc.dcb.Flags:=1;
flags:=SetCommState(hcomm,cc.dcb);//////////此处返回假!!!为什么呀?????
if not flags then
begin
messagebox(0,'通信端口设置错误!!','',mb_ok);
exit;
end;
end;