Bootstrap

多线程串口

import threading
import time
import serial
import os
import datetime
port= serial.Serial(‘com1’,9600)
if port.isOpen():
port.close()
port.open()

f= open(‘Serial.txt’,‘w+’)
f.write(“nihao”)
class threadDemo(threading.Thread):
def init(self):
threading.Thread.init(self)
def run(self):
while True:
str1= port.read(1)
sstr = str1.decode(‘utf-8’)
f.write(sstr)
print(sstr)
port.write(sstr.encode())

for index in range(1):
thread = threadDemo()
thread.start()
byte_write = ‘hello’.encode(‘utf-8’)
port.write(byte_write)#b’helloworld’

;