1、Python使用线程来接收串口数据
2、python3 Serial 串口助手的接收读取数据
创建串口助手首先需要创建一个类,重构类的实现过程如下:
#coding=gb18030
import threading
import TIme
import serial
class ComThread:
def __init__(self, Port=‘COM3’):
#构造串口的属性
self.l_serial = None
self.alive = False
self.waitEnd = None
self.port = Port
self.ID = None
self.data = None
#定义串口等待的函数
def waiTIng(self):
if not self.waitEnd is None:
self.waitEnd.wait()
def SetStopEvent(self):
if not self.waitEnd is None:
self.waitEnd.set()
self.alive = False
self.stop()
#启动串口的函数
def start(self):
self.l_serial = serial.Serial()
self.l_serial.port = self.port
self.l_serial.baudrate = 115200
#设置等待时间,若超出这停止等待
self.l_serial.TImeout = 2
self.l_serial.open()
#判断串口是否已经打开
if self.l_serial.isOpen():
self.waitEnd = threading.Event()
self.alive = True
self.thread_read = None
self.thread_read = threading.Thread(target=self.FirstReader)
self.thread_read.setDaemon(1)
self.thread_read.start()
return True
else:
return False
技术专区
- LDO的PSRR测量及输入电压在测试
- 电子产品性能评估,快速进行温升测试
- 基于DSP和SD卡的生理信号数据采集系统设计
- matlab串口数据采集
- python串口接收数据