Search code examples
powershellarduinoputtythonny

Can Powershell Replace PuTTY/Thonny/Arduino IDE as a Serial Terminal?


I often use Powershell as my all-in-one command line interface for everything I can manage. I have managed to get 'cl' in Windows to work to compile C code in stock Powershell instead of downloading the full VisualStudio IDE and the special Powershell terminal they try to make me use. I did this by editing the environment variables in Windows. I also have Powershell set up for SSH so I can log in to my Raspberry Pis right from Powershell instead of needing PuTTY. The lastest functionality I have been trying to achieve is to get Powershell to work as a serial terminal to replace things like Thonny (which I use for Raspberry Pi Pico) and the Arduino IDE. I have managed to figure out how to get it to do some basic communications with these commands:

$port= new-Object System.IO.Ports.SerialPort COM#,Baudrate,None,8,one
$port.open()
$port.WriteLine("some string")
$port.ReadLine()
$port.Close()

However, it is a little lame since I have to manually read lines one by one, and I can't seem to get it to act like other serial terminals where it has functionality like newline/no line ending and other features that make the other terminals more user friendly. I've seen a few "scripts" online that claim to make Powershell into a proper serial terminal but I can't seem to get the few I've played around with to work how I would expect. Does anyone know how I could accomplish this? Are "scripts" the only way to do this or is it possible to configure Powershell to behave like more fully featured serial terminals?

Edit: I found this script online which is close to what I want, but it runs through the entire program and never stops to wait for my input. I think I need something equivalent to "No line ending" in the Arduino IDE to get this to function correctly. If someone could suggest what I could add to this code to achieve "no line ending" I'd really appreciate it.

$port = New-Object System.IO.Ports.SerialPort
$port.PortName = "COM4"
$port.BaudRate = "9600"
$port.Parity = "None"
$port.DataBits = 8
$port.StopBits = 1
$port.ReadTimeout = 9000 # 9 seconds
$port.DtrEnable = "true"

$port.open() #opens serial connection

Start-Sleep 2 # wait 2 seconds until Arduino is ready

$port.Write("93c") #writes your content to the serial connection

try
{
   while($myinput = $port.ReadLine())
   {
   $myinput
   }
}

catch [TimeoutException]
{
# Error handling code here
}

finally
{
# Any cleanup code goes here
} 

$port.Close() #closes serial connection

Solution

  • I do this all the time to my Pi(s) using plink. If you can do what you need from Putty then plink will let you automate it in a batch file or Powershell.

    Download: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

    Documentation: https://documentation.help/PuTTY/plink-usage.html

    Also, this article provides an even simpler solution if you can live with having to hit the return key.

    https://batchloaf.wordpress.com/2013/02/12/simple-trick-for-sending-characters-to-a-serial-port-in-windows/