Welcome to MSDN Blogs Sign in | Join | Help

Writing and Reading info from Serial Ports

PSMDTAG:FAQ: How do I read/write data from a Serial Port?

Writing to a Serial Port

PS> [System.IO.Ports.SerialPort]::getportnames()
COM3
PS> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
PS> $port.open()
PS> $port.Write("Hello world")
PS> $port.Close()


Reading from a Serial Port

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()

Enjoy!

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

 

PSMDTAG:DOTNET: SerialPort

Published Thursday, August 31, 2006 5:17 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Writing and Reading info from Serial Ports

Is there a cmdlet or other voodoo to make PowerShell a usable VT100 serial terminal emulator? It doesn't need to be fancy. It just needs to be good enough to get a router or other network device configured over a serial connection and up on the network.

Hypertrm.exe is gone from Vista and this is a scenario that is currently not covered by telnet nor the "Phone and Modem options" applet. <http://windowshelp.microsoft.com/Windows/en-US/Help/e864444f-44d5-4206-a848-dcd6972594931033.mspx>.

I guess there's always Tera Term <http://en.wikipedia.org/wiki/Tera_Term>, but here's hoping you've got a solution.

Monday, January 29, 2007 12:53 AM by Brian Reiter

# re: Writing and Reading info from Serial Ports

Why does this read code crash on 1.0?  I am using the code verbatim (minus changing the com port from COM3 to COM7).

(I haven't tried the write code since I am trying to read from a barcode scanner.)

Everything seems to work well until I scan a barcode, then I receive this error and the window promptly closes.

An error has occurred that was not properly handled. Additional information is s

hown below. The Windows PowerShell process will exit.

Wednesday, May 09, 2007 11:20 AM by DarthHack

# re: Writing and Reading info from Serial Ports

I had the same problem with the read code. I am trying to write a simple script to send me an email when the modem detects an incoming phone call. As soon as I dial the phone to make the modem ring the powershell windows crashes.

If only I still had my DOS ProComm software!

Saturday, June 07, 2008 12:22 AM by Frank Smith

# re: Writing and Reading info from Serial Ports

Same problem here - I can easily read from a COM port as long as no data are received. As soon as anything is retrieved, PowerShell crashes - or rather an .NET runtime 2.0 Error occurs. How can I  come closer to solving this problem?

Saturday, July 19, 2008 1:09 PM by Jakob Bindslet

# re: Writing and Reading info from Serial Ports

and here we are...6 months later and the given code still crashes power shell!

Tuesday, February 24, 2009 8:30 AM by yo mama

# re: Writing and Reading info from Serial Ports

Reading from a Serial Port

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

PS> $port.Open()

PS> $port.ReadLine()

Writing to a Serial Port

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

COM3

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

PS> $port.open()

PS> $port.WriteLine("Hello world")

PS> $port.Close()

Sunday, April 19, 2009 5:24 PM by Joe

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker