1. 程式人生 > >pyserial模組使用記錄

pyserial模組使用記錄

pyserial是python下讀寫串列埠的模組,專案正好需要使用,有個坑,需要填一下。

正常情況下,只需要

import serial
ser = serial.Serial(port='/dev/ttyUSB1', baudrate=9600,
                      stopbits=serial.STOPBITS_ONE,
                      bytesize=serial.EIGHTBITS,
                      parity=serial.PARITY_NONE
                      )

這樣就可以對串列埠進行讀寫了。但是今天用在TX2上面,就報錯了:IOError: [Errno 32] Broken pipe

同樣的程式碼在臺式機上面使用,就沒有問題。而且之前也在TX2上面用過。在搜尋後,經過嘗試,需要在上述配置裡面,再增加兩項rtscts=True,dsrdtr=True,即:

import serial
ser = serial.Serial(port='/dev/ttyUSB1', baudrate=9600,
                      stopbits=serial.STOPBITS_ONE,
                      bytesize=serial.EIGHTBITS,
                      parity=serial.PARITY_NONE,
                      rtscts=True,dsrdtr=True,
                      )

這樣才能正常開啟使用。