I don't have any experience in serial port communications, but I need to come up with a PowerShell tool to query a device (display) connected through my USB port via RS232 (connected using Trendnet /Prolific USB-to-serial adapter)
I use PowerShell v 5.0
I wouldn't expect it to be a very complicated script.
Here is what I currently have
$p = Get-ItemProperty -Path "hklm:\hardware\devicemap\serialcomm\" -name "*prolific*" | Select-Object -ExpandProperty \Device\ProlificSerial*
$PowerStatus = "query power status"
$port= new-Object System.IO.Ports.SerialPort $p,9600,None,8,one
$port.DTREnable = "true"
$port.RtsEnable = "true"
$port.open()
$port.WriteLine($powerstatus)
# reading operation
$line = $port.ReadLine()
Write-Host $line
When I run this, Powershell hangs. Sometimes I get either "access to the COM port is denied" or
Exception calling "ReadLine" with "0" argument(s): "The port is closed."
Either way, I don't get any response and have to restart PowerShell every time :)
Apparently I am able to write to the port. I am able to power the display on and off from PowerShell.
When I check the port status after I write the "query power status" command, it looks like this
PS C:\WINDOWS\system32> $port
BaseStream : System.IO.Ports.SerialStream
BaudRate : 9600
BreakState : False
BytesToWrite : 0
BytesToRead : 10
CDHolding : False
CtsHolding : False
DataBits : 8
DiscardNull : False
DsrHolding : False
DtrEnable : True
Encoding : System.Text.ASCIIEncoding
Handshake : None
IsOpen : True
NewLine :
Parity : None
ParityReplace : 63
PortName : COM3
ReadBufferSize : 4096
ReadTimeout : -1
ReceivedBytesThreshold : 1
RtsEnable : True
StopBits : One
WriteBufferSize : 2048
WriteTimeout : -1
Site :
Container :
I see there are bytes sitting in the buffer but cannot get a response.
It looks like the port is still being used by the writing operation? so when I try to read, the port is busy
Please let me know what I am doing wrong. Thank you
This will probably fix it:
(Defaults are shown in brackets.)