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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)