码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 之前解析串口数据

时间:2016-05-10 16:46:24      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:

//串口接收数据
procedure TfrmClientMain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
ss,ffnn:string;
rbuf:array of byte;
begin
//setlength(rbuf,0);
memo1.Lines.Add(‘长度:‘+inttostr(bufferlength));
setlength(rbuf,BufferLength);
move(buffer^,pchar(rbuf)^,BufferLength);
ss:=‘接收:‘;
for i:=0 to bufferlength-1 do
begin
ss:=ss+inttohex(rbuf[i],2)+‘ ‘; //接受数据
//showmessage(ss);
end;
memo1.Lines.Add(ss);
//showmessage(inttohex(rbuf[1],1));
if inttohex(rbuf[1],2)=‘43‘ then
begin
DataStream:=TMemoryStream.Create;
DataAll:=0;
PkAll:=0;
PkNo:=0;
RadioNo1:=‘‘;
SayAddr; //发送读收音机编号命令
end;
if inttohex(rbuf[1],2)=‘A1‘ then
begin
GetRadioNo(rbuf); //读收音机编号
SayDataAll; //发送读数据总量命令
end;
if inttohex(rbuf[1],2)=‘A4‘ then
begin
GetDataAll(rbuf); //读数据总量
if dataall=0 then
begin
memo1.Lines.Add(‘卡的内容为空,请拔卡!‘);
end else
begin
SayPkNo; //发送第0个数据包号
end;

end;
if inttohex(rbuf[1],2)=‘D4‘ then
begin
GetPk(rbuf); //接收数据包
if PkNo=PkAll then //当最后一个包接收完毕
begin
memo1.Lines.Add(‘总数据量:‘+inttostr(DataAll)+‘流的大小:‘+inttostr(datastream.size));
if DataAll=DataStream.Size then
begin
ffnn:=SaveFileName;
DataStream.SaveToFile(SourceFileDir+ffnn); //写文件
DataStream.Free;
AScanSThread:=TScanSThread.CreateIt(ffnn);
//SayFormat; //发送格式化命令
end else
begin
DataStream:=TMemoryStream.Create;
DataAll:=0;
PkAll:=0;
PkNo:=0;
RadioNo1:=‘‘;
SayAddr; //重新在进行一遍
end;
end else
begin
SayPkNo; //发送读取下一个包命令
end;
end;
end;

 

procedure TfrmClientMain.GetRadioNo(buf:array of byte);
var
i:integer;
begin
RadioNo1:=BytetoStrings(buf[2])+BytetoStrings(buf[3])+BytetoStrings(buf[4])+BytetoStrings(buf[5]);
memo1.Lines.Add(‘收音机编号:‘+RadioNo1);
end;

 

procedure TfrmClientMain.SayDataAll;
var
sbuf:array[1..7] of byte;
begin
sbuf[1]:=byte($35);
sbuf[2]:=byte($A4); {命令号}
sbuf[3]:=byte($00);
sbuf[4]:=byte($00);
sbuf[5]:=byte($00);
sbuf[6]:=byte($00);
sbuf[7]:=byte($58);
senddata(sbuf);{调用发送函数}
end;

 


procedure TfrmClientMain.GetDataAll(buf:array of byte); //取得数据
var
s:string;
begin
//showmessage(‘###@@@!!!:‘+inttohex(buf[3],2));
s:=inttohex(buf[2],2)+inttohex(buf[3],2);//+inttohex(buf[4],2); 根据090810ctc-r-100新协议修改
DataAll:=strtoint(HexStrToDecStr(s));
memo1.Lines.Add(‘数据总量:‘+inttostr(dataall));
if (dataall mod 128)=0 then
begin
pkall:=DataAll div 128;
end else
begin
pkall:=DataAll div 128+1;
end;
memo1.Lines.Add(‘包数:‘+inttostr(pkall));
end;

Delphi 之前解析串口数据

标签:

原文地址:http://www.cnblogs.com/zhangzhifeng/p/5478233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!