Programming in ST Language
The example is programmed in ST structured litteral language. The dedicated section is under the same master task (MAST).
(* Function to write %MW100 to %MW140 in slave Y *)(* --------------------------------------------- *)CASE Step_4 OF 0: (* Initialization *) IF (Start_4) THEN (* trigger flag *) Error_4 := 0; Step_4 := 5; (* next step *) END_IF; 5: (* Send command to switch serial port from Modbus to character mode *) READ_STS(Ioddt_Pcmcia_0_3_1); (* read serial port status *) IF (Ioddt_Pcmcia_0_3_1.EXCH_STS = 0) THEN (* no active command *) Ioddt_Pcmcia_0_3_1.CONTROL := 16#00; (* reset control word *) SET(Ioddt_Pcmcia_0_3_1.MB_TO_CHAR); (* set MB_TO_CHAR command bit *) WRITE_CMD (Ioddt_Pcmcia_0_3_1); (* send command *) i := 0; (* initialize retry counter *) Step_4 := 10; (* next step *) END_IF; 10: (* Test result of switch command *) READ_STS(Ioddt_Pcmcia_0_3_1); (* read serial port status *) IF (Ioddt_Pcmcia_0_3_1.EXCH_STS = 0) THEN (* command completed *) RESET(Ioddt_Pcmcia_0_3_1.MB_TO_CHAR); (* reset MB_TO_CHAR command bit *) IF (Ioddt_Pcmcia_0_3_1.EXCH_RPT = 0) THEN (* no error *) IF (AND(Ioddt_Pcmcia_0_3_1.PROTOCOL, 16#0F) = 03) THEN (* character mode OK *) Step_4 := 15; (* next step *) ELSE i := i + 1; IF (i > 1000) THEN Error_4 := -1; (* error *) Step_4 := 65; (* next step = end *) END_IF; END_IF; ELSE (* error in sending command to port *) Error_4 := -2; (* error *) Step_4 := 65; (* next step = end *) END_IF; END_IF; 15: (* Send dial command to modem *) Adr_4 := ADDR('0.3.1.SYS'); (* communication port *) MngtPrint_4[3] := 50; (* timeout *) MngtPrint_4[4] := 16; (* number of bytes to send *) ReqString_4 := 'ATDT0102030405$N'; (* dial message *) PRINT_CHAR(Adr_4, ReqString_4, MngtPrint_4); MngtInput_4[3] := 300; (* timeout *) MngtInput_4[4] := 0; (* number of bytes to send *) INPUT_CHAR(Adr_4, 0, 12, MngtInput_4, AnsString_4); (* wait modem reply *) Step_4 := 20; (* next step *) 20: (* Test PRINT_CHAR function result *) IF (NOT MngtPrint_4[1].1) THEN IF (MngtPrint_4[2] = 0) THEN Step_4 := 25; (* success : next step *) ELSE Error_4 := -3; (* error *) Step_4 := 65; (* next step = end *) END_IF; END_IF; 25: (* Test INPUT_CHAR function result *) IF (NOT MngtInput_4[1].1) THEN IF (MngtInput_4[2] = 0) THEN Step_4 := 30; (* success : next step *) ELSE Error_4 := -4; (* error *) Step_4 := 65; (* next step = end *) END_IF; END_IF; 30: (* Test Modem reply *) IF (AnsString_4 = 'CONNECT 9600') THEN Step_4 := 35; (* success : next step *) ELSE Error_4 := -5; (* error *) Step_4 := 65; (* next step = end *) END_IF; 35: (* Initialize OUT_IN_MBUS parameters *) MbusCmd_4[1] := 10; (* slave PLC address *) MbusCmd_4[2] := 16#06; (* Modbus function 16#06 *) MbusCmd_4[3] := 100; (* slave PLC area = %MW100 *) MbusCmd_4[4] := 41; (* quantity of data *) RetryLmt_4 := 2; (* number of retry *) DataBits_4 := %KW0.3.1.1.8; (* 1 = 8 bits -> RTU mode, 0 = 7 bits -> ASCII mode *) RespTout_4 := 300; (* timeout = 30s *) Flag_Error_4 := 0; Step_4 := 40; (* next step *) 40: (* Call OUT_IN_MBUS *) Out_In_Mbus_4 (Adr_4, MbusCmd_4, RetryLmt_4, DataBits_4, RespTout_4, Abort_4, %MW100:41, Retry_4, Active_4, Done_4, Flag_Error_4, Status_4); IF (NOT Active_4) THEN (* request completed *) IF (NOT Flag_Error_4) THEN (* no error *) Step_4 := 45; (* next step *) ELSE (* error *) Error_4 := -6; (* error *) Step_4 := 45; (* next step *) END_IF; END_IF; 45: (* Hangup modem *) MngtPrint_4[3] := 50; (* timeout *) MngtPrint_4[4] := 9; (* number of bytes to send *) ReqString_4 := '+++ATH0$N'; (* hangup message *) PRINT_CHAR(Adr_4, ReqString_4, MngtPrint_4); Step_4 := 50; (* next step *) 50: (* Test PRINT_CHAR function result *) IF (NOT MngtPrint_4[1].1) THEN IF (MngtPrint_4[2] = 0) THEN (* Success : next step *) Step_4 := 55; ELSE (* End on error *) Error_4 := 1; Step_4 := 65; END_IF; END_IF; 55: (* Send command to switch serial port from Modbus to character mode *) READ_STS(Ioddt_Pcmcia_0_3_1); (* read serial port status *) IF (Ioddt_Pcmcia_0_3_1.EXCH_STS = 0) THEN (* no active command *) Ioddt_Pcmcia_0_3_1.CONTROL := 16#00; (* reset control word *) SET(Ioddt_Pcmcia_0_3_1.CHAR_TO_MB); (* set MB_TO_CHAR command bit *) WRITE_CMD (Ioddt_Pcmcia_0_3_1); (* send command *) i := 0; (* initialize retry counter *) Step_4 := 60; (* next step *) END_IF; 60: (* Test result of switch command *) READ_STS(Ioddt_Pcmcia_0_3_1); (* read serial port status *) IF (Ioddt_Pcmcia_0_3_1.EXCH_STS = 0) THEN (* command completed *) RESET(Ioddt_Pcmcia_0_3_1.CHAR_TO_MB); (* reset CHAR_TO_MB command bit *) IF (Ioddt_Pcmcia_0_3_1.EXCH_RPT = 0) THEN (* no error *) IF (AND(Ioddt_Pcmcia_0_3_1.PROTOCOL, 16#0F) = 07) THEN (* Modbus mode OK *) Step_4 := 65; (* next step *) ELSE i := i + 1; IF (i > 1000) THEN Error_4 := 2; (* error *) Step_4 := 65; (* next step *) END_IF; END_IF; ELSE (* error in sending command to port *) Error_4 := 3; (* error *) Step_4 := 65; (* next step *) END_IF; END_IF; 65: (* End *) Start_4 := 0; (* allow new demand *) Step_4 := 0; (* goto waiting state *)END_CASE;
