Skip to content

Commit

Permalink
fix a bug
Browse files Browse the repository at this point in the history
is switching mqtt mode to serial mode makes serial
  • Loading branch information
Felix-fz committed May 26, 2021
1 parent 2d4467a commit 6345a43
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
Binary file added figure_2021_05_26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 69 additions & 27 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import binascii
import re
import sys
import datetime

import pyqtgraph as pg
import pyqtgraph.exporters
Expand Down Expand Up @@ -51,7 +52,6 @@ def __init__(self, parent=None):
self.addTab(self.ResultTab, "测试结果")
self.addTab(self.PlotTab, "数据打印")


self.CreateItems()
self.CreateLayout()
self.CreateSignalSlot()
Expand Down Expand Up @@ -95,6 +95,9 @@ def __init__(self, parent=None):
self.set_high_Ev = 0
self.set_low_Ev = 0

# sys time
self.sys_time = ""

def CreateItems(self):
self.com = QSerialPort()
self.comNameLabel = QLabel('串口号')
Expand Down Expand Up @@ -175,19 +178,19 @@ def CreateItems(self):

self.urlLabel = QLabel('URL:')
self.urlLabel.setFixedHeight(30)
self.urlEdit = QLineEdit('mqtt.coplemqtt.xyz')
self.urlEdit = QLineEdit('coplemqtthuaweicloud.top')
self.urlEdit.setFixedHeight(30)
self.urlEdit.setFixedWidth(200)

self.pub_topicLabel = QLabel('Pub Topic:')
self.pub_topicLabel.setFixedHeight(30)
self.pubTopicEdit = QLineEdit('pub')
self.pubTopicEdit = QLineEdit('sub_msg')
self.pubTopicEdit.setFixedHeight(30)
self.pubTopicEdit.setFixedWidth(80)

self.sub_topicLabel = QLabel('Sub Topic:')
self.sub_topicLabel.setFixedHeight(30)
self.subTopicEdit = QLineEdit('test')
self.subTopicEdit = QLineEdit('test_data')
self.subTopicEdit.setFixedHeight(30)
self.subTopicEdit.setFixedWidth(80)

Expand Down Expand Up @@ -235,8 +238,11 @@ def CreateItems(self):

######## 串口状态显示 ##########
self.comStatus = QLabel('COM State:COM CLOSE')
self.wifiStatus = QLabel('WiFi State:Disconnect')

self.sendCountLabel = QLabel('S cnt:0')
self.receiveCountLabel = QLabel('R cnt:0')

######## 串口状态显示 ##########

self.sendTimer = QTimer()
Expand Down Expand Up @@ -286,11 +292,25 @@ def CreateLayout(self): # 布局
self.PlotLayout.addWidget(self.receivedDataEdit)
self.PlotTab.setLayout(self.PlotLayout)

self.tableWidget.setRowCount(4) # 设置行数
self.tableWidget.setColumnCount(3) # 设置列数
self.tableWidget.setRowCount(500) # 设置行数
self.tableWidget.setColumnCount(6) # 设置列数

# 指定列宽
self.tableWidget.horizontalHeader().resizeSection(0, 200)
self.tableWidget.horizontalHeader().resizeSection(1, 200)
self.tableWidget.horizontalHeader().resizeSection(2, 200)
self.tableWidget.horizontalHeader().resizeSection(3, 200)
self.tableWidget.horizontalHeader().resizeSection(4, 200)
self.tableWidget.horizontalHeader().resizeSection(5, 200)

self.ResultLayout.addWidget(self.tableWidget)
self.ResultTab.setLayout(self.ResultLayout)

self.tableWidget.setHorizontalHeaderLabels(
['Time', 'Ip(A)', 'Ep(mV)', 'Ah(C)', 'High_E(mV)', 'Low_E(mV)', '备注']) # 设置水平表头
self.tableWidget.horizontalHeader().setFixedHeight(50)
self.tableWidget.horizontalHeader().setStyleSheet(
"QHeaderView::section{background-color:rgb(155, 194, 230);font:12pt 'Times';color: black;height:200pt};")

self.mainLayout.addWidget(self.stopShowingButton, 15, 0, 1, 1)
self.mainLayout.addWidget(self.saveReceivedButton, 15, 1, 1, 1)
Expand All @@ -306,12 +326,15 @@ def CreateLayout(self): # 布局
self.mainLayout.addWidget(self.clearCouterButton, 20, 5, 1, 1)
self.mainLayout.addWidget(self.timerSendCheck, 20, 2, 1, 1)
self.mainLayout.addWidget(self.timerPeriodEdit, 20, 3, 1, 1)
self.mainLayout.addWidget(self.sendCountLabel, 21, 7, 1, 1)

self.mainLayout.addWidget(self.comStatus, 21, 0, 1, 3)
self.mainLayout.addWidget(self.receiveCountLabel, 21, 8, 1, 1)
self.mainLayout.addWidget(self.wifiStatus, 22, 0, 1, 3)

self.mainLayout.addWidget(self.sendCountLabel, 21, 8, 1, 1)
self.mainLayout.addWidget(self.receiveCountLabel, 22, 8, 1, 1)
self.mainLayout.setSpacing(5)

