我经常使用Powershell作为我所能管理的所有东西的全合一命令行界面。我已经设法使'cl‘在Windows中工作,以编译C代码在库存的Powershell,而不是下载完整的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()然而,它有点蹩脚,因为我不得不一个接一个地手动读取行,而且我似乎无法让它像其他串行终端那样,它具有诸如换行符/无行结束之类的功能,以及其他使其他终端更加友好的功能。我在网上看到了一些“脚本”,这些脚本声称将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发布于 2022-03-01 09:38:27
我一直在用plink对我的Pi做这件事。如果您可以从Putty中执行所需的操作,则plink将允许您在批处理文件或Powershell中自动执行。
下载:https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
文档:https://documentation.help/PuTTY/plink-usage.html
此外,本文还提供了一个更简单的解决方案,如果您可以接受按返回键的话。
https://stackoverflow.com/questions/71303723
复制相似问题