Modbus Shunya API

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

    1. RS485 - called as Modbus RTU
    2. 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#

ConfigDescription
Protocol TypeModbus is available in 2 variants RTU and TCP. Select the mode that you want to communicate
IP addressModbus server IP address (only for Modbus TCP type)
PortModbus server Port (only for Modbus TCP type)
DeviceModbus server Device node (only for Modbus RTU type)
Baud RateModbus 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

"modbus": {
"modbusType": "rtu",
"modbusServerIp": "modbus-TCP-server-IP-address",
"modbusServerPort": "modbus-TCP-server-port",
"modbusSerialDevice": "modbus-RTU-serial-device",
"modbusSerialBaudrate": 9600
},

Commonly configured parameters#

For example:

A typical Modbus configuration will look like this

"modbus-rtu": {
"modbusType": "rtu",
"modbusSerialDevice": "/dev/ttyS1",
"modbusSerialBaudrate": 9600,
},
"modbus-tcp": {
"modbusType": "tcp",
"modbusServerIp": "192.168.0.101",
"modbusServerPort": "5059",
},

Modbus API's#

To get the data from the device over modbus you can use these API's present in the Shunya Interfaces

APIDescriptionDetails
newModbusClient()Creates a new Modbus InstanceRead More
modbusConnect()Connect to deviceRead More
modbusRead()Read from PLC addressRead More
modbusWrite()Write to PLC addressRead More
modbusDisconnect()Disconnect to device connected viaRead 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

"modbus-tcp": {
"modbusType": "tcp",
"modbusServerIp": "192.168.0.101",
"modbusServerPort": "5059",
},

So the usage of the API's will be

modbusObj plc1 = newModbusClient("modbus-tcp");

modbusConnect()#

Description : Connect to the device

Parameters

  • *obj(modbusObj) - Pointer to the Modbus Instance.

Return-type : int

Usage :

/* Create new Modbus Instace */
modbusObj plc1 = newModbusClient("modbus");
/* Connect to the device*/
modbusConnect(&plc1);

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 :

/* Create new Modbus Instace */
modbusObj plc1 = newModbusClient("modbus");
/* Connect to the device*/
modbusConnect(&plc1);
/* Read the 1000 modbus address*/
modbusRead(&plc1, 1000);

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 value
  • val(uint16_t) - value to write

Return-type : int

Usage :

/* Create new Modbus Instace */
modbusObj plc1 = newModbusClient("modbus");
/* Connect to the device*/
modbusConnect(&plc1);
/* Write value 8883 at the 1000 modbus address*/
modbusWrite(&plc1, 1000, 8883);

modbusDisconnect()#

Description : Disconnect to the device.

Parameters

  • *obj(modbusObj) - Pointer to the Modbus Instance.

Return-type : int

Usage :

/* Disconnect to the device*/
modbusDisconnect(&plc1);