6 个解决方案
#1
是不是资源占用冲突
#2
//初始化com
Mscomm1.Settings :='9600,n,8,1';
Mscomm1.CommPort :=1;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.InBufferCount :=0;
Mscomm1.InputLen :=0;
Mscomm1.RThreshold :=1;
Mscomm1.SThreshold :=2;
Mscomm1.OutBufferSize:=1024;
if Mscomm1.PortOpen then
begin
Mscomm1.PortOpen :=false;
end;
Mscomm1.PortOpen :=true;
Mscomm1.Settings :='9600,n,8,1';
Mscomm1.CommPort :=1;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.InBufferCount :=0;
Mscomm1.InputLen :=0;
Mscomm1.RThreshold :=1;
Mscomm1.SThreshold :=2;
Mscomm1.OutBufferSize:=1024;
if Mscomm1.PortOpen then
begin
Mscomm1.PortOpen :=false;
end;
Mscomm1.PortOpen :=true;
#3
接收:
if Mscomm1.CommEvent = 2 then
begin
recstr := Mscomm1.Input ;
s := recstr;
end;
if Mscomm1.CommEvent = 2 then
begin
recstr := Mscomm1.Input ;
s := recstr;
end;
#4
发送数据:
recstr:Olevariant;
a:variant;
a:=VarArrayCreate([1,7],varByte);//发送位数
a[1]:=248;
a[2]:=d_1;
a[3]:=d_2;
a[4]:=d_3;
a[5]:=d_4;
a[6]:=d_5;
a[7]:=d_6;
recstr:=a;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.Output :=recstr;
recstr:Olevariant;
a:variant;
a:=VarArrayCreate([1,7],varByte);//发送位数
a[1]:=248;
a[2]:=d_1;
a[3]:=d_2;
a[4]:=d_3;
a[5]:=d_4;
a[6]:=d_5;
a[7]:=d_6;
recstr:=a;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.Output :=recstr;
#5
gzzzzzzz
#6
unit comdemou;
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 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);
//设置输入,输出缓冲区皆为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.Msgcomm
Process(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;
procedure TForm1.FormCreate(Sender: TObject);
begin
comminitialize;
post_event:=CreateEvent
(nil,true,true,nil); //创建同步事件;
Tcomm.Create(False);
//创建串行口监视线程;
end;
end.
#1
是不是资源占用冲突
#2
//初始化com
Mscomm1.Settings :='9600,n,8,1';
Mscomm1.CommPort :=1;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.InBufferCount :=0;
Mscomm1.InputLen :=0;
Mscomm1.RThreshold :=1;
Mscomm1.SThreshold :=2;
Mscomm1.OutBufferSize:=1024;
if Mscomm1.PortOpen then
begin
Mscomm1.PortOpen :=false;
end;
Mscomm1.PortOpen :=true;
Mscomm1.Settings :='9600,n,8,1';
Mscomm1.CommPort :=1;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.InBufferCount :=0;
Mscomm1.InputLen :=0;
Mscomm1.RThreshold :=1;
Mscomm1.SThreshold :=2;
Mscomm1.OutBufferSize:=1024;
if Mscomm1.PortOpen then
begin
Mscomm1.PortOpen :=false;
end;
Mscomm1.PortOpen :=true;
#3
接收:
if Mscomm1.CommEvent = 2 then
begin
recstr := Mscomm1.Input ;
s := recstr;
end;
if Mscomm1.CommEvent = 2 then
begin
recstr := Mscomm1.Input ;
s := recstr;
end;
#4
发送数据:
recstr:Olevariant;
a:variant;
a:=VarArrayCreate([1,7],varByte);//发送位数
a[1]:=248;
a[2]:=d_1;
a[3]:=d_2;
a[4]:=d_3;
a[5]:=d_4;
a[6]:=d_5;
a[7]:=d_6;
recstr:=a;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.Output :=recstr;
recstr:Olevariant;
a:variant;
a:=VarArrayCreate([1,7],varByte);//发送位数
a[1]:=248;
a[2]:=d_1;
a[3]:=d_2;
a[4]:=d_3;
a[5]:=d_4;
a[6]:=d_5;
a[7]:=d_6;
recstr:=a;
Mscomm1.DTREnable :=true;
Mscomm1.RTSEnable :=true;
Mscomm1.Output :=recstr;
#5
gzzzzzzz
#6
unit comdemou;
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 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);
//设置输入,输出缓冲区皆为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.Msgcomm
Process(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;
procedure TForm1.FormCreate(Sender: TObject);
begin
comminitialize;
post_event:=CreateEvent
(nil,true,true,nil); //创建同步事件;
Tcomm.Create(False);
//创建串行口监视线程;
end;
end.