Last Code Revision Date: 02-Sep-2025
This project logs Modbus TCP/IP device data into a MySQL database for historical storage and analysis. It uses pymodbus
for Modbus communication and mysql-connector-python
for MySQL operations.
MODBUS_SLAVE_ID
in config)src/config.py
modbus-mysql-datalogger-01092025
├── src
│ ├── main.py # Main application logic
│ ├── modbus_client.py # Modbus TCP client wrapper
│ ├── mysql_logger.py # MySQL logger for Modbus data
│ └── config.py # User configuration for Modbus/database/interval
├── requirements.txt # Python dependencies
├── README.md # Project documentation
Package | Version | Description |
---|---|---|
pymodbus | 3.11.1 | Modbus protocol implementation |
mysql-connector-python | 9.4.0 | MySQL database for Python |
git clone <repository-url>
cd modbus-mysql-datalogger-01092025
pip install -r requirements.txt
Edit src/config.py
to set:
MODBUS_SLAVE_ID
) to target a specific deviceExample (src/config.py
):
MYSQL_DB_CONFIG = {
'host': '127.0.0.1',
'user': 'root',
'password': 'toor',
'database': 'test'
}
MODBUS_SERVER = '127.0.0.1'
MODBUS_PORT = 502
MODBUS_SLAVE_ID = 1 # Modbus slave ID to read from
MODBUS_READ_CONFIG = {
"holding_registers": {
"addresses": [0, 1, 2, 5, 6, 7]
}
}
UPDATE_INTERVAL = 5 # seconds
To enable other data types (coils, discrete inputs, input registers), uncomment and configure them in MODBUS_READ_CONFIG
.
Start the datalogger:
python src/main.py
modbus_data
table in the test
database.MODBUS_READ_CONFIG
as needed).34.12$