-1

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

1 Answers1

0

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/

rabbit
  • 95
  • 7
  • 2
    Thanks for taking the time to share this solution. I was hoping I could get things natively in Powershell but I guess I'll just have to try this or continue using the other programs. It doesn't seem like there's going to be much interest in this question so I voted this the best answer. Thank you for the help. – Jayson Gazeley Mar 02 '22 at 12:53
  • @JaysonGazeley Did you read the article I sent also? It has promise for what you want to do, just port it to Powershell. – rabbit Mar 02 '22 at 14:03
  • I did take a look, I was a little confused by what was going on initially, but I will try and play around with it to see if I can get it to work with my Arduino program – Jayson Gazeley Mar 03 '22 at 15:51
  • this is a bit late, but anyway you can do this with ps (1) open com port (2)start do loop (3)let ps ask for input (4)send input to com port (5)let the loop run untill your string is the same as exit (or another stop word). – Chris Vermeijlen Jun 17 '22 at 11:24