如何使用 pyserial 将 shift 键发送到控制台

How to send shift key to a console using pyserial

我是 python 的新手,我正在使用 pyserial 连接到设备,所以,我的问题是:

1-我有办法将 shift 键盘按键事件发送到设备吗?我尝试搜索几乎所有内容,但没有找到解决方案,感谢您的帮助。

下面是我正在处理的代码,对于我的代码中的任何差异深表歉意(o_0)

import serial
import time
import serial.tools.list_ports


comlist = serial.tools.list_ports.comports()

connected = []

for ports in comlist:
connected.append(ports.device)

print("All ports on this Computer are: " + 
str(connected))
console = serial.Serial(ports[0])

while 1:
    console.write(b'+')
    time.sleep(0.2)
    console.write(b'1')
    time.sleep(0.2)

def read_from_console(console):
    bytes_to_be_read = console.inWaiting()

    while True:
        if bytes_to_be_read:
            output = 
console.read(bytes_to_be_read)
            return output.decode()
        else:
            return False
read_from_console(console)

您应该能够使用 ascii 发送 Shift 0x0E 和 0x0F 输出,请参阅维基百科的引用以供参考:

Shift Out (SO) and Shift In (SI) are ASCII control characters 14 and 15, respectively (0x0E and 0x0F).[1] These are sometimes also called "Control-N" and "Control-O".

https://en.wikipedia.org/wiki/Shift_Out_and_Shift_In_characters

我想你可以通过串口发送这些控制字符。这是一个例子:

# SI (Shift In)
console.write(b'14')

# some other key here that requires shift

# SO (Shift Out)
console.write(b'15')

希望对您有所帮助!