Writing and Reading info from Serial Ports

Writing and Reading info from Serial Ports

  • Comments 10

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.WriteLine("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.Open()
PS> $port.ReadLine()

For information about how to read from a serial port in an asynchronous manner in PowerShell V2, use the DataReceived event on the port object with the Register-ObjectEvent cmdlet.

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

Leave a Comment
  • Please add 3 and 3 and type the answer here:
  • Post
  • 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.

  • 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.

  • 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!

  • 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?

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

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

  • It is very good and following this information i can send words|commands through rs232 but how can i send "Enter" after command???

  • You can use `r at the end of the string for return:

    $port.Write(“some_command  `r”)

  • can powershell be used to retrieve an unknown port number? If so, what would the command for that be?

  • can I program commandshell into a vb.net windows app to write to the serial port?

Page 1 of 1 (10 items)