---
title: "Modbus master devices"
canonical: "https://docs.cuesystem.com/space/CVCDD/448299049/Modbus%20master%20devices"
format: markdown
---
# Overview

Those devices are the main devices for connecting ModbusNodes. There are two master devices. One for TCP communication and one for serial (RS485) communication. It is connected to one TCPClient connector or serial connector of the CUEdevice. Using this device, you can directly communicate with Modbus slaves using his functions. You also can connect nodes, which represent one entity of Modbus.

# ModbusTCPMaster device properties

|  |  |  |  |  |
| --- | --- | --- | --- | --- |
| Property Name | Data Type | Allowed Values | Default Value | Description |
| IP address | Text | Any | 127.0.0.1 | IP address of Modbus device |
| Port | Long | Any | 502 | TCP port of Modbus device |
| ResponseTimeout | Time | Any | 0:0:0.50 | Timeout for sending and receiving TCP packet |
| TCPtimeout | Time | Any | 0:0:1.00 | Timeout for connecting to Modbus device |
| Pool delay | Time | Any | 0:0:1.10 | Delay between reading and writing variables values on background |

# ModbusSerialMaster device properties

|  |  |  |  |  |
| --- | --- | --- | --- | --- |
| Property Name | Data Type | Allowed Values | Default Value | Description |
| Baud rate | Long | Any | 9600 | Serial port speed |
| Parity | Enum | Even, Odd, None | Even | Serial port parity |
| StopBits | Long | 1,2 | 2 | Srial port stopbits |
| ResponseTimeout | Time | Any | 0:0:1.00 | Timeout for connecting to Modbus device |
| Pool delay | Time | Any | 0:0:1.10 | Delay between reading and writing variables values on background |

# Public functions

## ReadCoils

01 (0x01) Read Coils

This Function code is used to read from 1 to 2000 contiguous status of coils in a remote device. The Request PDU specifies the starting address, i.e. the address of the first coil specified, and the number of coils. In the PDU Coils are addressed starting at zero. Therefore coils numbered 1-16 are addressed as 0-15. The coils in the response message are packed as one coil per bit of the data field. Status is indicated as 1 = ON and 0 = OFF. The LSB of the first data Byte contains the output addressed in the query. The other coils follow toward the high order end of this Byte, and from low order to high order in subsequent bytes. If the returned output quantity is Not a multiple of eight, the remaining bits in the final data Byte will be padded with zeros (toward the high order end of the Byte). The Byte Count field specifies the quantity of complete bytes of data.

#### Request

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x01 |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
| Quantity of coils | 2 Bytes | 1 to 2000 (0x7D0) |
| Error code | 1 Byte | Function code + 0x80 |
| Exception code | 1 Byte | 01 or 02 or 03 or 04 |

#### Syntax

```visualbasic
Global Function ReadCoils(
  FromCoil As Long, 
  NumberOfCoils As Long, 
  ByRef ResultField As Array[] As Byte, 
  Optional ModbusSlaveAddress As Byte := &hff)
As Boolean
```

## WriteSingleCoil

05 (0x05) Write Single Coil

This Function code is used to write a single output to either ON Or OFF in a remote device. The requested ON/OFF state is specified by a constant in the request data field. A Value of FF 00 hex requests the output to be ON. A Value of 00 00 requests it to be OFF. All other values are illegal and will Not affect the output. The Request PDU specifies the address of the coil to be forced. Coils are addressed starting at zero.

Therefore coil numbered 1 is addressed as 0. The requested ON/OFF state is specified by a constant in the Coil Value field. A Value of 0XFF00 requests the coil to be ON. A Value of 0X0000 requests the coil to be off. All other values are illegal and will Not affect the coil. The normal response is an echo of the request, returned after the coil state has been written. 

#### Request

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x05 |
| Output Address | 2 Bytes | 0x0000 to 0xFFFF |
| Output Value | 2 Bytes | 0x0000 Or 0xFF00 |
| Error code | 1 Byte | 0x85 |
| Exception code | 1 Byte | 01 or 02 or 03 or 04 |

