2015-06-08 68 views
-1
import time 
import serial 

# configure the serial connections (the parameters differs on the device you are connecting to) 
ser = serial.Serial(
    port='/dev/ttyS0', 
    #port='/dev/ttyACM0', 
    baudrate=115200, 
    parity=serial.PARITY_ODD, 
    #stopbits=serial.STOPBITS_TWO, 
    #bytesize=serial.SEVENBITS 
) 

ser.isOpen() # returns true 
time.sleep(1); 

ser.write("some_command \n") 
ser.close() 

我有一塊嵌入式主板。它有一個連接到我的電腦的串行端口。我正在運行腳本以訪問此串行端口並運行一些特定於電路板的命令。使用python的pyserial模塊時出錯

我的問題

我打開我的串口(使用小型機在linux)分別,然後運行上面的腳本,它的工作原理。如果我沒有單獨打開串口,腳本不起作用。

+1

什麼是機會Minicom是比你的端口上的程序設置不同的參數?這很可能是你的問題, – jsbueno

+0

不,我交叉檢查它。不是這種情況。這是我的串口設置 Serial (port ='/ dev/ttyS0',baudrate = 115200,bytesize = 8,parity ='O',stopbits = 1,timeout = None,xonxoff = False,rtscts = False,dsrdtr = False) –

+0

腳本如何不起作用?它會引發異常還是掛起? –

回答

-1

嘗試

ser.write("some_command \n".encode()) 

或者嘗試

ser.write(bytes(b"some_command \n"))