-
Notifications
You must be signed in to change notification settings - Fork 0
/
test7.py
193 lines (176 loc) · 9.24 KB
/
test7.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
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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test7.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import serial
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1199, 842)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.formLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.formLayoutWidget.setGeometry(QtCore.QRect(150, 30, 911, 431))
self.formLayoutWidget.setObjectName("formLayoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.PortNumber = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.PortNumber.setFont(font)
self.PortNumber.setObjectName("PortNumber")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.PortNumber)
self.SendDataBits = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.SendDataBits.setFont(font)
self.SendDataBits.setObjectName("SendDataBits")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.SendDataBits)
self.SendData = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.SendData.setFont(font)
self.SendData.setObjectName("SendData")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.SendData)
self.ReceivingData = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.ReceivingData.setFont(font)
self.ReceivingData.setObjectName("ReceivingData")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.ReceivingData)
self.datanumberEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.datanumberEdit.setFont(font)
self.datanumberEdit.setObjectName("datanumberEdit")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.datanumberEdit)
self.portnameEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.portnameEdit.setFont(font)
self.portnameEdit.setObjectName("portnameEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.portnameEdit)
self.datasenderEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.datasenderEdit.setFont(font)
self.datasenderEdit.setObjectName("datasenderEdit")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.datasenderEdit)
self.datareviewEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.datareviewEdit.setFont(font)
self.datareviewEdit.setObjectName("datareviewEdit")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.datareviewEdit)
self.open_button = QtWidgets.QPushButton(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.open_button.setFont(font)
self.open_button.setObjectName("open_button")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.open_button)
self.button = QtWidgets.QPushButton(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.button.setFont(font)
self.button.setObjectName("button")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.button)
self.button.clicked.connect(self.Cosender)
self.open_button.clicked.connect(self.Check_serial)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1199, 26))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
self.menuTest = QtWidgets.QMenu(self.menubar)
self.menuTest.setObjectName("menuTest")
self.menuSerial_Comm = QtWidgets.QMenu(self.menuTest)
self.menuSerial_Comm.setObjectName("menuSerial_Comm")
self.menuHelp = QtWidgets.QMenu(self.menubar)
self.menuHelp.setObjectName("menuHelp")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
#self.actionSerialOn = QtWidgets.QAction(MainWindow)
#self.actionSerialOn.setObjectName("actionSerialOn")
self.actionLollBack_Test = QtWidgets.QAction(MainWindow)
self.actionLollBack_Test.setObjectName("actionLollBack_Test")
self.actionReceive_Only = QtWidgets.QAction(MainWindow)
self.actionReceive_Only.setObjectName("actionReceive_Only")
self.actionAsync_Receive = QtWidgets.QAction(MainWindow)
self.actionAsync_Receive.setObjectName("actionAsync_Receive")
#self.menu.addAction(self.actionSerialOn)
self.menuSerial_Comm.addAction(self.actionLollBack_Test)
self.menuSerial_Comm.addAction(self.actionReceive_Only)
self.menuSerial_Comm.addAction(self.actionAsync_Receive)
self.menuTest.addAction(self.menuSerial_Comm.menuAction())
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menuTest.menuAction())
self.menubar.addAction(self.menuHelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.formLayoutWidget.hide()
self.actionLollBack_Test.triggered.connect(self.convert)
def convert(self):
if self.actionLollBack_Test.text() == "LollBack Test":
self.actionLollBack_Test.setText("OFF_LollBackTest")
self.formLayoutWidget.show()
elif self.actionLollBack_Test.text() == "OFF_LollBackTest":
self.actionLollBack_Test.setText("LollBack Test")
self.formLayoutWidget.hide()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.PortNumber.setText(_translate("MainWindow", "Port Number"))
self.SendDataBits.setText(_translate("MainWindow", "Send Data Bits"))
self.SendData.setText(_translate("MainWindow", "Send Data"))
self.ReceivingData.setText(_translate("MainWindow", "Receiving Data"))
self.open_button.setText(_translate("MainWindow", "open"))
self.button.setText(_translate("MainWindow", "Send out"))
self.menu.setTitle(_translate("MainWindow", "File"))
self.menuTest.setTitle(_translate("MainWindow", "Test"))
self.menuSerial_Comm.setTitle(_translate("MainWindow", "Serial Comm"))
self.menuHelp.setTitle(_translate("MainWindow", "Help"))
#self.actionSerialOn.setText(_translate("MainWindow", "SerialOn"))
self.actionLollBack_Test.setText(_translate("MainWindow", "LollBack Test"))
self.actionReceive_Only.setText(_translate("MainWindow", "Receive Only"))
self.actionAsync_Receive.setText(_translate("MainWindow", "Async Receive"))
def Cosender(self):
if self.flag_open==1:
if self.flag_open == 1: # Serial port is opened
self.str_input = self.datasenderEdit.text() # Return the sending text above
n = self.t.write((self.str_input + '\n').encode())
self.datanumberEdit.setText(str(n - 1)) # Write data digit box
self.datasenderEdit.setText(str(self.str_input)) # Write to send box
time.sleep(1) # sleep() and inWaiting() are best used in pairs
num = self.t.inWaiting() # Get the length of the received data
if num:
self.receivemessage = self.t.read(num) # Read and receive data
print(self.receivemessage)
self.datareviewEdit.setText(str(self.receivemessage)[2:-3]) # Write to receive box
else:
self.messageUI()
def Check_serial(self):
'''Detecting whether the serial port is opened'''
try:
self.t = serial.Serial('com3', 9600) #Open COM4 Serial Port
port = self.t.portstr #Return but port number
self.portnameEdit.setText(port) #Display on the interface
self.flag_open=1
except serial.serialutil.SerialException: #Failed to open, output prompt information
self.messageUI() #Tips
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())