Writing and Reading info from Serial Ports

Writing and Reading info from Serial Ports

  • Comments 13

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

  • readline or getportnames output to txt file? can out-file be used?

  • I wanted a function that would continuously read from the serial port with the option of writing to a text file as well. from various websites I created the following;

    function get-serialport {

    param(

       [parameter(position=0, Mandatory=$false, ValuefromPipeline=$true)]

       [String]$writetofile

       )

    if($writetofile -ne ""){

       $fstream = [System.IO.StreamWriter] $writetofile

       $fn = Get-ChildItem $writetofile | select fullname

    }

    # modified from the reading from a serial port on blogs.msdn.com/.../writing-and-reading-info-from-serial-ports.aspx

    $scom = [System.IO.Ports.SerialPort]::getportnames()

    $port= new-Object System.IO.Ports.SerialPort $scom,115200,None,8,one

    $port.open()

    while($true){

       # to break out of the loop, taken from powershell.com/.../8696.aspx

       if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {

           Write-Host "Exiting now, don't try to stop me...." -Background DarkRed

           $port.Close()

           if($writetofile -ne ""){

               $fstream.Close()

               Write-host $fn.fullname "saved!" -Background darkGreen

           }

           break;

       }

       else {

           $so = $port.readline()

           Write-Host $so

           if($writetofile -ne ""){

               $fstream.WriteLine($so)

           }

       }

    }

    #end of function

    }

    I hope this helps others

  • I needed run a continuous capture from a serial port. I wrote the following function that also has an option to save the output to a file.

    function get-serialport {

    param(

       [parameter(position=0, Mandatory=$false, ValuefromPipeline=$true)]

       [String]$writetofile

       )

    if($writetofile -ne ""){

       $fstream = [System.IO.StreamWriter] $writetofile

       $fn = Get-ChildItem $writetofile | select fullname

    }

    # modified from the reading from a serial port on blogs.msdn.com/.../writing-and-reading-info-from-serial-ports.aspx

    $scom = [System.IO.Ports.SerialPort]::getportnames()

    $port= new-Object System.IO.Ports.SerialPort $scom,115200,None,8,one

    $port.open()

    while($true){

       # to break out of the loop, taken from powershell.com/.../8696.aspx

       if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {

           Write-Host "Exiting now, don't try to stop me...." -Background DarkRed

           $port.Close()

           if($writetofile -ne ""){

               $fstream.Close()

               Write-host $fn.fullname "saved!" -Background darkGreen

           }

           break;

       }

       else {

           $so = $port.readline()

           Write-Host $so

           if($writetofile -ne ""){

               $fstream.WriteLine($so)

           }

       }

    }

    #end of function

    }

    I hope it's useful to others :)

Page 1 of 1 (13 items)