1

I am trying to send ASCII code (for ENTER, SPACE, SHIFT, F1, F2, F3 and F4 keys) serially in Powershell

port= new-Object System.IO.Ports.SerialPort COM1,115200,None,8,one
$port.open()

# Carriage return - ENTER 
$port.WriteLine("`r")
Start-Sleep -Milliseconds 500
$port.ReadExisting()

[Byte[]] $request = 13
$port.Write($request)
Start-Sleep -Milliseconds 500

$port.ReadExisting()

$port.Close()

Here in above code, I am sending Carriage Return serially. When I use WriteLine then it works but it fails when I am trying with Write.

1 Answer 1

1

Often CR+LF work as a command terminator on serial ports, so it probably doesn't pick up what you are doing unless you WriteLine. To send a CR+LF you should try

$port.Write("`r`n") 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. What about these keys (SPACE, SHIFT, F1, F2, F3 and F4 ) ?
Space should be trivial. The others aren't characters, they are keys/scan codes. That's something different. It's up to the receiving end how to encode those, so please check the documentation for whatever device you are communicating with.
My requirement is that I want to send keystrokes from Powershell script serially.
I understand your requirement, but you write characters on the the serial port, not scan codes, so there is no way to write F1. Your only hope is that the receiving end has some way to receive key presses, so again: Check the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.