在网上看见一个程序,我想改一下,想把返回的值写到数组里,然后调用一个处理函数
procedure Tfrmcom.cpdrecPacket(Sender: TObject; const Str: String);
var
strtemp:string;
i,j:integer;
begin
j:=0;
j:=j+strtointdef(copy(StatusBar1.Panels[1].Text,4,length(StatusBar1.Panels[1].Text)-3),0);
if chkrechex.Checked then
begin
for i:=1 to length(str) do
//if str[i]<>' ' then
begin
strtemp := trim(strtemp+' '+inttohex(ord(str[i]),2));
j:=j+1;
StatusBar1.Panels[1].Text:='RX:'+inttostr(j);
end;
if not bstop then
memrec.Text:= trim(memrec.Text+' ' + strtemp) ; end
else
begin
if not bstop then
memrec.Text :=memrec.Text +str;
j:=j+length(str);
StatusBar1.Panels[1].Text:='RX:'+inttostr(j);
end; if chkautoclean.Checked then
if memrec.Lines.Count>2000 then
memrec.Text :='';
end;想把返回的值写到数组里,然后调用一个处理函数
//接收处理
procedure TFrmMain.response_proc(DataMsg:array of Byte; DataLen:Integer);
begin
//仅为了演示,只包含读取温湿度的返回处理
//首先判断接收长度 小于5个字节一定不对
if (DataLen>=5) then
begin
//CRC效验
tmpCRC:=CRC16(ArrInfo,DataLen-2);
if (tmpCRC mod 255 = ArrInfo[DataLen-2]) and (tmpCRC div 255 = ArrInfo[DataLen-1]) then
begin
//判断响应的设备地址是否正确
if IntToHex2(ArrInfo[0])=Copy(trim(Memo2.Text),0,2) then
begin
//判断响应的功能是否正确
if IntToHex2(ArrInfo[1])=Copy(trim(Memo2.Text),3,2) then
begin
//...测试通讯
//...读取地址
//...写入地址
//读取温湿度
if ArrInfo[1]=$03 then
begin
edit1.Text:=IntToStr(ArrInfo[2])
end
//...读取工作模式
//...写入工作模式
end
else
begin
MessageBox(0, '数据接收错误,功能码不正确!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end
else
begin
MessageBox(0, '数据接收错误,响应主机地址不正确!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end
else
begin
MessageBox(0, '数据接收错误,CRC效验错误!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end
else
begin
MessageBox(0, '数据接收错误,数据不完整!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end;
procedure Tfrmcom.cpdrecPacket(Sender: TObject; const Str: String);
var
strtemp:string;
i,j:integer;
begin
j:=0;
j:=j+strtointdef(copy(StatusBar1.Panels[1].Text,4,length(StatusBar1.Panels[1].Text)-3),0);
if chkrechex.Checked then
begin
for i:=1 to length(str) do
//if str[i]<>' ' then
begin
strtemp := trim(strtemp+' '+inttohex(ord(str[i]),2));
j:=j+1;
StatusBar1.Panels[1].Text:='RX:'+inttostr(j);
end;
if not bstop then
memrec.Text:= trim(memrec.Text+' ' + strtemp) ; end
else
begin
if not bstop then
memrec.Text :=memrec.Text +str;
j:=j+length(str);
StatusBar1.Panels[1].Text:='RX:'+inttostr(j);
end; if chkautoclean.Checked then
if memrec.Lines.Count>2000 then
memrec.Text :='';
end;想把返回的值写到数组里,然后调用一个处理函数
//接收处理
procedure TFrmMain.response_proc(DataMsg:array of Byte; DataLen:Integer);
begin
//仅为了演示,只包含读取温湿度的返回处理
//首先判断接收长度 小于5个字节一定不对
if (DataLen>=5) then
begin
//CRC效验
tmpCRC:=CRC16(ArrInfo,DataLen-2);
if (tmpCRC mod 255 = ArrInfo[DataLen-2]) and (tmpCRC div 255 = ArrInfo[DataLen-1]) then
begin
//判断响应的设备地址是否正确
if IntToHex2(ArrInfo[0])=Copy(trim(Memo2.Text),0,2) then
begin
//判断响应的功能是否正确
if IntToHex2(ArrInfo[1])=Copy(trim(Memo2.Text),3,2) then
begin
//...测试通讯
//...读取地址
//...写入地址
//读取温湿度
if ArrInfo[1]=$03 then
begin
edit1.Text:=IntToStr(ArrInfo[2])
end
//...读取工作模式
//...写入工作模式
end
else
begin
MessageBox(0, '数据接收错误,功能码不正确!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end
else
begin
MessageBox(0, '数据接收错误,响应主机地址不正确!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end
else
begin
MessageBox(0, '数据接收错误,CRC效验错误!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end
else
begin
MessageBox(0, '数据接收错误,数据不完整!', '数据接收', MB_OK or MB_ICONWARNING);
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货