spcomm通讯的接收需要注意什么(菜鸟问题!)(35分)

  • 主题发起人 主题发起人 ESCduxf
  • 开始时间 开始时间
E

ESCduxf

Unregistered / Unconfirmed
GUEST, unregistred user!
本人最近新学delphi,用spcomm控件,发送很好!但无法接收,高手指导指导!
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Comm1: TComm;
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
comm1:tcomm;
viewstring:string;
sbuf:array[0..20] of byte;
rbuf:array[0..20] of byte;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Comm1.StartComm;
sleep(200);
sbuf[0]:=byte(strtoint('$'+'68')); //帧头
sbuf[1]:=byte(strtoint('$'+'03'));
sbuf[2]:=byte(strtoint('$'+'00'));
sbuf[3]:=byte(strtoint('$'+'00'));
sbuf[4]:=byte(strtoint('$'+'00'));
sbuf[5]:=byte(strtoint('$'+'00'));
sbuf[6]:=byte(strtoint('$'+'00'));
sbuf[7]:=byte(strtoint('$'+'68'));
sbuf[8]:=byte(strtoint('$'+'01'));
sbuf[9]:=byte(strtoint('$'+'02'));
sbuf[10]:=byte(strtoint('$'+'52'));
sbuf[11]:=byte(strtoint('$'+'C3'));
sbuf[12]:=byte(strtoint('$'+'EB'));
sbuf[13]:=byte(strtoint('$'+'16'));
Comm1.WriteCommData(@sbuf,14);
//Comm1.StopComm;
end;


procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin

viewstring:='';

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

for i:=1 to bufferlength do
begin
viewstring:=viewstring+ inttohex(rbuf,2)+'' ;
end;
viewstring:='接收'+ viewstring;
//memo1.lines.add(viewstring);
//memo1.lines.add('');
end;

procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
showmessage('错误');
end;

end.
 
procedure TFrmSPComm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
StrReCeive:string;
Str1:String;
begin
SetLength(StrReCeive,BufferLength);
Move(Buffer^,Pchar(StrReceive)^,BufferLength);
Memo1.Lines.Add(StrReCeive);
Memo1.Invalidate;
if StrReCeive=#27#114#112 then
Memo1.Lines.Add('写入成功!');
str1:=Copy(StrReCeive,1,2);
if Str1=#27#115 then
Memo1.Lines.Add('读取成功!');
end;
以上代码通过磁卡机测试成功。
接收的话,如果不是磁卡机,你就必须使用线程接收!!!!
 
谢谢楼上的!还想问一个问题!有关spcomm的各项属性值能否给我解释一下
比如说ReadIntervalTimeout等延时属性
 
后退
顶部