-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (118 loc) · 4.93 KB
/
main.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
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
import sys
import serial
import serial.tools.list_ports
from PyQt5.QtWidgets import (
QApplication, QDialog, QMainWindow, QMessageBox
)
from PyQt5.QtCore import QTimer
from PyQt5.uic import loadUi
from gui import Ui_MainWindow
import minimalmodbus
BAUDRATE = 57600
ADDRESS = 1
TABLE_HEIGHT_ADDRESS = 2000
BUTTON_PRESS_ADDRESS = 2002
USER_POS_1_ADDRESS = 26004
USER_POS_2_ADDRESS = 26006
USER_POS_3_ADDRESS = 26008
USER_POS_4_ADDRESS = 26010
class Window(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.timer = QTimer(self)
self.connectSignalsSlots()
def connectSignalsSlots(self):
self.refreshButton.clicked.connect(self.refreshComboBox)
#self.action_Exit.triggered.connect(self.close)
#self.action_About.triggered.connect(self.about)
self.connectButton.clicked.connect(self.connect)
self.timer.timeout.connect(self.getHeight)
self.button1.clicked.connect(self.setPosition1)
self.button2.clicked.connect(self.setPosition2)
self.button3.clicked.connect(self.setPosition3)
self.button4.clicked.connect(self.setPosition4)
self.upButton.pressed.connect(self.moveUp)
self.upButton.released.connect(self.stopMove)
self.downButton.pressed.connect(self.moveDown)
self.downButton.released.connect(self.stopMove)
self.stopButton.clicked.connect(self.stopMove)
self.actionSet_Position_1.triggered.connect(self.storePosition1)
def refreshComboBox(self):
ports = serial.tools.list_ports.comports()
self.portlist = [port.name for port in ports]
self.COMList.clear()
for port in ports:
self.COMList.addItem(port.name)
def connect(self):
print(self.COMList.currentText())
self.table = minimalmodbus.Instrument(self.COMList.currentText(), 1)
self.table.serial.baudrate = BAUDRATE
self.table.serial.stopbits = 2
self.timer.start(500)
self.preset1.display(self.readRegister(USER_POS_1_ADDRESS,0))
self.preset2.display(self.readRegister(USER_POS_2_ADDRESS,0))
self.preset3.display(self.readRegister(USER_POS_3_ADDRESS,0))
self.preset4.display(self.readRegister(USER_POS_4_ADDRESS,0))
def getHeight(self):
self.height = self.readRegister(TABLE_HEIGHT_ADDRESS, 0)
self.heightLCD.display(self.height)
def setPosition1(self):
self.writeRegister(BUTTON_PRESS_ADDRESS, 1, 0)
height = self.readRegister(USER_POS_1_ADDRESS,0)
self.heightLCD.display(height)
print("go to preset 1:"+str(height)+" cm")
def setPosition2(self):
self.writeRegister(BUTTON_PRESS_ADDRESS, 2, 0)
height = self.readRegister(USER_POS_2_ADDRESS,0)
self.heightLCD.display(height)
print("go to preset 2:"+str(height)+" cm")
def setPosition3(self):
self.writeRegister(BUTTON_PRESS_ADDRESS, 3, 0)
height = self.readRegister(USER_POS_3_ADDRESS,0)
self.heightLCD.display(height)
print("go to preset 3:"+str(height)+" cm")
def setPosition4(self):
self.writeRegister(BUTTON_PRESS_ADDRESS, 4, 0)
height = self.readRegister(USER_POS_4_ADDRESS,0)
self.heightLCD.display(height)
print("go to preset 4:"+str(height)+" cm")
def moveUp(self):
self.writeRegister(BUTTON_PRESS_ADDRESS, 5, 0)
#self.writeRegister(BUTTON_PRESS_ADDRESS, 5, 0)
def moveDown(self):
self.writeRegister(BUTTON_PRESS_ADDRESS, 6, 0)
def stopMove(self):
self.writeRegister(BUTTON_PRESS_ADDRESS, 0, 0)
def storePosition1(self):
self.writeRegister(USER_POS_1_ADDRESS, self.height, 0)
self.preset1.display(self.readRegister(USER_POS_1_ADDRESS,0))
def storePosition2(self):
self.writeRegister(USER_POS_2_ADDRESS, self.height, 0)
self.preset2.display(self.readRegister(USER_POS_2_ADDRESS,0))
def storePosition3(self):
self.writeRegister(USER_POS_3_ADDRESS, self.height, 0)
self.preset3.display(self.readRegister(USER_POS_3_ADDRESS,0))
def storePosition4(self):
self.writeRegister(USER_POS_4_ADDRESS, self.height, 0)
self.preset4.display(self.readRegister(USER_POS_4_ADDRESS,0))
def readRegister(self,*args):
try:
answer = self.table.read_register(*args)
except Exception as error:
answer = None
# handle the exception
print("An exception occurred:", type(error).__name__)
return answer
def writeRegister(self, *args):
print("Writeregister"+str(args))
try:
self.table.write_register(*args)
except Exception as error:
# handle the exception
print("An exception occurred:", type(error).__name__)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())