Mac Man
Mac Man

Reputation: 171

How to send hexadecimal commands to a monitor over a Serial Port with PowerShell

Okay this is a bit of a weird one but due to my complete lack of knowledge on how to use Serial Ports or PowerShell i couldn't think of anywhere else to go.

What I'm trying to do is send basic commands to a monitor that has a RS232 port on it that can be used to control the properties of the monitor, i.e. Brightness, Contrast, Backlight etc.

I'm attempting to use PowerShell to do this for testing purposes. I can create the $port in PowerShell and assign it to the relevant COM# that the monitor is connected to but I'm at a loss as to how to actually send the command to it as it must be Hexadecimal for the controller on the monitor to understand it. The monitor is capable of returning an acknowledgement using the same Hex layout but I'm unable to find a way of showing that response on the Powershell console. This is what I have been able to get so far.

PS C:\Users\Kingdel> [System.IO.Ports.SerialPort]::getportnames()
COM1
COM2
COM3
COM4
COM5
COM6
PS C:\Users\Kingdel> $port= new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one
PS C:\Users\Kingdel> $port.open()
PS C:\Users\Kingdel> $port.WriteLine("0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94")
PS C:\Users\Kingdel>

Anyone able to point me in the right direction as to how I can send this command to the monitor and to view the returned acknowledgement.

I am open to trying different terminals, I have tried PuTTy and Termite and neither of them were successful as far as I can tell.

Upvotes: 2

Views: 2310

Answers (1)

Peter Kay
Peter Kay

Reputation: 996

That's a really good question. Maybe I can help with this.

The SerialPort.WriteLine() method takes in a string to write to the output buffer, so using this, you're essentially sending an argument of strings.

To send something over to the [System.IO.Ports.SerialPort] object, you need to use SerialPort.Write() with a Byte[] argument. The Write() method can take in a number of bytes to the serial port using data from a buffer.

You also need to send it three arguments which are buffer Byte[], offset Int32, and a count Int32. So in your case, you can do the following:

[Byte[]] $hex = 0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94
$port.Write($hex, 0, $hex.Count)

The buffer argument is the array that contains the data to write to the port, offset is the zero-based byte offset in the buffer array at which to begin copying the bytes to the port, and the count is the number of bytes to write.

Upvotes: 1

Related Questions