简体   繁体   English

与python的串行通信

[英]Serial communication with python

I'm fiddling a bit with python for serial communication, I have a switch that interacts with a screen and this screen in turn has 2 PCs connected to its HDMI ports, what I want to do is make this switch change from HDMI1 to HDMI2 port what I've been trying looks like this:我正在使用 python 进行串行通信,我有一个与屏幕交互的开关,而该屏幕又将 2 台 PC 连接到其 HDMI 端口,我想要做的是将此开关从 HDMI1 更改为 HDMI2 端口我一直在尝试的东西是这样的:

import pyserial

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

I really believe that the connection is well established, when I do the following connection.is_open the answer is True.我真的相信连接已经建立,当我执行以下connection.is_open ,答案是 True。

However, I think that my problem is in what is the correct way to compose the chain that sends the commands to the switch through the function connection.write()但是,我认为我的问题在于通过函数connection.write()将命令发送到交换机的链的正确组合方式是什么

  • r source! r 来源! ( Reading the input source at that time ) (当时读取输入源)
  • s source 1! s 来源 1! ( Switch HDMI1 input(1:HDMI1,2:HDMI2, 3:HDMI3,4:DisplayPort,5:VGA/YPBPR/C-VIDEO) ) (切换HDMI1输入(1:HDMI1,2:HDMI2,3:HDMI3,4:DisplayPort,5:VGA/YPBPR/C-VIDEO))
  • s hdmi1 auido 0! s hdmi1 音频 0! ( Choice of audio source as audio input HDMI1 (0: Emb,1: Ext1,2: Ext2,3:Ext3,4:Ext4,5:Ext5) ) (选择音源作为音频输入HDMI1(0:Emb,1:Ext1,2:Ext2,3:Ext3,4:Ext4,5:Ext5))

I really believe that my real problem is not knowing how to compose the command string that is sent with the function write() other aspect to consider is that I am using python 2.7 and windows.我真的相信我真正的问题是不知道如何write()与函数write()一起发送的命令字符串,要考虑的其他方面是我使用的是 python 2.7 和 windows。

the truth is that I am a newbie, if you can help me I will be very grateful.事实是,我是一个新手,如果你能帮助我,我将非常感激。

just set a timeout and read until that happens只需设置超时并阅读直到发生

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