K
kenxy
Unregistered / Unconfirmed
GUEST, unregistred user!
我下面的程序能发出信息,但不知怎么接收正确的信息!请高手能指点一下
怎么写接收处理的代码?
程序如下:
unit comdemou;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, CPDrv;
const
WM_CommNotify=WM_User+12;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure comminitialize;
procedure MsgCommProcess(Var Message:TMessage);Message WM_CommNotify;
{ Private declarations }
public
{ Public declarations }
end;
//Thread declarations
TComm=Class(TThread)
protected
procedure Execute;override;
end;
var
Form1: TForm1;
hCom,Post_Event:Thandle;
lpoloverlapped;
implementation
{$R *.DFM}
Procedure TComm.Execute; //线程执行过程
var
dwEvtMaskword;
Wait:LongBool;
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('com1', //指定串口
GENERIC_READ or GENERIC_WRITE, //设定读写模式
0, //共享模式,此项必须为0
nil, //安全属性
OPEN_EXISTING, //产生方式
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED, //文件类型
0); //此项必须为空;
if hCom<>invalid_handle_value then
begin
SetupComm(hcom,4096,4096);
//设置输入,输出缓冲区皆为4096字节
GetCommState(hCom,lpdcb);
//获取串行口当前默认设置
lpdcb.BaudRate:=9600;
lpdcb.StopBits:=1;
lpdcb.ByteSize:=8;
lpdcb.Parity:=EVENPARITY;
SetCommState(hCom,lpdcb);
SetCommMask(hCom,EV_RXCHAR);
//指定串行口事件为接收到字符;
end;
end;
Procedure TForm1.MsgcommProcess(Var Message:Tmessage);
var
Clear:Boolean;
Coms:Tcomstat;
cbNum,ReadNumber,lpErrors:Cardinal;
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;
procedure TForm1.FormCreate(Sender: TObject);
begin
comminitialize;
post_event:=CreateEvent(nil,true,true,nil);//创建同步事件;
Tcomm.Create(false);
//创建串行口监视线程;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
Clear:Boolean;
Coms:Tcomstat;
cbNum,ReadNumber,lpErrors:Cardinal;
//Read_Buffer:array[1..10] of byte;
const
Read_buffer:array[1..17] of byte=($68,$A0,$00,$00,$68,$DD,$08,$11,$22,$33,$44,$55,$66,$77,$88,$B9,$16);
Begin
PurgeComm(hCom,PURGE_TXABORT);
for i:=1 to 17 do
begin
WriteFile(hCom,Read_Buffer,1,ReadNumber,@lpol);
end;
end;
end.
怎么写接收处理的代码?
程序如下:
unit comdemou;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, CPDrv;
const
WM_CommNotify=WM_User+12;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure comminitialize;
procedure MsgCommProcess(Var Message:TMessage);Message WM_CommNotify;
{ Private declarations }
public
{ Public declarations }
end;
//Thread declarations
TComm=Class(TThread)
protected
procedure Execute;override;
end;
var
Form1: TForm1;
hCom,Post_Event:Thandle;
lpoloverlapped;
implementation
{$R *.DFM}
Procedure TComm.Execute; //线程执行过程
var
dwEvtMaskword;
Wait:LongBool;
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('com1', //指定串口
GENERIC_READ or GENERIC_WRITE, //设定读写模式
0, //共享模式,此项必须为0
nil, //安全属性
OPEN_EXISTING, //产生方式
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED, //文件类型
0); //此项必须为空;
if hCom<>invalid_handle_value then
begin
SetupComm(hcom,4096,4096);
//设置输入,输出缓冲区皆为4096字节
GetCommState(hCom,lpdcb);
//获取串行口当前默认设置
lpdcb.BaudRate:=9600;
lpdcb.StopBits:=1;
lpdcb.ByteSize:=8;
lpdcb.Parity:=EVENPARITY;
SetCommState(hCom,lpdcb);
SetCommMask(hCom,EV_RXCHAR);
//指定串行口事件为接收到字符;
end;
end;
Procedure TForm1.MsgcommProcess(Var Message:Tmessage);
var
Clear:Boolean;
Coms:Tcomstat;
cbNum,ReadNumber,lpErrors:Cardinal;
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;
procedure TForm1.FormCreate(Sender: TObject);
begin
comminitialize;
post_event:=CreateEvent(nil,true,true,nil);//创建同步事件;
Tcomm.Create(false);
//创建串行口监视线程;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
Clear:Boolean;
Coms:Tcomstat;
cbNum,ReadNumber,lpErrors:Cardinal;
//Read_Buffer:array[1..10] of byte;
const
Read_buffer:array[1..17] of byte=($68,$A0,$00,$00,$68,$DD,$08,$11,$22,$33,$44,$55,$66,$77,$88,$B9,$16);
Begin
PurgeComm(hCom,PURGE_TXABORT);
for i:=1 to 17 do
begin
WriteFile(hCom,Read_Buffer,1,ReadNumber,@lpol);
end;
end;
end.