python?+?pyqt5制作一個(gè)串口助手
一、背景
串口助手是串口通信使用的工具。為整合知識(shí),打算做個(gè)串口助手。
1.1、開(kāi)發(fā)流程圖
如下圖1:

圖1
二、前提
2.1、關(guān)于環(huán)境
操作系統(tǒng):win 10
編輯器:pycharm edu
語(yǔ)言及版本:python 3.8
使用的庫(kù):pyqt5、sys、time等等庫(kù)
實(shí)現(xiàn)思路:需要實(shí)現(xiàn)什么功能就做什么功能,最后把功能整合在一起
前提:在pycharm 中配置好pyqt及其工具
最終實(shí)現(xiàn)的功能:串口選擇、串口狀態(tài)顯示、發(fā)送數(shù)據(jù)、定時(shí)發(fā)送發(fā)送區(qū)數(shù)據(jù)(定時(shí)時(shí)間可修改)、接收數(shù)據(jù)、數(shù)據(jù)顯示(ASCII)、數(shù)據(jù)顯示自動(dòng)換行、數(shù)據(jù)顯示顯示時(shí)間、清除接收區(qū)
2.2、關(guān)于源碼
源碼比較多,需要源碼借鑒的評(píng)論區(qū)留言,私聊我,進(jìn)行源碼分享。
三、步驟
3.1、使用pyqt創(chuàng)建一個(gè).ui界面并生成.py文件
如下圖2:

圖2
3.2、創(chuàng)建兩個(gè).py文件,一個(gè)用來(lái)繼承ui界面生成的.py類(lèi),一個(gè)用來(lái)實(shí)現(xiàn)各種功能
如下圖3:

圖3
3.3、各個(gè)功能代碼
3.3.1、打開(kāi)串口
def open_port(port_name, baudrate, bytesize, stop_bit, parity):
ser = serial.Serial(
port = port_name,
baudrate=baudrate,
bytesize=bytesize,
stopbits=stop_bit,
parity=parity,
rtscts=False,
xonxoff=False,
timeout=None,
write_timeout=None
)
return ser3.3.2、關(guān)閉串口
def close_port(self):
if self.current_port is not None:
self.time.stop()
self.serial_thread.ser = None
self.current_port.close()
self.current_port = None
self.ui.port_status.setText("closed")
self.ui.open_port.setDisabled(False)
self.ui.send_data.setDisabled(True)
self.ui.close_port.setDisabled(True)3.3.3、獲取串口號(hào)
import serial
import serial.tools.list_ports
def serial_port():
port_list = []
portlist = list(serial.tools.list_ports.comports())
for port in portlist:
port_list.append(port.device)
return port_list3.3.4、發(fā)送數(shù)據(jù)
def send_data(self):
if self.ui.send_select.isChecked():
timer = self.ui.send_time.value()
self.time.start(timer)
else:
self.time.stop()
data = self.ui.input_data.toPlainText()
print("這是發(fā)送的數(shù)據(jù):"+data)
self.current_port.write(data.encode('gbk'))3.3.5、接收數(shù)據(jù)
class SerialThread(QThread):
data_arrive_signal = pyqtSignal()
def __init__(self, ser=None):
super().__init__()
self.ser = ser
self.data = ''
def run(self):
while True:
if self.ser and self.ser.in_waiting:
self.data += self.ser.read_all().decode('gbk')
print(self.data)
self.data_arrive_signal.emit()3.3.6、數(shù)據(jù)顯示
def show_data(self):
self.recive_data = self.serial_thread.data
self.zdhh_data += self.recive_data
now_time = datetime.datetime.now()
if self.ui.zdhh.isChecked():
self.zdhh_data += '\n'
self.ui.show_ser_data.setText(self.zdhh_data)
print("這里是顯示的數(shù)據(jù):"+self.zdhh_data)
if self.ui.show_time.isChecked():
print(self.ui.show_time.isChecked())
time_data = str(now_time) + self.zdhh_data
print(time_data)
self.ui.show_ser_data.setText(time_data)
if self.ui.zdhh.isChecked() and self.ui.show_time.isChecked():
self.zdhh_data += '\n'
time_data = str(now_time) + self.zdhh_data
self.ui.show_ser_data.setText(time_data)
else:
self.ui.show_ser_data.setText(self.recive_data)
print("這里是顯示的數(shù)據(jù):"+self.recive_data)3.4、qtdersigner界面
如下圖4:

圖4
3.5、美化后運(yùn)行的程序
如下圖5:

圖5
3.6、打包.py為.exe
如下圖6:

圖6
3.7、打包為.exe后運(yùn)行
如下圖7:

圖7
3.8、串口測(cè)試連接圖
如下圖8:
兩個(gè)串口的RXD和TXD需要互接,進(jìn)行數(shù)據(jù)的互傳,在測(cè)試時(shí)這樣使用比用單片機(jī)發(fā)數(shù)據(jù)更快。

四、結(jié)果
4.1、代碼運(yùn)行結(jié)果
同圖6、7:

4.2、打包后運(yùn)行結(jié)果
同圖6、7:

4.3、將.py文件-->.exe文件可能遇到的問(wèn)題
4.3.1、UPX is not available
解決辦法:點(diǎn)這里
4.3.2、pip命令版本不匹配,需要升級(jí)pip版本
下載pyinstaller,在cmd命令行出現(xiàn)報(bào)錯(cuò),會(huì)有提示建議,輸入提示建議的命令即可。
到此這篇關(guān)于python + pyqt5制作一個(gè)串口助手的文章就介紹到這了,更多相關(guān)python 串口助手內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Theano和Tensorflow多GPU使用問(wèn)題
這篇文章主要介紹了關(guān)于Theano和Tensorflow多GPU使用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python爬取”頂點(diǎn)小說(shuō)網(wǎng)“《純陽(yáng)劍尊》的示例代碼
這篇文章主要介紹了python爬取”頂點(diǎn)小說(shuō)網(wǎng)“《純陽(yáng)劍尊》的示例代碼,幫助大家更好的利用python 爬蟲(chóng)爬取數(shù)據(jù),感興趣的朋友可以了解下2020-10-10
如何基于Python實(shí)現(xiàn)電子郵件的發(fā)送
這篇文章主要介紹了如何基于Python實(shí)現(xiàn)電子郵件的發(fā)送,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
如何使用 python查詢(xún)Amazon DynamoDB
本文介紹了如何使用Python Boto3在Amazon DynamoDB上查詢(xún)DynamoDB 表、創(chuàng)建、列出和執(zhí)行其他 CRUD 活動(dòng)以及執(zhí)行其他維護(hù)任務(wù),本文給大家介紹的非常詳細(xì),需要的朋友參考下2023-06-06
python 批量修改/替換數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇python 批量修改/替換數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
關(guān)于keras中卷積層Conv2D的學(xué)習(xí)記錄
這篇文章主要介紹了關(guān)于keras中卷積層Conv2D的學(xué)習(xí)記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Pycharm編輯器功能之代碼折疊效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了Pycharm編輯器功能之代碼折疊效果的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

