Manny Gonzalez
Manny Gonzalez

Reputation: 23

How to send shift key to a console using pyserial

I'm new to python and i'm connecting to a device using pyserial so, my question is:

1-Is there a way for me to send the shift keyboard key event to the device? i tried searching almost everything but haven't find a solution, thanks for the help.

Bellow is the code i'm working on, sorry for any discrepancies on my code (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)

Upvotes: 0

Views: 848

Answers (1)

Alexander Collins
Alexander Collins

Reputation: 192

You should be able to send Shift 0x0E for in and 0x0F for out with ascii, see this quote from wikipedia for reference:

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

I would presume you can to send these control characters over serial. Here is an example:

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

# some other key here that requires shift

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

Hope this helps!

Upvotes: 1

Related Questions