self.setFixedSize(1265, 780)
self.setFixedSize(1265, 790)
self.DataTab.setLayout(self.mainLayout)

def CreateSignalSlot(self): # 创建逻辑
Expand Down Expand Up @@ -390,18 +413,22 @@ def update_plot(self):
self.measure_Ip = max(data_list)
self.set_high_Ev = max(cmd_list_re)
self.set_low_Ev = min(cmd_list_re)
self.sys_time = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')

if (cmd_list_re[-1] >= self.U1) and (self.scan_1_finish_flag == 1):
cmd_list_re.clear()
cmdlist.clear()
data_list.clear()
datalist.clear()
self.scan_1_finish_flag = 0
QMessageBox().information(self, "完成", "单向扫描完成!\n"
+ "ip=" + str(self.measure_Ip) + "A\n"
+ "Ep=" + str(self.measure_Ep) + "V\n"
+ "Ah=" + str(self.measure_Ah) + "C\n"
+ "High E(V)=" + str(self.set_high_Ev) + "mV\n"
+ "Low E(V)=" + str(self.set_low_Ev) + "mV\n")
+ "time:" + self.sys_time + "\n"
+ "ip=" + str(self.measure_Ip) + "A\n"
+ "Ep=" + str(self.measure_Ep) + "V\n"
+ "Ah=" + str(self.measure_Ah) + "C\n"
+ "High E(V)=" + str(self.set_high_Ev) + "mV\n"
+ "Low E(V)=" + str(self.set_low_Ev) + "mV\n")


elif (cmd_list_re[-1] <= int(self.U0Edit.text())) and (self.scan_2_finish_flag == 1):
cmd_list_re.clear()
Expand All @@ -410,6 +437,7 @@ def update_plot(self):
datalist.clear()
self.scan_2_finish_flag = 0
QMessageBox.information(self, "完成", "双向扫描完成!\n"
+ "time:" + self.sys_time + "\n"
+ "ip=" + str(self.measure_Ip) + "A\n"
+ "Ep=" + str(self.measure_Ep) + "V\n"
+ "Ah=" + str(self.measure_Ah) + "C\n"
Expand Down Expand Up @@ -572,18 +600,30 @@ def on_receiveData(self):
if self.stopShowingButton.text() == '停止显示':
if not self.hexShowingCheck.isChecked(): # 若不是16进制显示
receivedData = receivedData.decode(self.encoding, 'ignore')
received_split_str = receivedData.split(',')
# print(type(received_split_str))
cmd_str = received_split_str[0][1:]
value_str = received_split_str[1][:-1]
# print('cmd:', cmd_str, 'value:', value_str)

cmd_list = self.decodeFrame(cmd_str)
value_list = self.decodeFrame(value_str)
print(cmd_list, value_list)

datalist.extend(value_list)
cmdlist.extend(cmd_list) # 注意追加的变量
if receivedData[0] == '{':
received_split_str = receivedData.split(',')
# print(type(received_split_str))
cmd_str = received_split_str[0][1:]
value_str = received_split_str[1][:-1]
# print('cmd:', cmd_str, 'value:', value_str)

cmd_list = self.decodeFrame(cmd_str)
value_list = self.decodeFrame(value_str)
print(cmd_list, value_list)

datalist.extend(value_list)
cmdlist.extend(cmd_list) # 注意追加的变量
else:
if "connected" in receivedData:
QMessageBox.information(self, "WiFi提示", "下位机成功连接WiFi!")
self.wifiStatus.setText('WiFi State:Connect')
print("YES")
elif "Clear" in receivedData:
QMessageBox.information(self, "WiFi提示", "下位机成功断开WiFi!")
self.wifiStatus.setText('WiFi State:Disconnect')
self.on_closeSerial()
self.on_openSerial()
print("Disconnect!")

self.receivedDataEdit.insertPlainText(receivedData) # 回显
else:
Expand Down Expand Up @@ -737,6 +777,7 @@ def on_connect(client, userdata, flags, rc): # rc的值决定连接成功与否
def _subscribe(self, client: mqtt_client): # 订阅函数
def on_message(client, userdata, msg): # 收到消息调用
receivedData = msg.payload.decode(self.encoding, 'ignore') # 使用MQTT接收,加判断

received_split_str = receivedData.split(',')
cmd_str = received_split_str[0][1:]
value_str = received_split_str[1][:-1]
Expand All @@ -747,6 +788,7 @@ def on_message(client, userdata, msg): # 收到消息调用
cmdlist.extend(cmd_list) # 注意追加的变量

print(value_list, cmd_list)
self.receivedDataEdit.insertPlainText(receivedData) # 回显

client.subscribe(self.subTopic)
client.on_message = on_message
Expand All @@ -768,7 +810,7 @@ def mqtt_run(self):
elif self.connectFlag == 0:
QMessageBox.information(self, "连接提示", "断开连接!")
myclient.disconnect()
myclient.loop_stop(force=False)
myclient.loop_stop(force=True)
self.connectFlag = 1
self.connectButton.setEnabled(False)
self.disconnectButton.setEnabled(True)
Expand Down

0 comments on commit 6345a43

Please sign in to comment.