You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

442 lines
13 KiB

FUNCTION_BLOCK BS_ModbusCom
VAR_EXTERNAL
END_VAR
VAR_INPUT
SerialCom : byte;
NetSend : array[0..64] of NetSendConfig;
NetSendDelay : time;
ComSend : array[0..64] of ComSendConfig;
ComSendDelay : time; (*t#200ms*)
ComRecvDelay : time; (*t#120ms*)
ComAnalysisDelay: time; (*t#150ms*)
DataReadLength : int;
END_VAR
VAR_OUTPUT
DataStr : array[0..128] of byte;
DataRead : array[0..128] of DataFormat;
END_VAR
VAR
SendFB : NW_SerSend;
RecvFB : NW_SerRecv;
SendP : pointer;
RecvP : pointer;
Send_act : bool;
Recv_act : bool;
i,j,k : int;
i0,i1,i2,i3 : int;
CRC16Lo1 : Byte;
CRC16Hi1 : Byte;
CRC16Lo : Byte;
CRC16Hi : Byte;
CL,CH : Byte;
SaveHi,SaveLo : Byte;
Flag : int;
ENO_Count0 : dword;
ENO_Count1 : dword;
timer1 : ton;
timer1_run : bool;
NetVarSend1 : NetVarSend;
t1 : ton;
t1_in : bool;
t1_q : bool;
t2 : ton;
t2_in : bool;
t2_q : bool;
t3 : ton;
t3_in : bool;
t3_q : bool;
Commandsend : int;
SendStr : array[0..8] of byte;
RecvStr : array[0..200] of byte;
CalcStr : array[0..200] of byte;
ErrStr : array[0..100] of int;
Recv_len : int;
Recv_lenp : int;
Recv_DataLen : int;
Recv_CRCLo : int;
Recv_CRCHi : int;
TestMode : bool;
StopMem : int;
ComTimeoutCount : int;
ComTimeoutClear : bool;
checkmark1 : bool;
checkmark2 : bool;
checkmark3 : bool;
checkmark4 : bool;
DATA1 : int;
DATA2 : int;
TempStr : array[0..128] of byte;
TempReal : real;
v2v : NW_MemCopy;
P1 : Pointer;
P2 : Pointer;
END_VAR
(*
20200915
1.3
Modbus03/04命令的读取及解析
Modbus06命令的单个寄存器写入
input 输入说明
SerialCom : byte;
NetSend : array[0..64] of NetSendConfig;
NetSendDelay : time;
ComSend : array[0..64] of ComSendConfig;
ComSendDelay : time;
ComRecvDelay : time;
ComAnalysisDelay: time;
DataReadLength : int;
DataType : array[0..256] of int ;
output 输出说明
DataStr : array[0..256] of byte;
DataRead : array[0..256] of DataFormat;
config 读写设置
ComSend[1].Enable :=1; 读取启用1 0测试为2;
ComSend[1].EquipAddr :=1; 设备站号0-255
ComSend[1].FunctionCode :=16#03; HR16#03AR16#04 HR16#06
ComSend[1].StartAddr :=2504; Modscan测试地址
ComSend[1].Length :=12; 读取寄存器数量
ComSend[1].StartMem :=0; 存放入DataStr数组起始位置+*2
ComSend[1].WriteDataH1 :=0; 写入数据的高位
ComSend[1].WriteDataL1 :=WriteTestW; 写入数据的低位
read 读取数据
DataType[00]:=1; 数据类型设置 1 int 2 float 3 byte 0 0
DATA:=DataRead[00].TO_BYTE; 分别对应 DataType 3
DATA:=DataRead[00].TO_REAL; 分别对应 DataType 2
DATA:=DataRead[00].TO_INT; 分别对应 DataType 1
EXC5000C3版本以上CPUCPUCOMCPUCOMModbus
仿MeterSim
*)
(********************************************************************************************)
(*
EXC1000COM口485读取9600BPS所得
线访
*)
(****************************************************************************************)
t1_in:=not t1.q;
t1(IN :=t1_in , PT :=ComSendDelay );
t2(IN :=t1_in , PT :=ComRecvDelay );
t3(IN :=t1_in , PT :=ComAnalysisDelay );
if t1.q=0 and t1_q=1 then
Send_act:=1;
else
Send_act:=0;
end_if;
if t2.q=1 then
Recv_act:=1;
else
Recv_act:=0;
end_if;
if ComSend[1].Enable=1 then (**)
if Send_act=1 then
if TestMode=0 then
Commandsend:=Commandsend + 1;
end_if;
end_if;
if ComSend[Commandsend].Enable=0 then (**)
Commandsend:=0;
end_if;
if ComSend[Commandsend].Enable=1 then
(**)
if ComSend[Commandsend].FunctionCode = 16#03 then
SendStr[0]:=ComSend[Commandsend].EquipAddr;(**)
SendStr[1]:=ComSend[Commandsend].FunctionCode;(**)
SendStr[2]:=int_to_byte((ComSend[Commandsend].StartAddr-1) / 256);(**)
SendStr[3]:=int_to_byte((ComSend[Commandsend].StartAddr-1) mod 256);(**)
SendStr[4]:=0;(**)
SendStr[5]:=ComSend[Commandsend].Length;(**)
end_if;
(**)
if ComSend[Commandsend].FunctionCode = 16#04 then
SendStr[0]:=ComSend[Commandsend].EquipAddr;(**)
SendStr[1]:=ComSend[Commandsend].FunctionCode;(**)
SendStr[2]:=int_to_byte((ComSend[Commandsend].StartAddr-1) / 256);(**)
SendStr[3]:=int_to_byte((ComSend[Commandsend].StartAddr-1) mod 256);(**)
SendStr[4]:=0;(**)
SendStr[5]:=ComSend[Commandsend].Length;(**)
end_if;
(**)
if ComSend[Commandsend].FunctionCode = 16#06 then
SendStr[0]:=ComSend[Commandsend].EquipAddr;(**)
SendStr[1]:=ComSend[Commandsend].FunctionCode;(**)
SendStr[2]:=int_to_byte((ComSend[Commandsend].StartAddr-1) / 256);(**)
SendStr[3]:=int_to_byte((ComSend[Commandsend].StartAddr-1) mod 256);(**)
SendStr[4]:=ComSend[Commandsend].WriteDataH1;
SendStr[5]:=ComSend[Commandsend].WriteDataL1;
end_if;
end_if;
if ComSend[Commandsend].Enable=2 then
SendStr[0]:= 16#00 ;
SendStr[1]:= 16#00 ;
SendStr[2]:= 16#00 ;
SendStr[3]:= 16#00 ;
SendStr[4]:= 16#00 ;
SendStr[5]:= 16#00 ;
end_if;
(*CRC校验*)
CRC16Lo := 255;
CRC16Hi := 255;
CL := 1;
CH := 160;
for i := 0 To 5 by 1 do
CRC16Lo := CRC16Lo xor SendStr[i];
for Flag := 0 to 7 by 1 do
SaveHi := CRC16Hi;
SaveLo := CRC16Lo;
CRC16Hi := shr(CRC16Hi,1);
CRC16Lo := shr(CRC16Lo,1);
if ((SaveHi and 1) = 1) then
CRC16Lo := CRC16Lo Or 128;
end_If;
If ((SaveLo and 1) = 1) then
CRC16Hi := CRC16Hi xor CH;
CRC16Lo := CRC16Lo xor CL;
end_if;
end_for;
end_for;
SendStr[6]:=CRC16Lo;
SendStr[7]:=CRC16Hi;
end_if;
if t3.q=1 then
(**)
if Recv_len>=6 then(**)
Recv_lenp :=Recv_len-3;(*-2,0开始-1*)
CRC16Lo := 255;
CRC16Hi := 255;
CL := 1;
CH := 160;
for i := 0 To Recv_lenp by 1 do
CRC16Lo := CRC16Lo xor RecvStr[i];
for Flag := 0 to 7 by 1 do
SaveHi := CRC16Hi;
SaveLo := CRC16Lo;
CRC16Hi := shr(CRC16Hi,1);
CRC16Lo := shr(CRC16Lo,1);
if ((SaveHi and 1) = 1) then
CRC16Lo := CRC16Lo Or 128;
end_If;
If ((SaveLo and 1) = 1) then
CRC16Hi := CRC16Hi xor CH;
CRC16Lo := CRC16Lo xor CL;
end_if;
end_for;
end_for;
CRC16Hi1:=CRC16Hi;
CRC16Lo1:=CRC16Lo;
Recv_CRCLo:=Recv_lenp+1;
Recv_CRCHi:=Recv_lenp+2;
(*
funtionCode03
Tx 设备地址 03 起始地址高位 CRCH CRCL
Rx 设备地址 03 字节数 1高位 1低位... CRCH CRCL
Recv_DataLen =(RecvStr[2])-1 是因为数据长度=RecvStr[2]-1i从0开始所以-1
j = i+3 是因为数据位是从第3位之后开始的
funtionCode06
Tx 设备地址 06 起始地址高位 CRCH CRCL
Rx 设备地址 06 起始地址高位 CRCH CRCL
Recv_DataLen =(RecvStr[2])-1 是因为数据长度=2单个寄存器读取只有2位-1i从0开始所以-1
j = i+4 是因为数据位是从第4位之后开始的
*)
if ComSend[Commandsend].FunctionCode = 16#03 or ComSend[Commandsend].FunctionCode = 16#04 then
Recv_DataLen:=byte_to_int(RecvStr[2])-1;
end_if;
if ComSend[Commandsend].FunctionCode = 16#06 then
Recv_DataLen:=1;
end_if;
if Commandsend<>0 then
if RecvStr[Recv_CRCLo]=CRC16Lo and RecvStr[Recv_CRCHi]=CRC16Hi then(**)
for i:=0 to Recv_DataLen by 1 do
if ComSend[Commandsend].FunctionCode = 16#03 or ComSend[Commandsend].FunctionCode = 16#04 then
j:=i+3;
end_if;
if ComSend[Commandsend].FunctionCode = 16#06 then
j:=i+4;
end_if;
if (SendStr[2]=int_to_byte((ComSend[Commandsend].StartAddr-1) / 256)) and (SendStr[3]=int_to_byte((ComSend[Commandsend].StartAddr-1) mod 256) )and (SendStr[1]=ComSend[Commandsend].FunctionCode) then(**)
CalcStr[i]:=RecvStr[j];
k:=ComSend[Commandsend].StartMem+i;
DataStr[k]:=CalcStr[i];
end_if;
end_for;
ErrStr[commandsend]:=0;
else
(**)
ErrStr[Commandsend]:=ErrStr[Commandsend]+1;
if ErrStr[Commandsend]>999 then
ErrStr[Commandsend]:=999;
end_if;
end_if;
end_if;
(**)
for i:=0 to 200 by 1 do
RecvStr[i]:=0;
CalcStr[i]:=0;
end_for;
(**)
if ErrStr[Commandsend]>ComTimeoutCount and ComTimeoutClear=1 then
StopMem:=ComSend[Commandsend].StartMem+byte_to_int(ComSend[Commandsend].Length);
for i:=ComSend[Commandsend].StartMem to StopMem by 1 do
DataStr[i]:=0;
end_for;
end_if;
end_if;
end_if;
SendP:=&SendStr;
RecvP:=&RecvStr;
SendFB(EN :=Send_act , COM :=SerialCom , DATA :=SendP , LENGTH :=8 , IQM :=0 );
RecvFB(EN :=Recv_act , COM :=SerialCom , DATA :=RecvP , IQM :=0 | Recv_len:= LENGTH);
t1_q:=t1.q;
t2_q:=t2.q;
t3_q:=t3.q;
(****************************************************************************************)
for i := 0 To DataReadLength by 1 do
(* Type 为0时全读*)
i0:=i;
i1:=i+1;
i2:=i+2;
i3:=i+3;
(* 1 int *)
DataRead[i].TO_INT:=byte_to_int(DataStr[i0])*256+byte_to_int(DataStr[i1]);
(* 2 Float *)
TempStr[0]:=DataStr[i1];
TempStr[1]:=DataStr[i0];
TempStr[2]:=DataStr[i3];
TempStr[3]:=DataStr[i2];
P1:=&TempStr;
P2:=&TempReal;
v2v(DEST :=P2 , SRC :=P1 , LENGTH :=4 , IQM :=0 );
DataRead[i].TO_REAL:=TempReal;
(* 3 byte *)
DataRead[i].TO_BYTE:=1;
end_for;
(*
for i := 0 To DataReadLength by 1 do
if DataType[i]=1 or DataType[i]=0 then
i0:=i;
i1:=i+1;
DataRead[i].TO_INT:=byte_to_int(DataStr[i0])*256+byte_to_int(DataStr[i1]);
end_if;
if DataType[i]=2 or DataType[i]=0 then
i0:=i;
i1:=i+1;
i2:=i+2;
i3:=i+3;
TempStr[0]:=DataStr[i1];
TempStr[1]:=DataStr[i0];
TempStr[2]:=DataStr[i3];
TempStr[3]:=DataStr[i2];
P1:=&TempStr;
P2:=&TempReal;
v2v(DEST :=P2 , SRC :=P1 , LENGTH :=4 , IQM :=0 );
DataRead[i].TO_REAL:=TempReal;
end_if;
if DataType[i]=3 or DataType[i]=0 then
i0:=i;
DataRead[i].TO_BYTE:=DataStr[i];
end_if;
4 BCD
if DataType[i]=4 or DataType[i]=0 then
i0:=i;
i1:=i+1;
k1:=byte_to_int(DataStr[i0])*256+byte_to_int(DataStr[i1]);
DataRead[i].TO_BCD01:=int_to_bool( k1 Mod 2#0000000000000001 );
DataRead[i].TO_BCD02:=int_to_bool( k1 Mod 2#0000000000000010 );
DataRead[i].TO_BCD03:=int_to_bool( k1 Mod 2#0000000000000100 );
DataRead[i].TO_BCD04:=int_to_bool( k1 Mod 2#0000000000001000 );
DataRead[i].TO_BCD05:=int_to_bool( k1 Mod 2#0000000000010000 );
DataRead[i].TO_BCD06:=int_to_bool( k1 Mod 2#0000000000100000 );
DataRead[i].TO_BCD07:=int_to_bool( k1 Mod 2#0000000001000000 );
DataRead[i].TO_BCD08:=int_to_bool( k1 Mod 2#0000000010000000 );
DataRead[i].TO_BCD09:=int_to_bool( k1 Mod 2#0000000100000000 );
DataRead[i].TO_BCD10:=int_to_bool( k1 Mod 2#0000001000000000 );
DataRead[i].TO_BCD11:=int_to_bool( k1 Mod 2#0000010000000000 );
DataRead[i].TO_BCD12:=int_to_bool( k1 Mod 2#0000100000000000 );
DataRead[i].TO_BCD13:=int_to_bool( k1 Mod 2#0001000000000000 );
DataRead[i].TO_BCD14:=int_to_bool( k1 Mod 2#0010000000000000 );
DataRead[i].TO_BCD15:=int_to_bool( k1 Mod 2#0100000000000000 );
DataRead[i].TO_BCD16:=int_to_bool( k1 Mod 2#1000000000000000 );
end_if;
*)
END_FUNCTION_BLOCK