串口数据发送与接收问题(高手请进,急急急。)(50分)

  • 主题发起人 主题发起人 cbq16
  • 开始时间 开始时间
C

cbq16

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我在工程中要用到的串口发送与接收代码,
请问我在发送如下数据时: sbuf[1]:=byte($f0);
//帧头
sbuf[2]:=byte($01);
//命令号
sbuf[3]:=byte($13);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($13);
sbuf[6]:=byte($f0);
//帧尾
它接收不到13的数据呢,接受数组接收的数据是如下这样的,
rbuf=[F0,01,FF,F0,0,0,0,0]
请问这是怎么回事儿呀,我该怎么解决这个问题呀,能说得详细一些吗,再现等待,急急急。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm, ExtCtrls;
type
TFCOMM = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Button3: TButton;
Comm1: TComm;
procedure Button3Click(Sender: TObject);
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
{ Public declarations }
end;

var
FCOMM: TFCOMM;
viewstring:string;
i:integer;
rbuf,sbuf:array[1..8] of byte;
implementation
{$R *.dfm}
procedure TFCOMM.Button3Click(Sender: TObject);
begin
application.Terminate;
end;

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

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

//自定义发送数据过程
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 6do
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($13);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($13);
sbuf[6]:=byte($f0);
//帧尾
senddata;//调用发送函数
end;

procedure TFCOMM.Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
i:integer;
str:string;
begin
viewstring:='';
// setLength(rbuf, BufferLength);
move(buffer^,pchar((@rbuf)^),BufferLength);
for i:=1 to bufferlengthdo
begin
viewstring:=viewstring+inttohex(rbuf,2)+'';
end;
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
end;

end.
 
procedure TFCOMM.Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
i:integer;
str:string;
begin
viewstring:='';
// setLength(rbuf, BufferLength);
move(buffer^,pchar((@rbuf)^),BufferLength);//这个地方好像不对吧
//应该是这样吧
Move(Buffer^,rbuf[0],BufferLength);
for i:=1 to bufferlengthdo
begin
viewstring:=viewstring+inttohex(rbuf,2)+'';
end;
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
end;
 
你这样看一下行不行
//向端口发送数据
procedure TfrmComm.btnSendClick(Sender: TObject);
var
str: Pchar;
Count: integer;
begin
if edtSendText.Text='' then
raise exception.create('发送字符串为空,错误终止');
COUNT:=edtSendText.GetTextLen;//获取文本框里字符长度
str := Pchar(edtSendText.Text);
//Count := Length(str);
if Comm.WriteCommData(str,Count) then
//str参数为要发送的字符串,count参数设置发送字符串的长度。函数用于将一个字符串发送到写进程,发送成功返回TRUE反之返回FLASE
begin
mmoComm.lines.Add('已发送字符串为:'+str );//'总共有'+ intTostr(Count) + '个字符')
mmoComm.lines.Add ('串长度'+ intTostr(Count) + '个字符');
end
else
raise exception.Create('发送错误');
end;

//从端口接收数据
procedure TfrmComm.CommReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
strRecv : string;
begin
setLength(strRecv,BufferLength);
Move(Buffer^,pchar(strRecv)^,BufferLength);
mmoComm.Lines.Add('已收到: '+intTostr(BufferLength)+'字节的数据');
mmoComm.Lines.Add(strRecv);
mmoComm.Invalidate ;
end;
 
问题已解决了,是参数设置的问题。谢谢大家了
 
请问各位,我照着上面的程序敲进去为什么会有下面的提示呢
[Error] serial.pas(98): There is no overloaded version of 'IntToHex' that can be called with these arguments
 
后退
顶部