我是新手,在用spcomm控件通信的时候为什么总是收不到数据.代码如下:谢谢解答!!(100分)

  • 主题发起人 主题发起人 我是变量
  • 开始时间 开始时间

我是变量

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;

procedure TFCOMM.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;

procedure 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(5);

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:='';

//SetLength(viewstring, BufferLength);

//move(buffer^,pchar(viewstring)^,bufferlength);

move(buffer^,pchar(@rbuf)^,bufferlength);

for i:=1 to bufferlength do

viewstring:=viewstring + inttohex(rbuf,2)+'';


viewstring:='接收' + viewstring;

memo1.lines.add(viewstring);

//Memo1.Invalidate;

end;
总是显示发送f001ffff01f0为什么没有显示接收的数据呢????谢谢高手帮我解答...

 
我给你看看我做的,你参考一下吧
unit Comm;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
w:boolean;
begin
w:=false;
while (w=false) do
begin
w:=Comm1.WriteCommData('N001A',5 ); //象串口发送指令
sleep(5000);
end; // WriteCommData(pDataToWrite: PChar;dwSizeofDataToWrite:Word ): boolean
Label1.Visible:=true;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Comm1.StopComm;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
Comm1.StartComm;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
p1,p2:PAnsiChar;
s:AnsiString;
begin
if BufferLength>=264 then
begin
p2:=AnsiStrPos(Buffer,'End*****');
p1:=AnsiStrPos(Buffer,'Nodata**');
if (p1=nil) and (p2=nil) then
begin
SetLength(S, BufferLength); //接收RS232的数据并显示Memo1上。
Move(Buffer^, PChar(S)^, BufferLength);
Memo1.Lines.Add(S);
Memo1.Invalidate;
end;
end;
end;
end.
下面是DFM文件
object Form1: TForm1
Left = 213
Top = 238
Width = 725
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
Scaled = False
OnClose = FormClose
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 72
Top = 400
Width = 216
Height = 29
Caption = '写数据成功了!!!'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -24
Font.Name = 'MS Sans Serif'
Font.Style = []
ParentFont = False
Visible = False
end
object Button1: TButton
Left = 632
Top = 400
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 0
Top = 0
Width = 717
Height = 361
Align = alTop
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object Comm1: TComm
CommName = 'COM2'
BaudRate = 9600
ParityCheck = False
Outx_CtsFlow = False
Outx_DsrFlow = False
DtrControl = DtrEnable
DsrSensitivity = False
TxContinueOnXoff = True
Outx_XonXoffFlow = True
Inx_XonXoffFlow = True
ReplaceWhenParityError = False
IgnoreNullChar = False
RtsControl = RtsEnable
XonLimit = 500
XoffLimit = 500
ByteSize = _8
Parity = None
StopBits = _1
XonChar = #17
XoffChar = #19
ReplacedChar = #0
ReadIntervalTimeout = 100
ReadTotalTimeoutMultiplier = 0
ReadTotalTimeoutConstant = 0
WriteTotalTimeoutMultiplier = 0
WriteTotalTimeoutConstant = 0
OnReceiveData = Comm1ReceiveData
Left = 24
Top = 40
end
end
 
如果硬件连线没有问题(用串口调试软件试一下),就单步调试跟踪。。。。。

我现在用笔记本没有串口 。。。。

最后自己买一本书看看"清华,人民邮电 " 都可以!
 
直接使用控件不好控制协议流程,还是改造一下再用,直接在自己的线程结合协议进行读写
 
楼主的接收数组与发送数组在那儿定义的,找不到,
接收方:
var
i:integer;
a:array[1..6]og byte;
s:string;
begin
move(buffer^,a,bufferlength);
for i:=1 to bufferlength do
s:=s + inttohex(rbuf,2)+'';
s:='接收' + s;
memo1.lines.add(s);
end;

肯定可收到,如果串口没问题的话,呵呵!
 
后退
顶部