Python使用线程来接收串口数据的示例
如下所示:
#!/usr/bin/envpython importserial importtime importthread classMSerialPort: message='' def__init__(self,port,buand): self.port=serial.Serial(port,buand) ifnotself.port.isOpen(): self.port.open() defport_open(self): ifnotself.port.isOpen(): self.port.open() defport_close(self): self.port.close() defsend_data(self,data): number=self.port.write(data) returnnumber defread_data(self): whileTrue: data=self.port.readline() self.message+=data if__name__=='__main__': mSerial=MSerialPort('/dev/ttyACM0',9600) thread.start_new_thread(mSerial.read_data,()) whileTrue: time.sleep(1) printmSerial.message print'nextline'
以上这篇Python使用线程来接收串口数据的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。