读不到串口数据?

wjqname 2004-11-10 09:46:13
procedure TForm1.Timer1Timer(Sender: TObject);
var
bf: array[0..10] of char;
n:cardinal;
begin
n:=0;
if readfile(hcom,bf,10,n,nil) then
listbox1.Items.Add(bf);
end;

procedure TForm1.FormCreate(Sender: TObject);
var dcb:TDCB;
begin
hcom:=createfile('com1',GENERIC_READ and GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if hcom=invalid_handle_value then
begin
showmessage('打开串口失败!') ;
exit;
end;
if not GetCommState(hCom,dcb) then
begin
showmessage('读串口状态失败!') ;
exit;
end;
dcb.BaudRate:=1200;
dcb.ByteSize:=8;
dcb.Parity:=NOPARITY;
dcb.StopBits:=ONESTOPBIT;
if SetCommState( hCom, dcb ) then

if not SetupComm( hcom, 1024, 1024 ) then
begin
CloseHandle( hcom );
showmessage('设置缓冲失败!')
end;
end;
...全文
169 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjqname 2004-11-11
  • 打赏
  • 举报
回复
给分失败,对不起了
劣势 2004-11-10
  • 打赏
  • 举报
回复
学习
zwb666 2004-11-10
  • 打赏
  • 举报
回复
学习
beyondtkl 2004-11-10
  • 打赏
  • 举报
回复
没写过串口 学习。
zhangheaaa 2004-11-10
  • 打赏
  • 举报
回复
可能是你的COMM1口,已经被占用了,无法使用,

或者COMM1有问题,无法使用。

可以用下面的方法获取可以使用的串口:

(********************************************************************
**
** GetAvailableComPorts -
**
** This is a procedure whose purpose is to determine
** which COM ports are available on the user's computer. It then
** fills up the PortsComboBox with those available COM port numbers.
**
********************************************************************)

procedure TJeffsForm.GetAvailableComPorts();
var
i : Integer;
S : String;
ComName: array[0..9] of Char;
TheComHandle: Integer;

begin

(*
** Okay, Clear everything in the PortsComboBox because
** we want to start off fresh.
*)

PortsComboBox.Clear();

(*
** Windows 95/98 can only handle up to 50 COM ports.
** All we are doing here is checking to see if Windows
** "can" open up the COM port. If so, then we know it's
** available. And so we add it to our ComboBox list
** and of course close the COM port each time we check it.
*)

for i := 1 to 50 do begin

StrFmt( ComName, '\\.\COM%d', [i] );

TheComHandle := CreateFile
(
ComName, // name
GENERIC_READ or GENERIC_WRITE, // access attributes
0, // no sharing
nil, // no security
OPEN_EXISTING, // creation action
FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_OVERLAPPED, // attributes
0 // no template
);

if ( TheComHandle <> INVALID_HANDLE_VALUE ) then begin

S := Format('COM%d', [i]);
PortsComboBox.Items.Add(S);

end;

CloseHandle( TheComHandle );

end; {end of for loop}

end;

wjqname 2004-11-10
  • 打赏
  • 举报
回复
原来是 hcom:=createfile('com1',GENERIC_READ * or * GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
我写成了and了,笔误,现在可以正常了
wjqname 2004-11-10
  • 打赏
  • 举报
回复
加了也不行我用mscomm只用设置以下几个属性:
settings:1200,n,8,1
rthreshold:10
其他采用确省值就可以接受到数据了
ksaiy 2004-11-10
  • 打赏
  • 举报
回复
file_flag_overlapped

你看一下这个参数。
wjqname 2004-11-10
  • 打赏
  • 举报
回复
不对,我用mscomm,spcomm很轻易的就能读到数据

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