FB_MBReadWriteRegs (Modbus-Funktion 23)

This function first reads 1 to 128 output registers (16 bit) and then writes 1 to 128 output registers (16 bit).
Inputs
VAR_INPUT
sIPAddr : STRING(15);
nTCPPort : UINT:= MODBUS_TCP_PORT;
nUnitID : BYTE:=16#FF;
nReadQuantity : WORD;
nMBReadAddr : WORD;
nWriteQuantity: WORD;
nMBWriteAddr : WORD;
cbDestLength : UDINT;
pDestAddr : POINTER OF BYTE;
cbSrcLength : UDINT;
pSrcAddr : POINTER OF BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
Name | Type | Description |
---|---|---|
sIPAddr |
STRING (15) |
Is a string containing the IP address of the target device. |
nTCPPort |
UINT |
Port number of the target device. |
nUnitID |
BYTE |
Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP, this value must be 16#FF. |
nReadQuantity |
WORD |
Number of output registers (data words) to be read. The value zero is not permitted. |
nMBReadAddr |
WORD |
Start address of the output registers to be read (word offset). |
nWriteQuantity |
WORD |
Number of output registers (data words) to be written. The value zero is not permitted. |
nMBWriteAddr |
WORD |
Start address of the output registers to be written (word offset). |
cbDestLength |
UDINT |
Contains the maximum byte size available in the destination buffer for the register values to be read. The buffer must have at least the byte size: nReadQuantity * 2. |
pDestAddr |
POINTER OF BYTE |
Contains the address of the destination buffer into which the data are to be read. The buffer can be a single variable, an array or a structure, whose address can be determined with the ADR operator. |
cbSrcLength | UDINT | Contains the maximum available byte size of the source buffer that contains the register values to be written. The buffer must have at least the byte size: nQuantity * 2. |
pSrcAddr | POINTER OF BYTE | Contains the address of the source buffer that contains the data to be written. The buffer can be a single variable, an array or a structure, whose address can be determined with the ADR operator. |
bExecute | BOOL | The function block is enabled by a rising edge at this input. |
tTimeout | TIME | States the length of the timeout that may not be exceeded by execution of the ADS command. |
Outputs
VAR_OUTPUT
bBUSY : BOOL;
bError : BOOL;
nErrId : UDINT;
cbRead : UDINT;
END_VAR
Name | Type | Description |
---|---|---|
bBusy | BOOL | When the function block is enabled, this output is set and remains set until a feedback is received. |
bError | BOOL | If an ADS error should occur during the transfer of the command, then this output is set once the bBusy output is reset. |
nErrId | UDINT | Returns the ADS error number when the bError output is set. |
cbRead | UDINT | Contains the number of bytes currently read. |
Sample of calling the function block in FBD
PROGRAM Test
VAR
fbReadWriteRegs : FB_MBReadWriteRegs;
bReadWriteRegs : BOOL;
bReadWriteRegsBusy : BOOL;
bReadWriteRegsError : BOOL;
nReadWriteRegsErrorId : UDINT;
nReadWriteRegsCount : UDINT;
nRdQuantity : WORD;
nRdMBAddr : WORD;
nWrQuantity : WORD;
nWrMBAddr : WORD;
arrRdData : ARRAY [1..9] OF WORD;
arrWrData : ARRAY [1..9] OF WORD;
END_VAR

After a rising edge of "bExecute" and successful execution of the ReadWriteRegs command, arrRdData contains the read register data, and the data from arrWrData are written to the registers.
Requirements
Development environment |
Target system type |
PLC libraries to be linked |
---|---|---|
TwinCAT v3.0.0 |
PC or CX (x86) |
Tc2_ModbusSrv |