python 實現linux串口收發數據源碼

使用python實現在linux平臺收發串口數據,源碼如下:

uart.py

#!/usr/bin/python
import serial,time,thread

ser=serial.Serial('/dev/ttyS1', timeout=1)
print ser.portstr

def recv_func(sec):
    global ser
    print 'recv'
    while True:
        readbuff=ser.read(10)
        print ('recv ',readbuff,'\n')
        time.sleep(sec)

if __name__ == '__main__':
    thread.start_new_thread(recv_func,(2,))
#    thread.start_new(recv_func())
    print 'main'
    i = 0
    try:
        while True:
            ser.write(b'hello')
            time.sleep(2)
            i += 1
            print i
    finally:
        ser.close()

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章