Powershell 可以将 PuTTY/Thonny/Arduino IDE 替换为串行终端吗?

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

我经常使用 Powershell 作为我的一体化命令行界面来处理我能管理的所有事情。我已经设法让 'cl' 在 Windows 中工作以在现有 Powershell 中编译 C 代码,而不是下载完整的 VisualStudio IDE 和他们试图让我使用的特殊 Powershell 终端。我通过编辑 Windows 中的环境变量来做到这一点。我还为 SSH 设置了 Powershell,这样我就可以直接从 Powershell 登录到我的 Raspberry Pis,而不需要 PuTTY。我一直在尝试实现的最新功能是让 Powershell 作为串行终端工作,以取代 Thonny(我用于 Raspberry Pi Pico)和 Arduino IDE 等东西。我已经设法弄清楚如何让它与这些命令进行一些基本通信:

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

但是,它有点蹩脚,因为我必须手动逐行读取,而且我似乎无法让它像其他具有 newline/no 行结束功能的串行终端一样运行以及使其他终端更加用户友好的其他功能。我在网上看到了一些声称可以将 Powershell 变成合适的串行终端的“脚本”,但我似乎无法让我玩过的几个“脚本”按照我的预期工作。有谁知道我怎么能做到这一点? “脚本”是执行此操作的唯一方法,还是可以配置 Powershell 使其表现得像功能更全的串行终端?

编辑:我在网上找到这个脚本,它接近我想要的,但它贯穿整个程序,从不停止等待我的输入。我想我需要类似于 Arduino IDE 中的“无行尾”的东西才能使其正常运行。如果有人可以建议我可以添加到此代码中以实现“无行尾”,我将不胜感激。

$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

我一直使用 plink 对我的 Pi 执行此操作。如果您可以通过 Putty 完成您需要的操作,那么 plink 将允许您在批处理文件或 Powershell 中自动执行它。

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

文档: https://documentation.help/PuTTY/plink-usage.html

此外,如果您可以忍受必须按 return 键,本文提供了一个更简单的解决方案。

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