回答如救人

ckbmail 2000-08-25 05:23:00
我这有一个关于串口通信的程序。通过修改后源代码如下:
unit ComExample;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

const Wm_commNotify=Wm_user+12;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);

private
procedure comminitialize;
procedure MsgcommProcess(Var Message:Tmessage);Message Wm_commnotify;
{ Private declarations }
public
{ Public declarations }
end;
Tcomm=Class(TThread)//线程申明
protected
procedure Execute;override;
end;
var
Form1: TForm1;
hcom,Post_Event:Thandle;
lpol:Poverlapped;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
comminitialize;
post_event:=Createevent(nil,true,true,nil);//创建同步事件
Tcomm.Create(False);//创建串行口监视线程
end;

procedure Tcomm.Execute;//线程执行过程
var
dwEvtMask:Dword;
Wait:Boolean;
begin
fillchar(lpol,sizeof(toverlapped),0);
while true do
begin
dwEvtMask:=0;
wait:=WaitCommEvent(hcom,dwevtmask,lpol);//等待串口事件
if wait then
begin
waitforsingleobject(post_event,infinite);//等待同步事件置
resetevent(post_event);//同步事件复位
PostMessage(Form1.Handle,WM_COMMNOTIFY,0,0);//发送消息
end;
end;
end;

procedure Tform1.comminitialize;
var
lpdcb:Tdcb;
begin
hcom:=Createfile('com2',generic_read or generic_write,0,nil,open_existing,file_attribute_normal or file_flag_overlapped,0);//打开串行口
if hcom=invalid_handle_value then
else
setupcomm(hcom,4096,4096);
getcommstate(hcom,lpdcb);
lpdcb.BaudRate:=2400;
lpdcb.StopBits:=1;
lpdcb.ByteSize:=8;
lpdcb.Parity:=EvenParity;
setcommstate(hcom,lpdcb);
setcommMask(hcom,ev_rxchar);//指定串行口事件为接受到字符
end;

procedure TForm1.MsgcommProcess(Var Message:tmessage);
var
Clear:Boolean;
Coms:TComstat;
cbNum,ReadNumber,lpErrors:integer;
Read_buffer:array[1..100] of char;
begin
#########Clear:=ClearCommError(hcom,lpErrors,@Coms);
if clear then
begin
cbNum:=Coms.cbInQue;
############ReadFile(hcom,Read_buffer,cbNum,ReadNumber,lpol);
setEvent(Post_Event);
end;
end;


end.
其中关于clearcommerror和readfile的两个函数总是通不过(用多个#号在前面标出)。
请各位大虾帮忙了,我感激涕零!!!!!!.
最好能给我一段完整的关于从串口中读取、传入数据的源代码,多谢了!!!!

急切希望在今天得到回复(mailto:ckbmail@elong.com) ,
过了今天我的命运可就不一定了。
再重复一边:我希望有对串口读出、写入的具体的源程序
(控件可不能通过我们公司的关).我一定给第一个解决问题的大哥加分。
对各位大虾深深的鞠躬了。
(我是个菜菜鸟!!!)
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sayhigh 2000-08-25
  • 打赏
  • 举报
回复
if ClearCommError( hCommFile, dwErrors, nil ) then ......

if ReadFile( hCommFile,szInputBuffer^,dwSizeofBuffer,
NumberOfBytesRead, lpOverlappedRead ) then
.....

参数不用解释了吧

Sayhigh 2000-08-25
  • 打赏
  • 举报
回复
用VCL呀,我有一个很不错,不只你要不要

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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