在delphi中用api 写串口的问题
我拟用API函数写一个串口通讯程序,其代码如下,可每次执行时都到了最后一句:
写串口失败,请问有人能解决吗?
hCom:=createfile('com1',generic_read or
generic_write,0,nil,open_existing,
file_attribute_normal or
file_flag_overlapped,0);//打开串行口
if hcom=invalid_handle_value then
begin
MessageBox(0,'Open 串口失败','Notice',MB_OK);
exit;
end
else
begin
setupcomm(hcom,4096,4096);
//设置输入,输出缓冲区皆为4096字节
getcommstate(hcom,lpdcb);
//获取串行口当前默认设置
lpdcb.baudrate:=1200;
lpdcb.StopBits:=1;
lpdcb.ByteSize:=8;
lpdcb.Parity:=NoParity; //偶校验
Setcommstate(hcom,lpdcb);
//MessageBox(0,'Open 串口成功','Notice',MB_OK);
b:=ord('a');
if WriteFile(hCom, b, 1, res, nil) then
messageBox(0,'Success','Notice',MB_OK)
else
messageBox(0,'failure','Notice',MB_OK);