(* Modbus Master for BELIMO V025R+VAC*)
TYPE UDT3
VERSION : 0.1
STRUCT
WORD[0]: WORD;
WORD[1]: WORD;
WORD[2]: WORD;
WORD[3]: WORD;
WORD[4]: WORD;
WORD[5]: WORD;
WORD[6]: WORD;
WORD[7]: WORD;
END_STRUCT ;
END_TYPE
FUNCTION_BLOCK FB1
TITLE = 'BLMOPN'
NAME : BLMOPN
FAMILY : ''
AUTHOR : CT
VERSION : '1.0'
VAR_INPUT
RESTART : BOOL;
TCON_PAR : TCON_PAR;
PollCyle : TIME;
END_VAR
VAR_OUTPUT
CONNECTED : BOOL;
ERROR : BOOL;
END_VAR
VAR_TEMP
i : INT;
RET : INT;
DATA : BYTE;
END_VAR
VAR
U3: UDT3;
TCON : TCON;
TSEND : TSEND;
TRCV : TRCV;
TRCV2 : TRCV;
TDISCON : TDISCON;
TON_CON : TON;
TON_SEND : TOF;
TON_State : TON;
PulsePosConn : BOOL;
PulseNegConn : BOOL;
EdgeFlagPos : BOOL;
EdgeFlagNeg : BOOL;
FG_TRCV : BOOL;
FG_TSND : BOOL;
Count : INT := 1;
HEADER : ARRAY[0..10] OF BYTE;
REGS : ARRAY[0..255] OF BYTE;
(*********************************************************)
(* Build the ModbusTCP Request *)
(*********************************************************)
KOMTEL : STRUCT
HEADER_TI : WORD := 1;
HEADER_PI : WORD := 0;
HEADER_LFL : BYTE := 0;
HEADER_HFL : BYTE := 6;
SLAVE : BYTE := 7;
FUNCTIONCODE : BYTE := 3;
STARTREG : INT := 0;
NOOFREGS : INT := 8;
END_STRUCT;
END_VAR
BEGIN
(*********************************************************)
(* Establish connection *)
(*********************************************************)
TCON(REQ := RESTART,
ID := TCON_PAR.id,
CONNECT := TCON_PAR);
IF TCON.DONE THEN
CONNECTED := TRUE;
END_IF;
TON_CON(IN := CONNECTED,
PT := T#2s);
(*********************************************************)
(* Send data over existing connection *)
(*********************************************************)
TSEND.LEN := 12;
IF TON_CON.Q THEN
TON_SEND(IN := NOT TON_SEND.Q AND NOT TRCV2.NDR,
PT := PollCyle);
FG_TSND := NOT TON_SEND.Q ;
TSEND(REQ := FG_TSND,
ID := TCON_PAR.id,
LEN := TSEND.LEN,
DATA := KOMTEL);
END_IF;
(*********************************************************)
(* Send Error *)
(*********************************************************)
IF TSEND.ERROR THEN
ERROR := TRUE;
END_IF;
IF TSEND.DONE THEN
ERROR := FALSE;
END_IF;
(*********************************************************)
(* Receive Header *)
(*********************************************************)
IF CONNECTED THEN
TRCV(EN_R := NOT FG_TRCV,
ID := TCON_PAR.ID,
LEN := 9,
DATA := HEADER);
IF TRCV.NDR THEN
FG_TRCV := TRUE;
END_IF;
(*********************************************************)
(* Recieve Registers *)
(*********************************************************)
TRCV2(EN_R := FG_TRCV,
ID := TCON_PAR.ID,
LEN := 0,
DATA := REGS);
IF TRCV2.NDR THEN (* Copy Registers to DB*)
CASE (BYTE_TO_INT(HEADER[1])) OF
7: ret := SFC20(SRCBLK := REGS,DSTBLK := U3);
END_CASE;
FG_TRCV := FALSE;
END_IF;
END_IF;
IF ERROR OR NOT CONNECTED THEN (*Error: Set all values to 0 if Device is not reachable*)
DATA := 0;
END_IF;
END_FUNCTION_BLOCK