使用说明
使用VSPD建立一组虚拟串口
查看MCU的端口号与波特率并修改python程序配置,运行即可看到双方收发的效果
通过串口助手连接到虚拟串口并向其发送消息
即Python显示MCU端发给串口助手的消息,并显示串口助手发送至MCU的消息,中间使用VSPD与Python进行消息互转与打印。
Python监听串口收发双方程序
import serial
import threading
'''
通过监听MCU端与PC上位机软件的通讯,打印双方收发的消息
'''
mcu_serial_config = {"MCU_serial": "COM19", "baud_rate": 115200}
vspd_serial_config = {"VSPD_A": "COM10", "VSPD_B": "COM11"}
class SerialLog:
def __init__(self, serial_config_dic, vspd_config=None):
if vspd_config == None:
vspd_config = {"VSPD_A": "COM10", "VSPD_B": "COM11"}
self.mcu_serial_num = serial_config_dic["MCU_serial"]
self.baud_rate = serial_config_dic["baud_rate"]
self.mcu_serial = serial.Serial(self.mcu_serial_num, self.baud_rate)
self.vspd_serial = serial.Serial(vspd_config["VSPD_A"], self.baud_rate)
# 创建两个线程
try:
threading.Thread(target=self.listing_mcu).start()
threading.Thread(target=self.listing_vspd).start()
except:
print(f"Error: 无法启动线程\n")
# 循环
while 1:
pass
# 监听MCU端串口信息
def listing_mcu(self):
try:
while 1:
if not self.mcu_serial.isOpen():
continue
data_size = self.mcu_serial.in_waiting
if data_size < 1:
continue
data = self.mcu_serial.read(data_size)
print(f'MCU:{data}')
self.vspd_serial.write(data)
finally:
print("Done")
# 监听VSPD端串口信息
def listing_vspd(self):
try:
while 1:
if not self.vspd_serial.isOpen():
continue
data_size = self.vspd_serial.in_waiting
if data_size < 1:
continue
data = self.vspd_serial.read(data_size)
print(f'VSPD:{data}')
self.mcu_serial.write(data)
finally:
print("Done")
if __name__ == '__main__':
print('Begining')
app = SerialLog(mcu_serial_config, vspd_serial_config)
标签:python,vspd,self,收发,串口,serial,config,data,mcu
From: https://www.cnblogs.com/dapenson/p/16885244.html