10

I need to know how I can continuously read data in from the COM port and dump it to a file using Windows Powershell. While I am reading data in, I also need to monitor the data being read in and, depending on what the last line that was read, write data to the COM port.

To open the COM port in Powershell, I am doing this:

[System.IO.Ports.SerialPort]::getportnames()
$port= new-Object System.IO.Ports.SerialPort COM3,115200,None,8,one
$port.open()

To read data in to the COM port, I am doing this:

$line=$port.ReadLine()

My initial thought was to have the main Powershell script open the COM port. Then it would start a background/child task to continuously read data in from COM port and dump it to a file. While that child task is running, the parent would then continuously monitor the file and write to the COM port when needed.

When I tried to do that, the child could not read data in from the COM port because the parent had it open and the child did not inherit that permission from the parent.

Any ideas on how I can accomplish this?

6
  • can you simply read the $line variable each time its created and do what you need to at that time if it matches the criteria while simultaneously logging it via Out-File or something for logging purposes? Commented Jul 8, 2015 at 18:25
  • I'm not real good with watchers, but I know you can watch a folder for file changes, can you watch a com port for activity, and react to it when it happens? Commented Jul 8, 2015 at 18:31
  • I just set up a while loop that would read in a line, log it to a file, and then did some checks on that line to determine if it needed to write to the COM port. Unfortunately, Powershell seems to have problems reading lines that do not end in a newline character or carriage return, such as a linux command line prompt. ReadLine() fails to read in the "prompt" lines. Any ideas on how to get around that? Commented Jul 30, 2015 at 16:25
  • headinabook did you find solution ? please share... I am also trying to read continuously from a serial port. Commented Oct 23, 2017 at 12:59
  • @AkshayPatole Do you need to use Windows Powershell, or can you use another program? Commented Oct 23, 2017 at 17:47

2 Answers 2

12

Simple answer: while loop. Calling functions to decide when to write data. Use child task and scripts to handle/process/get data but keep the communication in the same task/script. I used this code to read from my Ardunio:

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

function read-com {
    $port= new-Object System.IO.Ports.SerialPort $COM,9600,None,8,one
    $port.Open()
    do {
        $line = $port.ReadLine()
        Write-Host $line # Do stuff here
    }
    while ($port.IsOpen)
}
Sign up to request clarification or add additional context in comments.

1 Comment

If multiple COM ports are returned by getportnames(), $port.Open() will fail. This also does not address reading lines that do not end in a newline character or carriage return, such as a linux command line prompt.
-1

You can use the function bellow.
Also check ComPST.ps1 for more functions and application.


function read-com {
    try{
        $line = $port.ReadExisting()
        if($line) {
            Write-Host -NoNewline $line
        }
    } catch [System.Exception] {
        # Do some error handling
    }
}

do {
    read-com
} while ($port.IsOpen)

1 Comment

this makes a unhandeled tr/catch, it dupicates an existing solution, and vaguely links to external source with not much explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.