Events from SerialPort

Jul 20, 2007 at 1:39 PM
Hello,

I am trying to use the eventing lirary to read data from a Serial Port using the System.IO.Ports.SerialPort class. I have sucessfully declared the object and connected the event library using connect-eventlistener. I get back the colllection of DataReceived events from the port, but am stuck in trying to read the actual data returned.

I am relatively new at PowerShell and .NET framework, any help would be appreciated.

Thanks,

Bob

PS - Here's a transcript:

PS C:\PoSHeventing> $port


BaseStream : System.IO.Ports.SerialStream
BaudRate : 1200
BreakState : False
BytesToWrite : 0
BytesToRead : 21
CDHolding : False
CtsHolding : False
DataBits : 8
DiscardNull : False
DsrHolding : False
DtrEnable : False
Encoding : System.Text.ASCIIEncoding
Handshake : None
IsOpen : True
NewLine :

Parity : None
ParityReplace : 63
PortName : COM1
ReadBufferSize : 4096
ReadTimeout : -1
ReceivedBytesThreshold : 1
RtsEnable : False
StopBits : One
WriteBufferSize : 2048
WriteTimeout : -1
Site :
Container :



PS C:\PoSHeventing> get-eventbinding port

VariableName EventName TypeName Listening
------------ --------- -------- ---------
port DataReceived SerialPort True


PS C:\PoSHeventing> $events
PS C:\PoSHeventing> $event

Occurred Source Name Args
-------- ------ ---- ----
7/20/2007 8:24:13 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...
7/20/2007 8:24:13 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...
7/20/2007 8:24:13 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...
7/20/2007 8:24:13 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...
7/20/2007 8:24:13 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...
7/20/2007 8:24:13 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...


PS C:\PoSHeventing> $event0

Occurred Source Name Args
-------- ------ ---- ----
7/20/2007 8:24:13 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...
Coordinator
Jul 23, 2007 at 7:13 PM
Hi Rob,

I don't really know a lot about working with serial ports, but I've attempted to translate a working c# into powershell script using this source: http://mark.michaelis.net/Blog/TheBasicsOfSystemIOPortsSerialPort.aspx

it appears the general idea is to wait on the datareceived events then read the waiting data by calling the ReadExisting method on the port object itself:

$data = $port.ReadExisting()

I've updated the old 0.5 release's wrapper functions that should make life a bit easier for you. Download the "eventhandlers.ps1" script from the 1.0 release page and try:

PS > . .\eventhandlers.ps1
PS > add-eventhandler (get-variable port) DataReceived { $port.ReadExisting() } 
PS > do-event
This should then wait on events and call the scriptblock when the "datareceived" event arrives, which reads from your port and outputs the data to the pipeline.

I've updated the homepage with some info on these helper functions.

Have fun!

Jul 24, 2007 at 4:08 PM
Oisin,

Thnaks for the reply. I am new at this myself. I see events, but cannot read the data.

If I copy the event to a variable, I see:

Occurred Source Name Args
-------- ------ ---- ----
7/24/2007 11:00:20 AM System.Management.Automati... DataReceived System.IO.Ports.SerialData...

There is a args.Chars property or attribute, but I cannot read the actal data returned.

Thanks,

Bob
Jul 24, 2007 at 5:30 PM
Oisin,

Got it working!

Here's the code:

. ./eventhandler.ps1

$port=new-Object System.IO.Ports.SerialPort COM1,1200,None,8,one
$port.open()
$port.Write(char13 + char10)

add-eventhandler (get-variable port) DataReceived { $port.ReadExisting() }

$port.write("s")
$port.Write(char13 + char10)

start-sleep -s 5


$res=do-events

$port.Close()

$res0

-----
Is there a way I could just use the get-event function and read the data from there? I see the event, but how is the raw data exposed?

The script is used to pull data in from a UPS connected to COM1. It only needs to send a CRLF and then "s" + CRLF to read the status ...

Thanks,

Bob