Z
z_f123
Unregistered / Unconfirmed
GUEST, unregistred user!
问题:数据帧的帧头 AA+数据+帧尾BB,当数据多于7个字节的时候总是 出问题,请大家帮忙诊断一下
程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, strutils, OleCtrls, MSCommLib_TLB, ExtCtrls,Math,shellapi,
Buttons;
type
TForm1 = class(TForm)
Comm: TMSComm;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Memo1: TMemo;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure CommComm(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function HEXTOD(sstr:string):string;
function savedasta(aa:string):boolean;
end;
var
Form1: TForm1;
var
BEGINDATA:BOOLEAN;
ENDDATA:BOOLEAN;
dcount:integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
comm.RThreshold :=1;
/////comm.Settings :='9600,n,8,1';
//comm.CommPort :=1;
memo1.Text :='';
edit1.Text :='';
comm.InputLen :=0;
comm.InputMode :=cominputmodebinary;
BEGINDATA:=false;//BOOLEAN;
ENDDATA:=false;//BOOLEAN;
comm.InBufferCount :=0;
dcount:=1;
// comm.PortOpen :=false;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
//comm.PortOpen :=true;
if comm.PortOpen then
comm.PortOpen :=false;
comm.Settings :='9600,n,8,1' ;
comm.CommPort:=1;
comm.RThreshold :=2;
comm.InputLen :=0;
comm.InputMode :=cominputmodebinary;
//
comm.PortOpen:=true;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if comm.PortOpen =false then
comm.PortOpen :=true;
comm.PortOpen :=false;
end;
//
procedure TForm1.CommComm(Sender: TObject);
var rxdataolelevariant;
//rxdatabyt:byte;
rxdatabyt1:array of byte;
rxdatastr:array of string;
i,n:integer;
datafile:textfile;
dataname,s,ss:string;
begin
case comm.CommEvent of
comevreceive:
begin
n:=comm.InBufferCount;
rxdataole:=comm.Input;
setlength(rxdatabyt1,n);
setlength(rxdatastr,n);
rxdatabyt1:=rxdataole;
for i:=0 to n-1 do
begin
rxdatastr:=inttohex(rxdatabyt1,2);
s:=rxdatastr;
if (s='aa') or (s='AA') THEN
BEGIN
BEGINDATA:=TRUE;
ENDDATA:=FALSE;
CONTINUE;
END;
IF (s='BB') OR (s='bb') THEN
BEGIN
BEGINDATA:=false;
ENDDATA:=true;
if dcount=6 then////////////dcount的值如果设大于8就接收不到数据
begin
form1.memo1.Lines.add(ss);
form1.memo1.Lines.SaveToFile('d:/d.dat');
ss:='';
s:='';
dcount:=1;
end;
CONTINUE;
END;
IF BEGINDATA THEN
BEGIN
SS:=SS+S+' '; //
dcount:=dcount+1;
END;
end;
程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, strutils, OleCtrls, MSCommLib_TLB, ExtCtrls,Math,shellapi,
Buttons;
type
TForm1 = class(TForm)
Comm: TMSComm;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Memo1: TMemo;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure CommComm(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function HEXTOD(sstr:string):string;
function savedasta(aa:string):boolean;
end;
var
Form1: TForm1;
var
BEGINDATA:BOOLEAN;
ENDDATA:BOOLEAN;
dcount:integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
comm.RThreshold :=1;
/////comm.Settings :='9600,n,8,1';
//comm.CommPort :=1;
memo1.Text :='';
edit1.Text :='';
comm.InputLen :=0;
comm.InputMode :=cominputmodebinary;
BEGINDATA:=false;//BOOLEAN;
ENDDATA:=false;//BOOLEAN;
comm.InBufferCount :=0;
dcount:=1;
// comm.PortOpen :=false;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
//comm.PortOpen :=true;
if comm.PortOpen then
comm.PortOpen :=false;
comm.Settings :='9600,n,8,1' ;
comm.CommPort:=1;
comm.RThreshold :=2;
comm.InputLen :=0;
comm.InputMode :=cominputmodebinary;
//
comm.PortOpen:=true;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if comm.PortOpen =false then
comm.PortOpen :=true;
comm.PortOpen :=false;
end;
//
procedure TForm1.CommComm(Sender: TObject);
var rxdataolelevariant;
//rxdatabyt:byte;
rxdatabyt1:array of byte;
rxdatastr:array of string;
i,n:integer;
datafile:textfile;
dataname,s,ss:string;
begin
case comm.CommEvent of
comevreceive:
begin
n:=comm.InBufferCount;
rxdataole:=comm.Input;
setlength(rxdatabyt1,n);
setlength(rxdatastr,n);
rxdatabyt1:=rxdataole;
for i:=0 to n-1 do
begin
rxdatastr:=inttohex(rxdatabyt1,2);
s:=rxdatastr;
if (s='aa') or (s='AA') THEN
BEGIN
BEGINDATA:=TRUE;
ENDDATA:=FALSE;
CONTINUE;
END;
IF (s='BB') OR (s='bb') THEN
BEGIN
BEGINDATA:=false;
ENDDATA:=true;
if dcount=6 then////////////dcount的值如果设大于8就接收不到数据
begin
form1.memo1.Lines.add(ss);
form1.memo1.Lines.SaveToFile('d:/d.dat');
ss:='';
s:='';
dcount:=1;
end;
CONTINUE;
END;
IF BEGINDATA THEN
BEGIN
SS:=SS+S+' '; //
dcount:=dcount+1;
END;
end;