为什么不能接收到数据?关于mscomm32的多线程的!(50分)

C

cqwty

Unregistered / Unconfirmed
GUEST, unregistred user!
我在线程单元里面写了代码,将mscomm1.input加入到memo中,可是不行,
非要在oncomm事件中写,线程单元的代码如下:
unit mycommthread;
interface
uses
Classes,MSCommLib_TLB,shellapi,sysutils,StdCtrls,comctrls;
type
mycomm = class(TThread)
private
{ Private declarations }
amscomm:tmscomm;
amemo:tmemo;
astring:string;
protected
procedure Execute;
override;
procedure disinfo;
public
constructor create(mscomm:tmscomm;memo:tmemo);
end;

implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure mycomm.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ mycomm }
constructor mycomm.create(mscomm:tmscomm;memo:tmemo);
begin
inherited create(false);
amscomm:=mscomm;
amemo:=memo;
freeonterminate:=true;
end;

procedure mycomm.Execute;
begin
if amscomm.CommEvent=2 then
astring:=amscomm.Input;
synchronize(disinfo);
end;

procedure mycomm.disinfo;
begin
amemo.lines.add(astring);
end;
end.
哪位大侠救救命吧!还有一个问题就是:
当创建线程的时候,一定要mscomm1.openport:=true;
否则提示说端口没有打开!
 
怎么就没有人能够回答这个问题吗?
 
我没有试过你写的代码,不过你试试动态创建tmscomm和tmemo(在create构造
函数中)
 
你说的是直接在create函数里面写这种语句:
amscomm:=tmscomm.create(self);
amemo:=tmemo.create(self);
是这样写吧!
 
这个问题没有人能够回答了,麻烦版主帮忙把帖子结了分数还给我,谢谢!
 
mycomm.Execute是执行一次的,你执行的时候,可能数据还没发过来,还没准备好呢,结果线程就结束了
你循环一下或者等待一下可能就可以了
 
你的思路有问题,应该在接收缓冲区有数据时启动线程来读数据。
在主程序的mscomm32控件的mscomm事件里执行你上面的那个线程。
 
同意halps的说法,
 
我看思路是没问题的,
但可能没注意到Mscomm.input有两种格式:Binary和Text
默认的格式是Binary,这样的格式是不能直接送Tmemo.text的
检查一下你的相关设置?
 
我的意见是:不用线程直接在MSCOMM的事件中写
if amscomm.CommEvent=2 then
amemo.lines.add(amscomm.input);
注意一下,是不是ASCII码,如果不是,写代码转化。
在PB中,我是这样写的,OK!
后来,在DELPHI6 中写也OK!
 
多人接受答案了。
 

Similar threads

Q
回复
4
查看
298
穿越沦陷的爱
穿
C
回复
3
查看
303
coolingxyz
C
C
回复
6
查看
309
coolingxyz
C
C
回复
9
查看
214
coolingxyz
C
顶部