![](/img/trans.png)
[英]Not able to write and read from serial port using pyserial python script
[英]Unable to read from the Serial Port using PySerial
這是我的職責 我試圖將文字傳感器發送到COM端口,然后取回結果。
def serialportcommunication():
ser = serial.Serial(
port='COM5',
baudrate=115200
)
print('Writing SENSORS command to COM7')
ser.write(b"sensors")
time.sleep(4)
print('Reading 10 bytes from serial buffer')
read_val = ser.read(size=10)
print(read_val)
print('Done printing output')
我得到上面代碼的輸出:
將SENSORS命令寫入COM7
從串行緩沖區讀取10個字節
b'傳感器\\ r \\ n'
完成打印輸出
如果使用諸如Putty之類的終端程序在COM端口上執行命令“ sensors” ,則會從目標設備上看到一堵牆(示例輸出如下所示,不過我必須將大部分輸出塗白)。
我正在閱讀此文本,我想使用以上ser.read(size = ??)的teh命令在Python中閱讀,但我什么也沒讀回。
如何讀回去?
解決的評論:
Do you have to hit Enter when typing the command manually? Then you need to do the same here - ser.write(b"sensors\\r") (or maybe \\n, or \\r\\n). – jasonharper Yes, when i enter the command in Putty, I to hit enter. Thank you thank you. I added the '\\n in the end and it works now!!!! – Neil Dey
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.