'keyboard events are logged by the python script and powershell which runs the python script at the same time
I am running into an issue where I am trying to read/write to serial port from windows, its similar to teraterm/putty but trying to customize it for personal use. I am using powershell and using python script to read and write. I keep listening on keyboard events and wait for esc keyboard key release to quit the program. It works great. The problem is that once, I quit the program, it takes me back to powershell prompt and I get all the console commands written to serial port using the powershell prompt. For some reason, powershell also listens to the keyboard events while it runs the python script and writes it out soon the python script exits.
I use pynput and installed pyserial library.
from pynput import keyboard
import serial
Create serial connection:
serialPort = serial.Serial(port = com_port, baudrate=115200, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
When enter button is pressed, it writes to serial port.
if (key == keyboard.Key.enter) :
if (len(data_to_write) != 0) :
serialPort.write(data_to_write.encode('Ascii'))
The main thread listens for keyboard events.
while (True) :
with keyboard.Listener(on_release=key_release_event) as listener :
listener.join()
When the program exists, all the commands I write while the script was running gets written to powershell like below.
Solution 1:[1]
I couldn't find a way to stop the duplicate logging, hence instead of listening to keyboard events and parsing it, I used input python call which solved the purpose in my case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vasanth |