请教一个MsComm控件的问题(串口通讯)!

potter_cs 2002-12-10 08:44:42
当我用[Form.]Mscomm.output:=Value(Value:array of byte) 时 raise串口在打开时无法使用,但我已经初始化了串口的啊,还有程序在com1上可以用改在com2口上也出同样的问题,请大侠指教,谢谢谢谢!
...全文
81 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SilveryFox 2002-12-10
  • 打赏
  • 举报
回复

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.
SilveryFox 2002-12-10
  • 打赏
  • 举报
回复
gzzzzzzz
thisisxutao 2002-12-10
  • 打赏
  • 举报
回复
发送数据:
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;
thisisxutao 2002-12-10
  • 打赏
  • 举报
回复
接收:
if Mscomm1.CommEvent = 2 then
begin
recstr := Mscomm1.Input ;
s := recstr;
end;
thisisxutao 2002-12-10
  • 打赏
  • 举报
回复
//初始化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;
SilveryFox 2002-12-10
  • 打赏
  • 举报
回复
是不是资源占用冲突

1,184

社区成员

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

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