Powershell and Serial Ports – for the Sentry Project


Using Microsoft Powershell to send and receive info via the serial port was a suggestion I got from Robert at Intellipool (www.intellipool.se) when I was looking at creating a piece of hardware to integrate with Intellipool Network Monitor (INM) for the display of status information on monitored systems.

Robert suggested Powershell as a platform that would cover all the bases this small project has to cover so I took a look and as an initial step tested serial comms with the Sentry Hardware as follows

    Finding out what ports you have first !

at the Powershell prompt (PS>) run

PS> [System.IO.Ports.SerialPort]::getportnames()

COM1

where com1 is your port serial port as reported by Windows

    Opening the port

PS> $port= new-Object System.IO.Ports.SerialPort COM1,9600,None,8,two

note that the two relates to the stop bits for the Sentry Iasi link !! Change it to what your device requires (one normally)

PS> $port.open()
PS> $port.Write(“Hello world”)
PS> $port.Close()

Now in this case – there will be no output on the screen or matrices since they need specifically formatted commands but it illustrates the opening, writing and closure of the port under Powershell.

    so finally reading from a Serial Port under Powershell

PS> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
PS> $port.add_DataReceived({`$this is a handle to SerialPort. $_ is a pointer to SerialDataRecievedEventArgs})
PS> $port.Open()

and any other code you require to handle incoming info – a control loop until signalled complete !!

and there you have it. Simple serial comms for the Sentry project using some simple Powershell commands.

More in another article for programming the output to Sentry from INM …