You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
5.5 KiB
179 lines
5.5 KiB
|
|
from serial.serialutil import SerialTimeoutException
|
|
|
|
from all_ui.uart import Ui_MainWindow as pyui
|
|
import serial
|
|
import serial.tools.list_ports
|
|
import time
|
|
from PySide6.QtCore import QTimer
|
|
|
|
A=0
|
|
currentcom=""
|
|
|
|
class UI(pyui):
|
|
def __init__(self):
|
|
global current_com
|
|
# 从文件中加载UI定义
|
|
super().__init__()
|
|
self.setupUi(self)
|
|
|
|
self.__portlist=[]
|
|
#表示每1ms做一次receivedata当然需要用启动按钮来启动接收
|
|
self.time=QTimer()
|
|
self.time.setInterval(50)
|
|
self.time.timeout.connect(self.receive_data)
|
|
#每4s就读取一次端口号
|
|
self.time1 = QTimer()
|
|
self.time1.setInterval(4000)
|
|
self.time1.timeout.connect(self.read_com)
|
|
self.time1.start()
|
|
#初始化端口列表
|
|
self.comboBox_2.addItems(self.get_com_list())
|
|
|
|
|
|
self.ser=serial.Serial()
|
|
#初始化端口
|
|
self.ser.port=self.comboBox_2.currentText()
|
|
#初始化波特率
|
|
self.ser.baudrate=int(self.comboBox.currentText())
|
|
self.ser.bytesize=int(self.comboBox_6.currentText()) #每个数据包的字节数
|
|
self.ser.timeout = 1 # 设置超时时间为1秒
|
|
#四个按钮
|
|
self.Button1.clicked.connect(self.button1_clicked)
|
|
self.Button2.clicked.connect(self.button2_clicked)
|
|
self.Button3.clicked.connect(self.button3_clicked)
|
|
self.Button4.clicked.connect(self.button4_clicked)
|
|
|
|
self.comboBox.currentTextChanged.connect(self.comboBoxchange)
|
|
self.comboBox_2.currentTextChanged.connect(self.comboBox_2change)
|
|
self.comboBox_3.currentTextChanged.connect(self.comboBox_3change)
|
|
self.comboBox_4.currentTextChanged.connect(self.comboBox_4change)
|
|
self.comboBox_5.currentTextChanged.connect(self.comboBox_5change)
|
|
self.comboBox_6.currentTextChanged.connect(self.comboBox_6change)
|
|
#button1为开始接收
|
|
def button1_clicked(self):
|
|
#接收区的文本框
|
|
global A
|
|
if A==0:
|
|
A=1
|
|
else:
|
|
A=0
|
|
if A==1:
|
|
try:
|
|
if not self.ser.is_open:
|
|
self.ser.open()
|
|
self.ser.read(10)
|
|
except SerialTimeoutException:
|
|
# 处理串口通信超时异常
|
|
self.plainTextEdit.insertPlainText("未检测到此端口有任何设备")
|
|
self.ser.close()
|
|
else:
|
|
self.ser.close()
|
|
self.ser.open()
|
|
self.time.start()
|
|
|
|
else:
|
|
self.ser.close()
|
|
self.time.stop()
|
|
|
|
|
|
#将发送区的东西发出去
|
|
def button2_clicked(self):
|
|
# 打开串口
|
|
if not self.ser.is_open:
|
|
self.ser.open()
|
|
# 清空输入缓冲区和输出缓冲区
|
|
# self.ser.reset_input_buffer()
|
|
# self.ser.reset_output_buffer()
|
|
# 将文本通过串口发送
|
|
|
|
self.ser.write(self.plainTextEdit_2.toPlainText().encode('utf-8')) # 将字符串转换为字节流并发送
|
|
|
|
# 关闭串口
|
|
self.ser.close()
|
|
|
|
pass
|
|
|
|
|
|
#清空接收区succ
|
|
def button3_clicked(self):
|
|
self.plainTextEdit.clear()
|
|
|
|
|
|
#清空发送区succ
|
|
def button4_clicked(self):
|
|
self.plainTextEdit_2.clear()
|
|
|
|
|
|
def receive_data(self):
|
|
|
|
# num = self.ser.in_waiting
|
|
# if num > 0:
|
|
time.sleep(0.1)
|
|
num = self.ser.in_waiting
|
|
# 延时,再读一次数据,确保数据完整性
|
|
data = self.ser.read(num)
|
|
try:
|
|
self.plainTextEdit.insertPlainText(str(self.ser.read(num),'utf-8'))
|
|
except UnicodeDecodeError:
|
|
self.plainTextEdit.insertPlainText("波特率不对")
|
|
self.ser.flush()
|
|
|
|
|
|
pass
|
|
|
|
def get_com_list(self):
|
|
com_dev_list = tuple(serial.tools.list_ports.comports())
|
|
if not com_dev_list:
|
|
self.__portlist = []
|
|
else:
|
|
self.__portlist = [i.name for i in com_dev_list]
|
|
return self.__portlist
|
|
#动态读取目前的端口号succ
|
|
def read_com(self):
|
|
global currentcom
|
|
currentcom=self.comboBox_2.currentText()
|
|
if A==1:
|
|
self.time.stop()
|
|
self.ser.close()
|
|
self.comboBox_2.clear()
|
|
self.comboBox_2.addItems(self.get_com_list())
|
|
self.comboBox_2.setCurrentText(currentcom)
|
|
self.ser.port = self.comboBox_2.currentText()
|
|
if A==1:
|
|
self.ser.open()
|
|
self.time.start()
|
|
|
|
|
|
#波特率succ
|
|
def comboBoxchange(self):
|
|
self.ser.baudrate=int(self.comboBox.currentText())
|
|
|
|
#端口号succ
|
|
def comboBox_2change(self):
|
|
self.ser.port=self.comboBox_2.currentText()
|
|
|
|
#是否启用数据流控succ
|
|
def comboBox_3change(self):
|
|
if self.comboBox_3.currentText() == 'None':
|
|
self.ser.xonxoff=False
|
|
self.ser.dsrdtr=False
|
|
self.ser.rtscts=False
|
|
elif self.comboBox_3.currentText() == 'soft':
|
|
self.ser.xonxoff = True
|
|
self.ser.dsrdtr = False
|
|
self.ser.rtscts = False
|
|
else:
|
|
self.ser.xonxoff = False
|
|
self.ser.dsrdtr = True
|
|
self.ser.rtscts = True
|
|
pass
|
|
#设置校验位succ
|
|
def comboBox_4change(self):
|
|
self.ser.parity=self.comboBox_4.currentText()
|
|
#设置停止位succ
|
|
def comboBox_5change(self):
|
|
self.ser.stopbits = int(self.comboBox_5.currentText())
|
|
#设置一帧数据的长度一般默认为8succ
|
|
def comboBox_6change(self):
|
|
self.ser.bytesize=int(self.comboBox_6.currentText()) |