Modbus Configuration files for ESP8266/Arduino

At development of devices on the basis of microcontrollers ESP8266/ESP32/Arduino working through RS-485 on protocol Modbus important moments is configuration of Modbus operations.

There is no Sense to "reinvent the wheel" in the configuration of the format. One of the projects used The JSON Description of the Modbus configuration for Microsoft IoT Edge. Actually, it makes sense to use this configuration format because it closes most questions well.

The Original configuration file is slightly modified because the JSON file on the microcontroller is more difficult to work with because of memory limitations.

  [
  {
    "Slave": {
      "Connection": "ttyS0",
      "RxPin": "15",
      "TxPin": "13",
      "PollingInterval": "2000",
      "RetryCount": "10",
      "RetryInterval": "100",
      "HwId": "TermoSensor-0a:01:01:01:01:02",
      "BaudRate": "9600",
      "Config": "SERIAL_8N1",
      "Ops": [
        {
          "PollingInterval": "2000",
          "UnitId": "1",
          "Function": "0x04",
          "Address": "0x01",
          "Len": "1",
          "DisplayName": "Temp"
        },
        {
          "PollingInterval": "2000",
          "UnitId": "1",
          "Function": "0x04",
          "Address": "0x02",
          "Len": "1",
          "DisplayName": "Humidity"
        }          
      ]
    }
  },
  {
    "Slave": {
      "Connection": "192.168.1.2",
      "TcpPort": "502",
      "PollingInterval": "2000",
      "HwId": "TermoSensor-0a: 01:01:01:01:02",
      "Ops": [
        {
          "PollingInterval": "2000",
          "UnitId": "1",
          "Function": "0x04",
          "Address": "0x01",
          "Len": "1",
          "DisplayName": "Temp"
        }  
      ]
    }
  }
]

Field expansion:

  • "PublishInterval"-Interval between each push to Cloud Server in Millisecond.
  • "Slave"-Contains one or more Modbus slaves ' configuration.
    • "SlaveConnection"-Ipv4 address or the serial port name of the Modbus slave.
    • "RxPin"-Rx pin (e.g. for SoftwareSerial initialization).
    • "TxPin"-Rx pin (e.g. for SoftwareSerial initialization).
    • "RetryCount"-Max retry attempt for reading data, default to 10.
    • "RetryInterval"-Retry interval between each Retry attempt, default to 50 milliseconds
    • "HwId"-Unique Id for each Modbus slave (User Defined).
    • "BaudRate"-Serial port communication parameter. (Valid values:… 9600, 14400.19200…)
    • "Config"-Serial port configuration. Default SERIAL_8N1. Possible values see here.
    • "Ops"-Contains one or more Modbus read requests.
      • "PollingInterval": Interval between each read request in Millisecond.
      • "UnitId"-The unit ID (SlaveID) to be read.
      • "Function"-the number of the function (register) to read the data. Instead of "StartAddress" in the original JSON version.
      • "Address"-the offset of the address in the selected register.
      • "Len"-Number of registers/bits to be read. Instead of "Count" in the original version.
      • "DisplayName"-Operation name.
      • "CorrelationId"-The Operations with the same ID will be grouped together in their output message. (for future releases).

Library for the implementation of the configuration file for Modbus-ArduinoJSON. The code described earlier is used To read the Config from the viewpoint of working with the file system. The Generated JSON file can be uploaded to the ESP8266/ESP32 file system either from the cloud server or through the Arduino IDE.

When working with the file download utility in SPIFFS through the Arduino IDE, you should consider that

  • The utility adds the ". txt" extension to the file. Accordingly, if the file rs485cfg is placed in the/data folder, SPIFFS will already have the rs485cfg. txt file.
  • At each download, the utility deletes ALL previously created files. If you have created some files programmatically, they will also be jammed.

Possible Modbus "Function" values:

Function CodeDescription Value Type Type of Access
01 (0x01)Read Coil StatusDiscreteReading
02 (0x02)Read Input StatusDiscreteReading
03 (0x03)Read Holding Registers16 bitReading
04 (0x04)Read Input Registers16 bitReading
05 (0x05)Write Single CoilDiscreteRecord
06 (0x06)Write Single Register16 bitRecord
15 (0x0F)Write Multiple CoilsDiscreteRecord
16 (0x10)Read/Write Multiple Registers16 bitRead/Write

But at the moment I'm only interested in reading. The "Address" Field shifts the address in the selected register.

The Test configuration file for the temperature sensor and humidity is as follows.

  [
  {
    "Slave": {
      "Connection": "tty0",
      "PollingInterval": "4000",
      "RetryCount": "20",
      "RetryInterval": "100",
      "HwId": "TermoSensor-0a:01:01:01:01:02",
      "RxPin": "15",
      "TxPin": "13",
      "BaudRate": "9600",
      "Config": "SERIAL_8N1",
      "Ops": [
        {
          "PollingInterval": "2000",
          "UnitId": "1",
          "Function": "0x04",
          "Address": "0x01",
          "Len": "1",
          "DisplayName": "Temp"
        },
        {
          "PollingInterval": "2000",
          "UnitId": "1",
          "Function": "0x04",
          "Address": "0x02",
          "Len": "1",
          "DisplayName": "Humidity"
        }          
      ]
    }
  }
]

Laid Out the ModbusConfig library, written to work with the Modbus configuration file, on Github.

Spread the love
This entry was posted in IT Recipies, Без рубрики. Bookmark the permalink.

Leave a Reply