Delphi 之前解析串口数据


本文整理自网络,侵删。

 //串口接收数据
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 edit1.gettextlen 获取编辑框字符数

Delphi对cookie的操作

Delphi firedac与ado读写数据的性能测试

Delphi memo1 数字列设定取值范围

Delphi indy 10.5.7的数据发送接收的用法

Delphi 获取 设置文件时间

Delphi 获取窗体坐标

Delphi编程实现sql server挂起清除

Delphi 提升权限查找进程关闭进程单元

Delphi windows 根据进程名结束进程的代码

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...