Modbus Shunya API
#
Modbus Communication ProtocolModbus 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 ShunyaConfig | 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 parametersFor example:
A typical Modbus configuration will look like this
#
Modbus API'sTo get the data from the device over modbus you can use these API's present in the Shunya Interfaces
API | Description | Details |
---|---|---|
newModbusClient() | Creates a new Modbus Instance | Read More |
modbusConnect() | Connect to device | Read More |
modbusRead() | Read from PLC address | Read More |
modbusWrite() | Write to PLC address | Read More |
modbusDisconnect() | Disconnect to device connected via | Read More |
newModbusClient()
#
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 /etc/shunya/config.json
JSON file looks like
So the usage of the API's will be
modbusConnect()
#
Description : Connect to the device
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.
Return-type : int
Usage :
modbusRead()
#
Description : Read from PLC address
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.func
(uint8_t) - Function code (supports all read function codes)addr
(uint16_t) - coil address to read the value from
Return-type : int
Usage :
modbusWrite()
#
Description : Write to PLC address connected.
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.func
(uint8_t) - Function code (supports all read function codes)addr
(uint16_t) - coil address to write the valueval
(uint16_t) - value to write
Return-type : int
Usage :
modbusDisconnect()
#
Description : Disconnect to the device.
Parameters
*obj
(modbusObj) - Pointer to the Modbus Instance.
Return-type : int
Usage :