#### Syntax

```visualbasic
Global Function WriteSingleCoil( 
  Coil As Long, 
  NewValue As Boolean, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## WriteMultipleCoils

15 (0x0F) Write Multiple Coils

This Function code is used to force each coil in a sequence of coils to either ON Or OFF in a remote device. The Request PDU specifies the coil references to be forced. Coils are addressed starting at zero. Therefore coil numbered 1 is addressed as 0. The requested ON/OFF states are specified by contents of the request data field. A logical '1' in a bit position of the field requests the corresponding output to be ON. A logical '0' requests it to be OFF. The normal response returns the Function code, starting address, and quantity of coils forced.

#### Request PDU

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x0F |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
| Quantity of Outputs | 2 Bytes | 0x0001 to 0x07B0 |
| Byte Count | 1 Byte | N* |
| Outputs Value | N* x 1 Byte |  |
| Error code | 1 Byte | 0x8F |
| Exception code | 1 Byte | 01 or 02 or 03 or 04 |

> 📝 *N = Quantity of Outputs / 8, if the remainder is different of 0 ⇒ N = N+1

#### Syntax

```visualbasic
Global Function WriteMultipleCoils(
  FromCoil As Long, 
  NumberOfCoils As Long, 
  ByRef NewValueField As Array[] As Byte, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## ReadDiscreteInputs

02  (0x02) Read Discrete Inputs

This Function code is used to read from 1 to 2000 contiguous status of discrete inputs in a remote device. The Request PDU specifies the starting address, i.e. the address of the first input specified, and the number of inputs. In the PDU Discrete Inputs are addressed starting at zero. Therefore Discrete inputs numbered 1- 16 are addressed as 0-15. The discrete inputs in the response message are packed as one input per bit of the data field. Status is indicated as 1= ON; 0= OFF. The LSB of the first data Byte contains the input addressed in the query. The other inputs follow toward the high order end of this Byte, and from low order to high order in subsequent bytes. If the returned input quantity is Not a multiple of eight, the remaining bits in the final data Byte will be padded with zeros (toward the high order end of the Byte). The Byte Count field specifies the quantity of complete bytes of data.

#### Request

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x02 |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
| Quantity of Inputs | 2 Bytes | 1 to 2000 (0x7D0) |
| Error code | 1 Byte | 0x82 |

#### Syntax

```visualbasic
Global Function ReadDiscreteInputs(
  FromInput As Long, 
  NumberOfInputs As Long, 
  ByRef ResultField As Array[] As Byte, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## ReadInputRegisters

04 (0x04) Read Input Registers

This Function code is used to read from 1 to 125 contiguous input registers in a remote device. The Request PDU specifies the starting register address and the number of registers. In the PDU Registers are addressed starting at zero. Therefore input registers numbered 1-16 are addressed as 0-15. The register data in the response message are packed as two bytes per register, with the binary contents right justified within each Byte. For each register, the first Byte contains the high order bits and the second contains the low order bits.

#### Request

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x04 |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
| Quantity of Input Registers | 2 Bytes | 0x0001 to 0x007D |
| Error code | 1 Byte | 0x84 |
| Exception code | 1 Byte | 01 or 02 or 03 or 04 |

#### Syntax

```visualbasic
Global Function ReadInputRegisters(
  FromInput As Long, 
  NumberOfInputs As Long, 
  ByRef ResultField As Array[] As Long, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## ReadHoldingRegisters

03  (0x03) Read Holding Registers

This Function code is used to read the contents of a contiguous block of holding registers in a remote device. The Request PDU specifies the starting register address and the number of registers. In the PDU Registers are addressed starting at zero. Therefore registers numbered 1-16 are addressed as 0-15.

#### Request

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x03 |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
| Quantity of Registers | 2 Bytes | 1 to 125 (0x7D) |
| Error code | 1 Byte | 0x83 |
| Exception code | 1 Byte | 01 or 02 or 03 or 04 |

#### Syntax

```visualbasic
Global Function ReadHoldingRegisters(
  FromRegister As Long, 
  NumberOfRegisters As Long, 
  ByRef ResultField As Array[] As Long, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## WriteSingleRegister

06 (0x06) Write Single Register

This Function code is used to write a single holding register in a remote device. The Request PDU specifies the address of the register to be written. Registers are addressed starting at zero. Therefore register numbered 1 is addressed as 0. The normal response is an echo of the request, returned after the register contents have been written.

#### Request

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x06 |
| Register Address | 2 Bytes | 0x0000 to 0xFFFF |
| Register Value | 2 Bytes | 0x0000 to 0xFFFF |
| Error code | 1 Byte | 0x86 |
| Exception code | 1 Byte | 01 or 02 or 03 or 04 |

#### Syntax

```visualbasic
Global Function WriteSingleRegister(
  Register As Long, 
  NewValue As Long, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## WriteMultipleRegisters

16 (0x10) Write Multiple registers

This Function code is used to write a block of contiguous registers (1 to 123 registers) in aremote device. The requested written values are specified in the request data field. Data is packed as two bytes per register. The normal response returns the Function code, starting address, and quantity of registers written. 

#### Request

|  |  |  |
| --- | --- | --- |
| Function code | 1 Byte | 0x10 |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
| Quantity of Registers | 2 Bytes | 0x0001 to 0x007B |
| Byte Count | 1 Byte | 2 x N* |
| Registers Value | N* x 2 Bytes | Value |
| Exception code | 1 Byte | 01 or 02 or 03 or 04 |

> 📝 *N = Quantity of Registers Error code 1 Byte 0x90

#### Syntax

```visualbasic
Global Function WriteMultipleRegisters(
  FromRegister As Long, 
  NumberOfRegisters As Long, 
  ByRef NewValueField As Array[] As Long, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## SendPDUandReceiveResponse

GENERAL PDU

Send user defined PDU and receive response. This Function sends any user defined PDU. The slave device responce is stored in Response ByteString it consists of Slave address + Responsed PDU.

#### Syntax

```visualbasic
Global Function SendPDUandReceiveResponse(
  PDU As ByteString, 
  ByRef Response As ByteString, 
  Optional Timeout As Time :=:0.1, 
  Optional ModbusSlaveAddress As Byte := &hff) 
As Boolean
```

## LastError

Returns last error code.

#### Syntax

```visualbasic
Global Function LastError() As Long
```

#### Return value

Global Const MODBUSERROR_NO_RESPONSE                  As Long := 110500   
Global Const MODBUSERROR_CRC_LENGTH                     As Long := 110501   
Global Const MODBUSERROR_WRONG_RESPONSE          As Long := 110502   
Global Const MODBUSERROR_NEGATIVE_RESPONSE       As Long := 110000 + MODBUS Exception code

## MODBUS Exception Codes (basic)

|  |  |  |
| --- | --- | --- |
| 01 | ILLEGAL Function | The Function code received in the query is Not an allowable action For the server (Or slave). |
| 02 | ILLEGAL DATA ADDRESS | The data address received in the query is Not an allowable address For the server(Or slave). |
| 03 | ILLEGAL DATA Value | A Value contained in the query data field is Not an allowable Value For server (Or slave). |
| 04 | SLAVE DEVICE FAILURE | An unrecoverable error occurred in the server(Or slave). |
| 05 | ACKNOWLEDGE | Specialized use in conjunction with programming commands. |
| 06 | SLAVE DEVICE BUSY | Specialized use in conjunction with programming commands. |
| 08 | MEMORY PARITY ERROR | Specialized use in conjunction with Function codes. |

## LastResponse

Returns full slave device responce (is stored in Response ByteString) it consists both of Slave address + Responsed PDU.

#### Syntax

```visualbasic
Global Function LastResponse(ByRef Response As ByteString)
```