Py学习  »  Python

与python的串行通信

hernan castellanos • 6 年前 • 2062 次点击  

我在用python进行串行通信,我有一个与屏幕交互的开关,这个屏幕有两台电脑连接到它的HDMI端口,我想做的是把这个开关从HDMI1改为HDMI2端口我一直在尝试的是这样的:

import pyserial

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

我真的相信当我做以下事情的时候 connection.is_open

但是,我认为我的问题在于如何正确地组成通过函数向交换机发送命令的链 connection.write()

  • r来源!
  • s源1! (切换HDMI1输入(1:HDMI1,2:HDMI2, 3: HDMI3,4:显示端口,5:VGA/YPBPR/C-VIDEO)
  • 高密度1自动0! (选择音频源作为音频输入

我真的相信我真正的问题是不知道如何组合与函数一起发送的命令字符串 write() 另一个需要考虑的方面是,我使用的是Python2.7和windows。

事实上,我是一个新手,如果你能帮助我,我将非常感激。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55948
文章 [ 1 ]  |  最新文章 6 年前
Joran Beasley
Reply   •   1 楼
Joran Beasley    6 年前

只需设置一个超时并读取,直到发生这种情况

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