-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpymodbus_gui.py
More file actions
423 lines (340 loc) · 18.7 KB
/
pymodbus_gui.py
File metadata and controls
423 lines (340 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import csv
import time
import os
import threading
import tkinter as tk
from tkinter import ttk, scrolledtext, filedialog, messagebox
from datetime import datetime
from pymodbus.client import ModbusTcpClient, ModbusSerialClient
class ModbusLoggerUI:
def __init__(self, root):
self.root = root
self.root.title("Modbus Data Logger")
self.root.geometry("800x600")
self.root.minsize(800, 600)
# Variables
self.log_running = False
self.log_thread = None
self.client = None
self.reconnect_count = 0
self.max_reconnect_attempts = 3
# Configuration variables
self.modbus_type = tk.StringVar(value="rtu")
self.plc_ip = tk.StringVar(value="192.168.1.10")
self.plc_port = tk.IntVar(value=502)
self.serial_port = tk.StringVar(value="COM3")
self.baudrate = tk.IntVar(value=9600)
self.parity = tk.StringVar(value="O")
self.stopbits = tk.IntVar(value=1)
self.bytesize = tk.IntVar(value=8)
self.timeout = tk.IntVar(value=1)
self.register_address = tk.StringVar(value="0x6304")
self.register_count = tk.IntVar(value=1)
self.unit_id = tk.IntVar(value=1)
self.csv_file = tk.StringVar(value="plc_data.csv")
self.log_interval = tk.IntVar(value=5)
# Create UI
self.create_ui()
# Log area
self.log("Modbus Data Logger started")
def create_ui(self):
notebook = ttk.Notebook(self.root)
notebook.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Create tabs
settings_frame = ttk.Frame(notebook)
log_frame = ttk.Frame(notebook)
data_frame = ttk.Frame(notebook)
notebook.add(settings_frame, text="Settings")
notebook.add(log_frame, text="Log")
notebook.add(data_frame, text="Data")
# Settings Tab
self.create_settings_tab(settings_frame)
# Log Tab
self.create_log_tab(log_frame)
# Data Tab
self.create_data_tab(data_frame)
# Control buttons at the bottom
control_frame = ttk.Frame(self.root)
control_frame.pack(fill=tk.X, padx=10, pady=10)
self.start_button = ttk.Button(control_frame, text="Start Logging", command=self.start_logging)
self.start_button.pack(side=tk.LEFT, padx=5)
self.stop_button = ttk.Button(control_frame, text="Stop Logging", command=self.stop_logging, state=tk.DISABLED)
self.stop_button.pack(side=tk.LEFT, padx=5)
ttk.Button(control_frame, text="Exit", command=self.on_exit).pack(side=tk.RIGHT, padx=5)
def create_settings_tab(self, parent):
settings_frame = ttk.LabelFrame(parent, text="Connection Settings")
settings_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Left column - Connection settings
left_frame = ttk.Frame(settings_frame)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10, pady=10)
ttk.Label(left_frame, text="Modbus Type:").grid(row=0, column=0, sticky=tk.W, pady=2)
ttk.Radiobutton(left_frame, text="TCP", variable=self.modbus_type, value="tcp", command=self.update_fields).grid(row=0, column=1, sticky=tk.W, pady=2)
ttk.Radiobutton(left_frame, text="RTU (Serial)", variable=self.modbus_type, value="rtu", command=self.update_fields).grid(row=0, column=2, sticky=tk.W, pady=2)
# TCP Settings
self.tcp_frame = ttk.LabelFrame(left_frame, text="TCP Settings")
self.tcp_frame.grid(row=1, column=0, columnspan=3, sticky=tk.W+tk.E, pady=5)
ttk.Label(self.tcp_frame, text="PLC IP:").grid(row=0, column=0, sticky=tk.W, pady=2)
ttk.Entry(self.tcp_frame, textvariable=self.plc_ip, width=15).grid(row=0, column=1, sticky=tk.W, pady=2)
ttk.Label(self.tcp_frame, text="Port:").grid(row=1, column=0, sticky=tk.W, pady=2)
ttk.Entry(self.tcp_frame, textvariable=self.plc_port, width=6).grid(row=1, column=1, sticky=tk.W, pady=2)
# RTU Settings
self.rtu_frame = ttk.LabelFrame(left_frame, text="RTU Settings")
self.rtu_frame.grid(row=2, column=0, columnspan=3, sticky=tk.W+tk.E, pady=5)
ttk.Label(self.rtu_frame, text="Serial Port:").grid(row=0, column=0, sticky=tk.W, pady=2)
ttk.Entry(self.rtu_frame, textvariable=self.serial_port, width=10).grid(row=0, column=1, sticky=tk.W, pady=2)
ttk.Label(self.rtu_frame, text="Baudrate:").grid(row=1, column=0, sticky=tk.W, pady=2)
ttk.Combobox(self.rtu_frame, textvariable=self.baudrate, width=8, values=[1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]).grid(row=1, column=1, sticky=tk.W, pady=2)
ttk.Label(self.rtu_frame, text="Parity:").grid(row=2, column=0, sticky=tk.W, pady=2)
ttk.Combobox(self.rtu_frame, textvariable=self.parity, width=3, values=["N", "E", "O"]).grid(row=2, column=1, sticky=tk.W, pady=2)
ttk.Label(self.rtu_frame, text="Stopbits:").grid(row=3, column=0, sticky=tk.W, pady=2)
ttk.Combobox(self.rtu_frame, textvariable=self.stopbits, width=3, values=[1, 2]).grid(row=3, column=1, sticky=tk.W, pady=2)
ttk.Label(self.rtu_frame, text="Bytesize:").grid(row=4, column=0, sticky=tk.W, pady=2)
ttk.Combobox(self.rtu_frame, textvariable=self.bytesize, width=3, values=[5, 6, 7, 8]).grid(row=4, column=1, sticky=tk.W, pady=2)
ttk.Label(self.rtu_frame, text="Timeout:").grid(row=5, column=0, sticky=tk.W, pady=2)
ttk.Entry(self.rtu_frame, textvariable=self.timeout, width=3).grid(row=5, column=1, sticky=tk.W, pady=2)
# Right column - Register settings
right_frame = ttk.Frame(settings_frame)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10)
register_frame = ttk.LabelFrame(right_frame, text="Modbus Register Settings")
register_frame.pack(fill=tk.X, pady=5)
ttk.Label(register_frame, text="Register Address:").grid(row=0, column=0, sticky=tk.W, pady=2)
ttk.Entry(register_frame, textvariable=self.register_address, width=10).grid(row=0, column=1, sticky=tk.W, pady=2)
ttk.Label(register_frame, text="Register Count:").grid(row=1, column=0, sticky=tk.W, pady=2)
ttk.Entry(register_frame, textvariable=self.register_count, width=5).grid(row=1, column=1, sticky=tk.W, pady=2)
ttk.Label(register_frame, text="Unit ID:").grid(row=2, column=0, sticky=tk.W, pady=2)
ttk.Entry(register_frame, textvariable=self.unit_id, width=5).grid(row=2, column=1, sticky=tk.W, pady=2)
# Logging settings
log_settings_frame = ttk.LabelFrame(right_frame, text="Logging Settings")
log_settings_frame.pack(fill=tk.X, pady=5)
ttk.Label(log_settings_frame, text="CSV File:").grid(row=0, column=0, sticky=tk.W, pady=2)
ttk.Entry(log_settings_frame, textvariable=self.csv_file, width=20).grid(row=0, column=1, sticky=tk.W, pady=2)
ttk.Button(log_settings_frame, text="Browse...", command=self.browse_csv).grid(row=0, column=2, sticky=tk.W, pady=2)
ttk.Label(log_settings_frame, text="Log Interval (s):").grid(row=1, column=0, sticky=tk.W, pady=2)
ttk.Entry(log_settings_frame, textvariable=self.log_interval, width=5).grid(row=1, column=1, sticky=tk.W, pady=2)
# Update fields based on current selection
self.update_fields()
def create_log_tab(self, parent):
# Log display
log_frame = ttk.Frame(parent)
log_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Log text area
self.log_text = scrolledtext.ScrolledText(log_frame, wrap=tk.WORD, height=20)
self.log_text.pack(fill=tk.BOTH, expand=True)
self.log_text.config(state=tk.DISABLED)
# Button to clear log
ttk.Button(log_frame, text="Clear Log", command=self.clear_log).pack(pady=5)
def create_data_tab(self, parent):
# Data display
data_frame = ttk.Frame(parent)
data_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Table for data
self.tree = ttk.Treeview(data_frame, columns=("timestamp", "value"), show="headings")
self.tree.heading("timestamp", text="Timestamp")
self.tree.heading("value", text="Value")
self.tree.column("timestamp", width=200)
self.tree.column("value", width=100)
self.tree.pack(fill=tk.BOTH, expand=True)
# Scrollbar
scrollbar = ttk.Scrollbar(self.tree, orient=tk.VERTICAL, command=self.tree.yview)
self.tree.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
def update_fields(self):
# Enable/disable fields based on Modbus type
if self.modbus_type.get() == "tcp":
for child in self.tcp_frame.winfo_children():
child.configure(state=tk.NORMAL)
for child in self.rtu_frame.winfo_children():
child.configure(state=tk.DISABLED)
else:
for child in self.tcp_frame.winfo_children():
child.configure(state=tk.DISABLED)
for child in self.rtu_frame.winfo_children():
child.configure(state=tk.NORMAL)
def browse_csv(self):
filename = filedialog.asksaveasfilename(
defaultextension=".csv",
filetypes=[("CSV files", "*.csv"), ("All files", "*.*")]
)
if filename:
self.csv_file.set(filename)
def log(self, message):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message = f"[{timestamp}] {message}\n"
self.log_text.config(state=tk.NORMAL)
self.log_text.insert(tk.END, log_message)
self.log_text.see(tk.END)
self.log_text.config(state=tk.DISABLED)
# Also print to console for debugging
print(log_message.strip())
def clear_log(self):
self.log_text.config(state=tk.NORMAL)
self.log_text.delete(1.0, tk.END)
self.log_text.config(state=tk.DISABLED)
def add_data_row(self, timestamp, value):
self.tree.insert("", 0, values=(timestamp, value))
# Limit to 1000 rows to prevent memory issues
if len(self.tree.get_children()) > 1000:
self.tree.delete(self.tree.get_children()[-1])
def start_logging(self):
# Validate settings
try:
# Convert register address from hex string if needed
if self.register_address.get().startswith("0x"):
register_addr = int(self.register_address.get(), 16)
else:
register_addr = int(self.register_address.get())
# Update GUI state
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
# Set log running flag
self.log_running = True
# Start logging thread
self.log_thread = threading.Thread(target=self.logging_thread, args=(
self.modbus_type.get(),
self.plc_ip.get(),
self.plc_port.get(),
self.serial_port.get(),
self.baudrate.get(),
self.parity.get(),
self.stopbits.get(),
self.bytesize.get(),
self.timeout.get(),
register_addr,
self.register_count.get(),
self.unit_id.get(),
self.csv_file.get(),
self.log_interval.get()
))
self.log_thread.daemon = True
self.log_thread.start()
except ValueError as e:
messagebox.showerror("Invalid Settings", f"Invalid settings: {str(e)}")
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
def stop_logging(self):
self.log_running = False
self.log("Stopping data logging...")
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
def connect_to_plc(self, modbus_type, plc_ip, plc_port, serial_port, baudrate, parity, stopbits, bytesize, timeout):
"""Connects to the PLC using either Modbus TCP or Modbus RTU."""
try:
if modbus_type == "tcp":
client = ModbusTcpClient(plc_ip, port=plc_port)
self.log(f"Attempting to connect to PLC at {plc_ip}:{plc_port} via Modbus TCP")
else:
client = ModbusSerialClient(
port=serial_port,
baudrate=baudrate,
parity=parity,
stopbits=stopbits,
bytesize=bytesize,
timeout=timeout,
)
self.log(f"Attempting to connect to PLC at {serial_port} via Modbus RTU")
if client.connect():
self.log(f"✅ Connected to PLC via Modbus {modbus_type.upper()}")
return client
else:
self.log("❌ Failed to connect to PLC")
return None
except Exception as e:
self.log(f"❌ Exception while connecting to PLC: {str(e)}")
return None
def read_plc_data(self, client, register_address, register_count, unit_id):
"""Reads data from PLC coils."""
try:
response = client.read_coils(address=6304, count=1)
if hasattr(response, 'isError') and response.isError():
self.log(f"❌ Modbus Error: {response}")
return None
else:
return response.bits # ✅ Correct attribute for coils (True/False list)
except Exception as e:
self.log(f"❌ Exception while reading PLC: {str(e)}")
return None
def initialize_csv(self, csv_file, register_address, register_count):
"""Creates a new CSV file with headers if it doesn't exist."""
if not os.path.exists(csv_file):
with open(csv_file, mode="w", newline="") as file:
writer = csv.writer(file)
headers = ["Timestamp"] + [f"Register_{register_address + i}" for i in range(register_count)]
writer.writerow(headers)
self.log(f"✅ Created new CSV file: {csv_file}")
def save_to_csv(self, csv_file, data):
"""Saves PLC data to a CSV file with timestamps."""
if data is None:
self.log("⚠️ No data to save")
return
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Open CSV in append mode
with open(csv_file, mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow([timestamp] + [1 if bit else 0 for bit in data]) # Convert boolean to 1/0
# Format data for display
data_str = [1 if bit else 0 for bit in data]
self.log(f"✅ Data logged at {timestamp}: {data_str}")
# Add to data display
for i, bit in enumerate(data):
value = 1 if bit else 0
self.root.after(0, lambda t=timestamp, v=value: self.add_data_row(t, v))
def logging_thread(self, modbus_type, plc_ip, plc_port, serial_port, baudrate, parity, stopbits, bytesize, timeout,
register_address, register_count, unit_id, csv_file, log_interval):
"""Thread function for logging data."""
# Initialize CSV file with headers
self.initialize_csv(csv_file, register_address, register_count)
# Connect to PLC
client = self.connect_to_plc(modbus_type, plc_ip, plc_port, serial_port, baudrate, parity, stopbits, bytesize, timeout)
if client is None:
retry_count = 0
while client is None and retry_count < self.max_reconnect_attempts and self.log_running:
self.log(f"Retrying connection ({retry_count + 1}/{self.max_reconnect_attempts})...")
time.sleep(5) # Wait before retry
retry_count += 1
client = self.connect_to_plc(modbus_type, plc_ip, plc_port, serial_port, baudrate, parity, stopbits, bytesize, timeout)
if client is None:
self.log("Failed to connect after multiple attempts. Exiting.")
self.root.after(0, self.stop_logging)
return
self.log("📡 Starting data logging...")
try:
reconnect_count = 0
while self.log_running:
data = self.read_plc_data(client, register_address, register_count, unit_id)
# Handle connection loss during operation
if data is None and reconnect_count < self.max_reconnect_attempts:
self.log(f"Connection may be lost. Attempting to reconnect ({reconnect_count + 1}/{self.max_reconnect_attempts})...")
client.close()
time.sleep(2)
client = self.connect_to_plc(modbus_type, plc_ip, plc_port, serial_port, baudrate, parity, stopbits, bytesize, timeout)
reconnect_count += 1
if client is None:
continue
elif data is None:
self.log("Too many failed read attempts. Stopping logging.")
break
else:
reconnect_count = 0 # Reset counter on successful read
self.save_to_csv(csv_file, data)
time.sleep(log_interval) # Wait before next read
except Exception as e:
self.log(f"Error in logging thread: {str(e)}")
finally:
if client is not None:
client.close()
self.log("Connection closed. Logging stopped.")
self.root.after(0, self.stop_logging)
def on_exit(self):
if self.log_running:
if messagebox.askyesno("Exit", "Logging is still running. Are you sure you want to exit?"):
self.stop_logging()
self.root.destroy()
else:
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = ModbusLoggerUI(root)
# Handle window close event
root.protocol("WM_DELETE_WINDOW", app.on_exit)
# Start main loop
root.mainloop()