为什么我下面的串口通信等程序接不到数据? ( 积分: 0 )

  • 主题发起人 主题发起人 lhmlhl
  • 开始时间 开始时间
L

lhmlhl

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Comm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;

type
TFCOMM = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Button1: TButton;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
procedure senddata;
{ Public declarations }
end;

var
FCOMM: TFCOMM;
viewstring:string;
i:integer;
rbuf:array[0..16] of byte;
sbuf:array[0..16] of byte;
implementation

{$R *.dfm}

procedure TFCOMM.FormShow(Sender: TObject);
begin
//打开串口
comm1.StartComm;
end;

procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//关闭串口
comm1.StopComm;
end;

procedure TFCOMM.senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=True;
for i:=1 to 6 do
begin
if not fcomm.Comm1.WriteCommData(@sbuf,1) then
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf,2)+''
end;
viewstring:='发送'+viewstring;
fcomm.memo1.lines.add(viewstring);
fcomm.memo1.lines.add('');
if not commflg then messagedlg('发送失败' ,mterror,[mbyes],0);
end;
//发送按钮的点击事件
procedure TFCOMM.Button1Click(Sender: TObject);
begin
sbuf[1]:=byte($f0);//帧头
sbuf[2]:=byte($01); //命令号
sbuf[3]:=byte($ff);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($01);
sbuf[6]:=byte($f0); //帧尾
senddata;//调用发送函数
end;

procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+inttohex(rbuf,2)+'';
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
//如果 memo1上显示发送 F0 01 FF FF 01 F0
//和接收到 F0 01 FF FF 01 F0,
//这表示串口已正确地发送出数据并正确地接收到数据,则串口通信成功
end.
 
unit Comm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;

type
TFCOMM = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Button1: TButton;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
procedure senddata;
{ Public declarations }
end;

var
FCOMM: TFCOMM;
viewstring:string;
i:integer;
rbuf:array[0..16] of byte;
sbuf:array[0..16] of byte;
implementation

{$R *.dfm}

procedure TFCOMM.FormShow(Sender: TObject);
begin
//打开串口
comm1.StartComm;
end;

procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//关闭串口
comm1.StopComm;
end;

procedure TFCOMM.senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=True;
for i:=1 to 6 do
begin
if not fcomm.Comm1.WriteCommData(@sbuf,1) then
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf,2)+''
end;
viewstring:='发送'+viewstring;
fcomm.memo1.lines.add(viewstring);
fcomm.memo1.lines.add('');
if not commflg then messagedlg('发送失败' ,mterror,[mbyes],0);
end;
//发送按钮的点击事件
procedure TFCOMM.Button1Click(Sender: TObject);
begin
sbuf[1]:=byte($f0);//帧头
sbuf[2]:=byte($01); //命令号
sbuf[3]:=byte($ff);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($01);
sbuf[6]:=byte($f0); //帧尾
senddata;//调用发送函数
end;

procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+inttohex(rbuf,2)+'';
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
//如果 memo1上显示发送 F0 01 FF FF 01 F0
//和接收到 F0 01 FF FF 01 F0,
//这表示串口已正确地发送出数据并正确地接收到数据,则串口通信成功
end.
 
comm1的一些属性设置得对不对,比如串口,波特率,接受的字符格式
并且看代码,好像你用的是同一个串口吧,那么应该将发送针和接收针联起来
 
怎么连起来呀?
 
只要把串口的2脚和3脚连接起来就可以了。
 
接受答案了.
 
为什么我接到的数据是01FFFF01F000
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
533
import
I
I
回复
0
查看
521
import
I
I
回复
0
查看
802
import
I
后退
顶部