OPCUA Component
Modbus Communication Protocol
Modbus is a communication protocol for use with programmable logic controllers (PLC). It is typically used to transmit signals from instrumentation and control devices back to a main controller, or data gathering system.
The method is used for transmitting information between electronic devices. The device requesting information is called “master” and “slaves” are the devices supplying information.
In a standard Modbus network, there is one master and up to 247 slaves, each with a unique slave address from 1 to 247.
Communication between a master and a slave occurs in a frame that indicates a function code.
The function code identifies the action to perform, such as read a discrete input; read a first-in, first-out queue; or perform a diagnostic function.
The slave then responds, based on the function code received.
The protocol is commonly used in IoT as a local interface to manage devices.
Modbus protocol can be used over 2 interfaces
- RS485 - called as Modbus RTU
- Ethernet - called as Modbus TCP/IP
What is RS485?
- RS485 is a serial (like UART) transmission standard, you can put several RS485 devices on the same bus.
- RS485 is not directly compatible: you must use the correct type of interface, or the signals won't go through. Mainly done through an easy to use an RS485 to USB.
Modbus configuration in Shunya
Config | Description |
---|---|
Protocol Type | Modbus is available in 2 variants RTU and TCP. Select the mode that you want to communicate |
IP address | Modbus server IP address (only for Modbus TCP type) |
Port | Modbus server Port (only for Modbus TCP type) |
Device | Modbus server Device node (only for Modbus RTU type) |
Baud Rate | Modbus server Baud rate (only for Modbus RTU type) |
The configurations need to be set manually by the user before using the API's
For example:
A sample JSON snippet should contain
Commonly configured parameters
For example:
A typical Modbus configuration will look like this
Modbus API's
To get the data from the device over modbus you can use these API's present in the Shunya Interfaces
API | Description | Details |
---|---|---|
newModbus() | Creates a new Modbus Instance | Read More |
modbusRtuConnect() | Connect to device connected via Serial | Read More |
modbusRtuRead() | Read from PLC address connected via Serial | Read More |
modbusRtuWrite() | Write to PLC address connected via Serial | Read More |
modbusRtuDisconnect() | Disconnect to device connected via Serial | Read More |
modbusTcpConnect() | Connect to device connected via TCP | Read More |
modbusTcpRead() | Read from PLC address connected via TCP(Eth/wifi) | Read More |
modbusTcpWrite() | Write to PLC address connected via TCP(Eth/Wifi) | Read More |
modbusTcpDisconnect() | Disconnect to device connected via TCP | Read More |
newModbus()
Description : Creates a new Modbus Instance
Parameters
name (char *)
- Name of the JSON object.
Return-type : modbusObj
Returns : modbusObj Instance for Modbus
Usage :
For example: Lets say your JSON file looks like
So the usage of the API's will be
modbusRtuConnect()
Description : Connect to the device Connected via Serial
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.
Return-type : int
Usage :
modbusRtuRead()
Description : Read from PLC address connected via Serial
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.addr
(uint16_t) - coil address to read the value from
Return-type : int
Usage :
modbusRtuWrite()
Description : Write to PLC address connected via Serial
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.addr
(uint16_t) - coil address to write the valueval
(uint16_t) - value to write
Return-type : int
Usage :
modbusRtuDisconnect()
Description : Disconnect to the device Connected via Serial
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.
Return-type : int
Usage :
modbusTcpConnect()
Description : Connect to the device Connected via Ethernet
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.
Return-type : int
Usage :
modbusTcpRead()
Description : Read from PLC address connected via TCP(Eth/wifi)
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.addr
(uint16_t) - coil address to read the value from
Return-type : int
Usage :
modbusTcpWrite()
Description : Read from PLC address connected via TCP(Eth/wifi)
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.addr
(uint16_t) - coil address to read the value fromval
(uint16_t) - value to write
Return-type : int
Usage :
modbusTcpDisconnect()
Description : Disconnect to the device Connected via Ehternet
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.
Return-type : int
Usage :