与 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
答案是正确的。
但是,我认为我的问题在于通过函数 connection.write()
组成将命令发送到交换机的链的正确方法是什么
- r source!(读取当时的输入源)
- s source 1! ( 切换HDMI1输入(1:HDMI1,2:HDMI2,
3:HDMI3,4:DisplayPort,5:VGA/YPBPR/C-VIDEO))
- s hdmi1 auido 0! (选择音频源作为音频输入
HDMI1 (0: Emb,1: Ext1,2: Ext2,3:Ext3,4:Ext4,5:Ext5)
我真的相信我真正的问题是不知道如何编写与函数一起发送的命令字符串 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
我正在摆弄 python 进行串行通信,我有一个与屏幕交互的开关,而这个屏幕又有 2 台 PC 连接到它的 HDMI 端口,我想做的是将这个开关从 HDMI1 端口更改为 HDMI2 端口,我一直在尝试的是这样的:
import pyserial
connection = serial.Serial(
'COM1',
baudrate=9600,
bytesize=8,
patity='N',
stopbits=1
)
我真的相信连接已经建立,当我执行以下操作时 connection.is_open
答案是正确的。
但是,我认为我的问题在于通过函数 connection.write()
- r source!(读取当时的输入源)
- s source 1! ( 切换HDMI1输入(1:HDMI1,2:HDMI2, 3:HDMI3,4:DisplayPort,5:VGA/YPBPR/C-VIDEO))
- s hdmi1 auido 0! (选择音频源作为音频输入 HDMI1 (0: Emb,1: Ext1,2: Ext2,3:Ext3,4:Ext4,5:Ext5)
我真的相信我真正的问题是不知道如何编写与函数一起发送的命令字符串 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