ModbusRtuMasterV2_PcCOM
The function block ModbusRtuMasterV2_PcCOM
implements a Modbus master that communicates via a serial PC interface (COM port). Additional function blocks are available for communication via a serial bus terminal.
![]() | Hardware connection The data structures required for the link with the communication port are included in the function block. They are displayed in the TwinCAT System Manager once the PLC program has been integrated and can be connected with a COM port. The procedure is analogous to the description in the chapter Serial PC Interface of the TF6340 TC3 Serial Communication documentation. |
The function block is not called in its basic form, but individual actions of that block are used within a PLC program. Each Modbus function is implemented as an action.
Supported Modbus functions (actions)
- ModbusMaster.ReadCoils
Modbus function 1 = Read Coils - Reads binary outputs (coils) from a connected slave. The data is stored in compressed form (8 bits per byte) from the specified address
pMemoryAddr
. - ModbusMaster.ReadInputStatus
Modbus function 2 = Read Input Status - Reads binary inputs from a connected slave. The data is stored in compressed form (8 bits per byte) from the specified address
pMemoryAddr
. - ModbusMaster.ReadRegs
Modbus function 3 = Read Holding Registers - Reads data from a connected slave.
- ModbusMaster.ReadInputRegs
Modbus function 4 = Read Input Registers - Reads input registers from a connected slave.
- ModbusMaster.WriteSingleCoil
Modbus function 5 = Write Single Coil - Sends a binary output (coil) to a connected slave. The data must be ready to send in compressed form (8 bits per byte) from the specified address
pMemoryAddr
. - ModbusMaster.WriteSingleRegister
Modbus function 6 = Write Single Register - Sends a single data word to a connected slave
- ModbusMaster.WriteMultipleCoils
Modbus function 15 = Write Multiple Coils - Sends binary outputs (coils) to a connected slave. The data must be ready to send in compressed form (8 bits per byte) from the specified address
pMemoryAddr
. - ModbusMaster.WriteRegs
Modbus function 16 = Preset Multiple Registers - Sends data to a connected slave
- ModbusMaster.Diagnostics
Modbus function 8 = Diagnostics - Sends a diagnostic request to the slave with a user-specified function code (subfunction code). Since this function does not address a memory, the function code is transferred in the data word
MBAddr
. Any data required for the function is included inpMemoryAddr
.
Supported Modbus functions (actions) of the MasterV2 function blocks
- ModbusMaster.ReadWriteRegs
Modbus function 23 = Read/Write Multiple Registers - Sends the data specified via the Aux parameters to a connected slave and receives data from the slave at the same time. The received data is stored at the address specified by pMemoryAddr.
- ModbusMaster.UserReadWrite
Universal user telegram - The Modbus function code is specified by the user in the first byte of specified data (pMemoryAddr). With this function the user is able to send a Modbus telegram with any function code. Any data received from the slave is stored at the address specified by pAuxMemoryAddr.
Inputs
VAR_INPUT
UnitID : BYTE;
Quantity : WORD;
MBAddr : WORD;
cbLength : UINT;
pMemoryAddr : POINTER TO BYTE;
AuxQuantity : WORD;
AuxMBAddr : WORD;
AuxcbLength : UINT;
pAuxMemoryAddr : POINTER TO BYTE;
Execute : BOOL;
Timeout : TIME;
END_VAR
Name | Type | Description |
---|---|---|
UnitID | UINT | Modbus station address (1..247). The Modbus slave will only answer if it receives telegrams containing its own station address. Optionally, collective addresses can be used for replying to any requests. Address 0 is reserved for broadcast telegrams and is therefore not a valid station address (see UnitID) |
Quantity | WORD | Number of data words to be read or written for word-oriented Modbus functions. For bit-oriented Modbus functions, Quantity specifies the number of bits (inputs or coils). |
MBAddr | WORD | Modbus data address, from which the data are read from the end device (slave). This address is transferred unchanged to the slave and is interpreted there as data address. |
cbLength | UINT | Size of the data variable used for send or read actions in bytes. cbLength must be greater than or equal to the amount of data transferred as determined by Quantity. For word accesses, for example: [cbLength >= Quantity * 2]. cbLength can be calculated with SIZEOF(Modbus data). |
pMemoryAddr | BYTE | Memory address in the PLC, calculated with ADR (Modbus data). For read actions, the read data are stored in the addressed variable. For send actions, the data are transferred from the addressed variable to the end device. |
AuxQuantity | WORD | Additional parameter which is only used for read/write functions, see ReadWriteRegs/UserReadWrite |
AuxMBAddr | WORD | Additional parameter which is only used for read/write functions, see ReadWriteRegs/UserReadWrite. |
AuxcbLength | UINT | Additional parameter which is only used for read/write functions, see ReadWriteRegs/UserReadWrite |
pAuxMemoryAddr | BYTE | Additional parameter which is only used for read/write functions, see ReadWriteRegs/UserReadWrite |
Execute | BOOL | Start signal. The action is triggered by a rising edge at the Execute input. |
Timeout | TIME | Timeout value for waiting for a response from the addressed slave. |
Outputs
VAR_OUTPUT
BUSY : BOOL;
Error : BOOL;
ErrorId : MODBUS_ERRORS;
cbRead : UINT;
END_VAR
Name | Type | Description |
---|---|---|
Busy | BOOL | Indicates that the function block is active. Busy becomes TRUE with a rising edge at Execute and becomes FALSE again once the started action is completed. At any one time, only one action can be active. |
Error | BOOL | Indicates that an error occurred during execution of an action. |
ErrorId | MODBUS_ERRORS | Indicates an error number in the event of disturbed or faulty communication. |
cbRead | UINT | Provides the number of read data bytes for a read action. |
Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT v3.1.0 | PC or CX (x86, x64, ARM) | Tc2_ModbusRTU |