如果你有moxa的窜口驱动程序,实现这个功能非常简单。以下是用于工业RS485通讯的例子
如果你要检测别人的程序所发送或接受的数据,你需要2台机器,自己编程序,作用相当于路由器。
Function GetPIDValue(ID: integer;Act: integer;Var Value:Real): Boolean;
var
S1,S2 : String;
i1,K,L : Longint;
i2 : integer;
CRCB : Byte;
Begin
Str(ID,S1);
if Length(S1)<2 then S1:='0'+S1;
S1:=Chr(4)+S1+ActDefine[Act]+Chr(5);
sioFlush(RS485Port,2);
i1:=sioWrite(RS485Port,S1,Length(S1));
if i1<0 then
Begin
WriteError(MyErrMsg(20));//写通讯错误
RS485Alarm:=True;
GetPIDValue:=False;
Exit;
End;
K:=0;
L:=sioIqueue(RS485Port);//有多少在缓冲区
Repeat
Sleep(20);
if L=sioIqueue(RS485Port) then inc(K)
else L:=sioIqueue(RS485Port)
until K>=2;
if L>0 then
Begin
SetLength(S2,100);//缓冲可以很大
i2:=sioRead(RS485Port,S2,L);
if i2>0 then
Begin
S2[i2+1]:=chr(0);
While (S2<>'')and(S2[1]=#4) do Delete(S2,1,1);
if Length(S2)>=11 then
Begin
CRCB:=0;
For i2:=2 to 10 do
CRCB:=CRCB XOR Ord(S2[i2]);
if Ord(S2[11])=CRCB then
Begin
Val(Copy(S2,4,6),Value,i2);
GetPIDValue:=True;
RS485Alarm:=False;
Exit;
End else Begin WriteError(MyErrMsg(23)); RS485Alarm:=True;end;//通讯校验错
End else Begin WriteError(MyErrMsg(22));RS485Alarm:=True;end;//通讯不完整
End else Begin WriteError(MyErrMsg(21));RS485Alarm:=True;end;//读通讯错误
End else Begin WriteError(MyErrMsg(24));RS485Alarm:=True;end;;//通讯无回应
GetPIDValue:=False;
End;