Real-time PLC data acquisition using Python and Modbus TCP. Reads live signals from a CODESYS SoftPLC connected to a Factory I/O 3D industrial simulation and logs them to CSV for analysis.
This project bridges industrial PLC automation with data engineering. A Python script connects to a CODESYS SoftPLC via Modbus TCP, reads two process variables every second, prints them to console, and logs them to a CSV file β replicating how real SCADA systems acquire data from field devices.
Key achievement: Full Industry 4.0 data acquisition stack running entirely in software.
βββββββββββββββββββ Modbus TCP ββββββββββββββββββββ 3D Simulation
β CODESYS V3.5 β βββββββββββββββββββΊ β Factory I/O β βββββββββββββββββββΊ
β SoftPLC SP17 β 127.0.0.1:502 β Ultimate v2.5 β Visual Feedback
β Ladder Logic β β Scene: A to B β
ββββββββββ¬βββββββββ ββββββββββββββββββββ
β Modbus TCP
β 127.0.0.1:502
ββββββββββΌβββββββββ
β Python Script β βββββββββββββββββββΊ plc_data_log.csv
β pymodbus 3.x β logs every 1s
βββββββββββββββββββ
| Variable | Type | Modbus Register | Direction |
|---|---|---|---|
| Sensor_Object | BOOL | Coil 0 | Factory I/O β PLC β Python |
| Motor_Running | BOOL | Discrete Input 0 | PLC β Factory I/O + Python |
pip install pymodbus
client = ModbusTcpClient(host='127.0.0.1', port=502)
Every second the script:
- Reads Coil 0 β
Sensor_Object(object detected by physical sensor) - Reads Discrete Input 0 β
Motor_Running(conveyor belt state) - Prints timestamped values to console
- Appends a row to
plc_data_log.csv
Press Ctrl+C to stop β the connection closes cleanly.
Timestamp,Sensor_Object,Motor_Running
19:49:56,False,True
19:49:57,False,True
19:50:13,True,False β sensor detects box, motor stops
19:50:14,True,False
19:50:17,False,True β box clears sensor, motor restarts
The CSV captures every state change with millisecond-accurate timestamps β the same data format used in industrial historians and SCADA systems.
| Tool | Version | Purpose |
|---|---|---|
| CODESYS Development System | V3.5 SP17 | PLC programming (Ladder) |
| CODESYS Control Win V3 x64 | 3.5.17.0 | SoftPLC runtime |
| CODESYS Modbus | 4.0.0.0 | Modbus TCP Slave Device |
| Factory I/O | Ultimate v2.5.10 | 3D industrial simulation |
| Python | 3.14.5 | Data acquisition script |
| pymodbus | 3.13.0 | Modbus TCP client library |
- Start CODESYS Control Win V3 x64 from Windows Start Menu β Start PLC
- Open CODESYS project β Login β Run (F5)
- Open Factory I/O β Scene "1 - From A to B" β Connect Modbus TCP Client β Play
- Run the Python script:
python monitor.py
- Watch real-time data in console. Stop with Ctrl+C.
- Open
plc_data_log.csvin Excel to analyze the logged data.
Full stack running β CODESYS + Factory I/O + Python console Stack Running
CSV data logged to Excel CSV Data
Make sure CODESYS Control Win is running and the PLC is in RUN mode before starting the script. Check Windows Firewall is not blocking port 502.
Right-click Device in project tree β Update device β Select CODESYS Control Win V3 x64 v3.5.17.0.
This project is Part 6 of an Industry 4.0 Engineer learning path:
- β Projects 1β4 β PLC fundamentals (TIA Portal, S7-1500)
- β Project 5 β Conveyor Belt Sensor Control (CODESYS + Modbus TCP + Factory I/O)
- β Project 6 β PLC Python Modbus Monitor (Real-time data acquisition to CSV)
- π Project 7 β Industrial Dashboard (InfluxDB + Grafana)
Alexis Medrano β Control & Automation Engineer | Industry 4.0
github.com/AlexisMMMM