FB_MBWriteSingleCoil (Modbus function 5)
This function is used for writing a single digital output (coil). Bit access is used.
VAR_INPUT
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nMBAddr : WORD;
nValue : WORD;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
sIPAddr: Is a string containing the IP address of the target device.
nTCPPort: Port number of the target device.
nUnitID: Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP, this value must be 16#FF.
nMBAddr: Address of the digital output (bit offset).
nValue: Value to be written into the digital output. The value 16#FF00 switches the output on, 16#0000 switches it off.
bExecute: The function block is activated by a rising edge at this input.
tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.
VAR_OUTPUT
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is received.
bError : If an ADS error should occur during the transfer of the command, then this output is set once the bBusy output is reset.
nErrId : Supplies the ADS error number when the bError output is set.
Function specific ADS error code | Possible reason |
---|---|
0x8001 | Modbus function not implemented |
0x8002 | Invalid address or length |
0x8003 | Invalid parameters: - wrong number of registers |
0x8004 | Modbus server error |
Example of calling the block in FBD:
PROGRAM Test
VAR
fbWriteSingleCoil : FB_MBWriteSingleCoil;
bWriteCoil : BOOL;
bWriteCoilBusy : BOOL;
bWriteCoilError : BOOL;
nWriteCoilErrorId : UDINT;
nMBAddr : WORD := 3;
nValue : WORD := 16#FF00;
END_VAR
After a rising edge of "bExecute" and successful execution of the WriteSingleCoil command, digital output 4 is switched on.
Requirements
Development environment |
Target system type |
PLC libraries to be linked |
---|---|---|
TwinCAT v3.0.0 |
PC or CX (x86) |
Tc2_ModbusSrv |