How to exit Plink (command line PuTTY) after sending command from PowerShell script?

795 Views Asked by At

I am communicating with a sensor via serial (COM port) connection. The sensor outputs data only when receiving a specific input string. I want to write a script to repeatedly send the input and save the outputs to a text file.

I am using Plink as serial terminal, and PowerShell for scripting.

I have tried the following:

Write-Output $P |
    & $plinkpath -batch -serial COM4 -sercfg 38400,8,n,1,N |
    Out-File -filepath $outputfile -Append

The serial connection is made successfully, and sensor output is added to the text file once. However the script hangs here and never terminates so I am not able to loop this command and save multiple data points. I believe the problem is the Plink session is still open and waiting for input, so this line never finishes executing. Any way to close Plink and let my PowerShell script continue?

1

There are 1 best solutions below

0
Martin Prikryl On

The only way is to kill Plink once you get the output you need. I do not have PowerShell example, but maybe this will at least push you in the right direction.

But I have a batch file example:
Send commands via COM port using plink and exit