python3 串口通讯

来源:互联网 发布:贵州云博大数据 编辑:程序博客网 时间:2024/05/22 16:42
import serialimport threadingimport timex=serial.Serial('/dev/ttyUSB1',9600)# i=0def fasong():    while True:        #print("wocao")        myinput=input('shuru>')        myinput=myinput.encode(encoding="utf-8")        #print("you input "+myinput)        #time.sleep(1)        x.write(myinput)def jieshou():    myout=""    while True:       while x.inWaiting()>0:           myout+=x.read(1).decode()       if myout!="":            print(myout)            myout=""       #myout=x.read(14)      # myout="lll"       #time.sleep(1)if __name__== '__main__':     t1 = threading.Thread(target=jieshou,name="jieshou")     t2= threading.Thread(target=fasong, name="fasong")     t2.start()     t1.start()
1 0