回答如救人
我这有一个关于串口通信的程序。通过修改后源代码如下:
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) ,
过了今天我的命运可就不一定了。
再重复一边:我希望有对串口读出、写入的具体的源程序
(控件可不能通过我们公司的关).我一定给第一个解决问题的大哥加分。
对各位大虾深深的鞠躬了。
(我是个菜菜鸟!!!)