FB_MBReadInputs (Modbus function 2)

FB_MBReadInputs (Modbus function 2) 1:

This function is used for reading 1 to 2048 digital inputs. One digital input corresponds to one bit of the read data bytes.

VAR_INPUT

VAR_INPUT
    sIPAddr         : STRING(15);
    nTCPPort        : UINT:= MODBUS_TCP_PORT;
    nUnitID         : BYTE:=16#FF;
    nQuantity       : WORD;
    nMBAddr         : WORD;
    cbLength        : UDINT;
    pDestAddr       : UDINT;
    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.

nQuantity: Number of digital inputs (data bits) to be read. The value of nQuantity must be > 0.

nMBAddr: Start address of the digital inputs to be read (bit offset).

cbLength: Contains the max. byte size of the destination buffer. The minimum buffer byte size must be: (nQuantity + 7) / 8.

pDestAddr: 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 found with the ADR operator.

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;
    cbRead      : 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.

cbRead: Contains the number of bytes currently read.

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
    fbReadInputs        : FB_MBReadInputs;
    bReadInputs         : BOOL;
    bReadInputsBusy     : BOOL;
    bReadInputsError    : BOOL;
    nReadInputsErrorId  : UDINT;
    nReadInputsCount    : UDINT;
    nQuantity           : WORD := 20;
    nMBAddr             : WORD := 29;
    arrData             : ARRAY [1..3] OF BYTE;
END_VAR

FB_MBReadInputs (Modbus function 2) 2:

After a rising edge of "bExecute" and successful execution of the ReadInputs command, the content of digital inputs 30 - 49 is written into the arrData array:

Digital outputs

Array offset

Status

29-36

1

0x34

The status of inputs 36 is the MSB of this byte (left)
The status of inputs 29 is the LSB of this byte (right)

37-44

2

0x56

The status of inputs 44 is the MSB of this byte (left)
The status of inputs 37 is the LSB of this byte (right)

45-49

3

0x07

Since only 20 outputs are to be read, the remaining bits (5-8) are set to 0.

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v2.8.0

PC (i386)

TcModbusSrv.Lib