过来看看呀,谢谢!!!!!!!有分赚的啦!!!!!!!!

danliyou 2002-04-24 04:12:34
我刚刚接触串口通讯,对MSCOMM控件刚刚了解。
我模仿别人做了一个计算机两个串口之间通讯的程序,
我知道不对,可是步知道怎么修改。希望大侠们给点帮助。
程序如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, MSCommLib_TLB;

type
TForm1 = class(TForm)
MSComm1: TMSComm;
Memo1: TMemo;
Memo2: TMemo;
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
MSComm2: TMSComm;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MSComm2Comm(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Caption:='串口通讯';
MSComm1.InBufferCount:=0;
MSComm1.InputLen:=0;
MSComm1.RThreshold:=1;
MSComm1.CommPort:=1;
MSComm1.DTREnable:=true;
MSComm1.RTSEnable:=true;
MSComm1.PortOpen:=true;
MSComm1.Settings:='9600,n,8,1';
Mscomm2.Settings:='9600,n,8,1';
MSComm2.InBufferCount:=0;
MSComm2.InputLen:=0;
MSComm2.RThreshold:=1;
MSComm2.CommPort:=2;
MSComm2.DTREnable:=true;
MSComm2.RTSEnable:=true;
MSComm2.PortOpen:=true;
Memo2.text:='';
Memo1.text:='';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
s,datastr:string;
begin
s:=memo1.text;
datastr:='';
if MSComm1.PortOpen=false then
showmessage('error!')
else
begin
showmessage('ok!');
for i:=0 to (length(s) div 2)-1 do
begin
datastr:=datastr+chr(strtoint('$'+ copy(s,2*i+1,2)));//什么意思?
end;
begin
MSComm1.output:=datastr;
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if MSComm1.PortOpen=true then
begin
MSComm1.PortOpen:=false;
Mscomm2.PortOpen:=false;
close();
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if MSComm1.PortOpen=true then
MSComm1.PortOpen:=false;
Mscomm1.PortOpen:=false;
end;


procedure TForm1.MSComm2Comm(Sender: TObject);
var
recstr:string;
strtemp:string;
i:integer;
begin
if Mscomm2.CommEvent=comEvReceive then
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=1 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;

end;

end.
现在问题如下:
我不知道这样是否能实现com1和com2的通讯?运行时发现接收的Memo框中根本就没有接收到信息。
单步执行时,下面的一段根本就没有执行。
procedure TForm1.MSComm2Comm(Sender: TObject);
var
recstr:string;
strtemp:string;
i:integer;
begin
if Mscomm2.CommEvent=comEvReceive then
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=1 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;
我是新手,不太了解串口通讯。
希望大家能帮助我一下。如何实现计算机两个串口之间的通讯?(用MSCOMM控件)
给点思路也可以。如有源程序,请发送至:speedaway@21cn.com
十分感谢各位的帮助。

...全文
34 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
danliyou 2002-07-01
  • 打赏
  • 举报
回复
接受答案..........
mrzho 2002-06-29
  • 打赏
  • 举报
回复
.
danliyou 2002-05-06
  • 打赏
  • 举报
回复
接受答案..........
MemechaLook 2002-04-26
  • 打赏
  • 举报
回复
Mscomm漏洞较多,可以在“DELPHI深度探索”中下载Spcomm控件,不错
danliyou 2002-04-26
  • 打赏
  • 举报
回复
我现在不是不想用API嘛,现在用控件。
Jobs_Weng 2002-04-26
  • 打赏
  • 举报
回复
花2个小时学习一下API,再花30分钟查一下API函数。
danliyou 2002-04-26
  • 打赏
  • 举报
回复
没有人回复了吗??
danliyou 2002-04-26
  • 打赏
  • 举报
回复
各位观众:
现在还有没有人回答呢?
谁解答了把分全部拿去。。。
danliyou 2002-04-26
  • 打赏
  • 举报
回复
我用Spcomm控件做了一个利用本机两个串口进行通讯的程序,你帮我看看对不对?
谢谢你啦。。
如果你愿意我贴代码上来。。
guohongning 2002-04-25
  • 打赏
  • 举报
回复
学习
danliyou 2002-04-25
  • 打赏
  • 举报
回复
to deathcat(从死亡边缘站起来的患了狂猫病的猫):
现在可以肯定的是:串口线的连接没有问题.
下面这段代码放在onComm事件中根本就不会执行。
if Mscomm2.CommEvent=comEvReceive then
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=0 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;
end;
我将其放在一个按钮事件中,点击按钮表示接收。
DeathCat 2002-04-25
  • 打赏
  • 举报
回复
if Mscomm2.CommEvent=comEvReceive then
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=0 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;
end;


把这段代码放到onComm事件中去看看。
Borland_Delphi 2002-04-25
  • 打赏
  • 举报
回复
up
DeathCat 2002-04-25
  • 打赏
  • 举报
回复
程序看起来好象没有什么问题,建议检查一下你的串口线。连接串口的2、3、5脚,其中有一个232口上的2、3脚要对跳。因为你不能把发送和发送连接到一起,必须要发送脚连接接收脚,5是地线。把线查好了,再看程序。
5730 2002-04-25
  • 打赏
  • 举报
回复
up
up
danliyou 2002-04-25
  • 打赏
  • 举报
回复
现在我将程序改为下面:
初始化过程:
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Caption:='串口通讯';
MSComm1.InBufferCount:=0;
MSComm1.InputLen:=0;
MSComm1.RThreshold:=1;
MSComm1.CommPort:=1;
MSComm1.DTREnable:=true;
MSComm1.RTSEnable:=true;
MSComm1.PortOpen:=true;
MSComm1.InputMode:=1;
MSComm1.Settings:='9600,n,8,1';
Mscomm2.Settings:='9600,n,8,1';
MSComm2.InputMode:=1;
MSComm2.InBufferCount:=0;
MSComm2.InputLen:=0;
MSComm2.RThreshold:=1;
MSComm2.CommPort:=2;
MSComm2.DTREnable:=true;
MSComm2.RTSEnable:=true;
MSComm2.PortOpen:=true;
Memo2.text:='';
Memo1.text:='';
end;
发送过程:
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
s,datastr:string;
begin
s:=memo1.text;
datastr:='';
if MSComm1.PortOpen=false then
showmessage('error!')
else
begin
showmessage('ok!');
for i:=0 to (length(s) div 2)-1 do
begin
datastr:=datastr+chr(strtoint('$'+ copy(s,2*i+1,2)));
end;
begin
MSComm1.output:=datastr;
end;
end;
end;
怎样证明发送成功?
接收过程:
procedure TForm1.Button3Click(Sender: TObject);
var
recstr:string;
strtemp:string;
i:integer;
begin
if Mscomm2.PortOpen=false then
showmessage('error')
else
showmessage('ok');
if Mscomm2.CommEvent=comEvReceive then
begin
sleep(350);
recstr:=Mscomm1.input;
strtemp:='';
for i:=0 to length(recstr) do
strtemp:=strtemp+inttohex(ord(recstr[i]),2);
memo2.text:=memo2.text+strtemp;
end;
end;
memo2框中看不到数据,单步执行时发现程序在接收过程中运行到:
if Mscomm2.CommEvent=comEvReceive then就步往下执行了,为什么呢?
希望大家能给我找到错误,再一次感谢大家的光临。

tanqth 2002-04-25
  • 打赏
  • 举报
回复
UP
ljy_17 2002-04-25
  • 打赏
  • 举报
回复
我帮你Up

danliyou 2002-04-25
  • 打赏
  • 举报
回复
to ActiveLF(程序人生):
我按照呢说的方法做了,可是还是收不到数据。
能不能给点另外的帮助呢?
谢谢!!!!!!
danliyou 2002-04-25
  • 打赏
  • 举报
回复
这个问题以前网上有讨论过呀.
我只是没有太看懂所以才问的.
怎么那些高手哪里去了.
加载更多回复(19)

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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