簡體   English   中英

與python的串行通信

[英]Serial communication with python

我正在使用 python 進行串行通信,我有一個與屏幕交互的開關,而該屏幕又將 2 台 PC 連接到其 HDMI 端口,我想要做的是將此開關從 HDMI1 更改為 HDMI2 端口我一直在嘗試的東西是這樣的:

import pyserial

connection = serial.Serial(
        'COM1',
        baudrate=9600,
        bytesize=8,
        patity='N',
        stopbits=1
    )

我真的相信連接已經建立,當我執行以下connection.is_open ,答案是 True。

但是,我認為我的問題在於通過函數connection.write()將命令發送到交換機的鏈的正確組合方式是什么

  • r 來源! (當時讀取輸入源)
  • s 來源 1! (切換HDMI1輸入(1:HDMI1,2:HDMI2,3:HDMI3,4:DisplayPort,5:VGA/YPBPR/C-VIDEO))
  • s hdmi1 音頻 0! (選擇音源作為音頻輸入HDMI1(0:Emb,1:Ext1,2:Ext2,3:Ext3,4:Ext4,5:Ext5))

我真的相信我真正的問題是不知道如何write()與函數write()一起發送的命令字符串,要考慮的其他方面是我使用的是 python 2.7 和 windows。

事實是,我是一個新手,如果你能幫助我,我將非常感激。

只需設置超時並閱讀直到發生

import pyserial
connection = serial.Serial(
        'COM1',
        baudrate=9600,
        bytesize=8,
        patity='N',
        stopbits=1,
        timeout=1 # could probably be less
    )
# maybe .... are there some docs for whatever switch you are using?
connection.write("r source!\n")
# you might need connection.write(b"r source!")
print(connection.read(1000)) # try and read 1000 bytes or until timeout ocurres(1 sec)
# if you knew what the terminal character the device sends is you could just readuntil that character

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM