bg

How to Log MODBUS RTU to MySQL Database

A complete guide to polling MODBUS RTU serial devices and exporting the register data directly into a MySQL database.

1. Protocol Configuration: MODBUS RTU

Communication Type: Active Polling (Master/Slave over RS485/COM)

Because MODBUS transmits data in binary, the parser must extract specific Holding Registers (e.g., 40001) or Input Registers. If your sensor uses 32-bit floating-point numbers, configure the parser to read two adjacent 16-bit registers and apply the correct byte-swapping (endianness) to reconstruct the float.

Apply this base configuration for the serial connection:

{
  "port": "COM3",
  "baud_rate": 9600,
  "data_bits": 8,
  "stop_bits": 1,
  "parity": "none",
  "protocol": "modbus_rtu_serial",
  "poll_interval_ms": 1000
}
MODBUS RTU plugin selection to query and parse data.

MODBUS RTU plugin selection to query and parse data.

MODBUS RTU query queue: custom configuration to read different registers.

MODBUS RTU query queue: custom configuration to read different registers.

Ready to connect MODBUS RTU to MySQL?

2. Database Setup: MySQL

You will need the MySQL Connector/ODBC installed on the machine running the Data Logger. Create a System DSN pointing to your MySQL server instance.

Use the following SQL script to create your target table. We recommend using the InnoDB engine to handle high-frequency concurrent inserts without table locking.

CREATE TABLE sensor_data (
  id INT AUTO_INCREMENT PRIMARY KEY,
  log_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
  device_id VARCHAR(50),
  register_value FLOAT
) ENGINE=InnoDB;
MySQL plugin to export data.

MySQL plugin to export data.

MySQL database connection settings.

MySQL database connection settings.

INSERT SQL statements in a queue with custom parameters.

INSERT SQL statements in a queue with custom parameters.

3. Protocol-Specific Troubleshooting

  • Timeout or CRC errors: Unlike TCP, serial MODBUS RTU relies on strict timing. Verify that your RS485 A/B wires are not inverted, and that the baud rate and device ID in the software exactly matches the slave device.
  • The device returns the "Illegal address" error code: Try to use the function #3 instead of the function #4 in your MODBUS request. Otherwise, verify that you are using the correct registe address. Try to specify an absolute offset (not logical address like 40001).
  • I have several values that I want to export in one data row. Now, the logger writes one value per row: You need to enable the "Export all data at once" option in the MODBUS plugin settings.

4. Database-Specific Troubleshooting

  • MySQL server has gone away: If your serial device is idle overnight, MySQL will drop the connection. Enable the 'Keep connection alive' setting in the Data Logger's ODBC export module.
  • How can I insert or update a data row for my device?: You can use any SQL query in our 'SQL Database Pro' plugin. In MySQL, you can easily execute the following SQL: 'INSERT INTO ....... ON DUBLICATE KEY .... UPDATE ....'.
  • I want to write data from each of my devices to a different table. How can I do it?: In the 'SQL Database Pro' plugin, you can use a placeholder in your SQL. For example, 'INSERT INTO SerialData%DEVICE_ID% (....)'. Before executing SQL, the logger replaces %DEVICE_ID% with the corresponding value from parsed data, and the real destination table will vary (SerialData1, SerialData2, etc.).

Ready to connect MODBUS RTU to MySQL?

Product Buy Download Support Contacts Site map
Copyright © 1999 - 2026, AGG Software.
All rights reserved.. Terms of use | Privacy policy