-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
67 lines (59 loc) · 2.27 KB
/
controller.py
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
import minimalmodbus
from model import VFDModel
from view import VFDView
import modbus_settings as MB
def generate_port_list():
port_list = []
for i in range(1, 21):
port_list.append(f"COM{i}")
return port_list
class VFDController:
def __init__(self, port):
self.com_ports = generate_port_list()
self.selected_port = port
self.model = VFDModel(self.selected_port)
self.view = VFDView(self, self.com_ports, self.selected_port)
self.connected = False
def connect(self, com_port):
try:
self.model = VFDModel(com_port)
self.connected = True
self.view.update_connection_status("Connected")
except Exception as e:
self.connected = False
self.view.update_connection_status("Disconnected")
self.view.show_error_message(f"Failed to connect to {com_port}: {str(e)}")
def reconnect(self):
selected_port = self.view.selected_com_port.get()
self.connect(selected_port)
def read_vfd(self):
if self.connected:
try:
frequency_data = self.model.read_VFD(MB.READ_FREQUENCY, MB.READ_LENGTH)
self.view.update_labels(frequency_data)
except minimalmodbus.ModbusException as e:
# Handle modbus communication error
print("Modbus Exception:", e)
except Exception as e:
# Handle other exceptions
print("Exception:", e)
else:
self.reconnect()
self.view.root.after(1000, self.read_vfd)
def set_spindle(self, value):
if self.connected:
# We write frequency to the vfd but the value will be viewed in RPMs
outcome = self.model.write_VFD(value, "frequency")
if outcome is False:
self.view.show_error_message("Please enter a value between 7200 and 18000")
else:
self.reconnect()
def adjust_spindle(self, value):
if self.connected:
outcome = self.model.write_VFD(value, "spindle")
if outcome is False:
self.view.show_error_message("Please enter a value between 7200 and 18000")
else:
self.reconnect()
def start(self):
self.view.start()