gpt4 book ai didi

powershell - Arduino 通过 PowerShell 脚本与 PC 通信

转载 作者:行者123 更新时间:2023-12-02 23:56:28 26 4
gpt4 key购买 nike

最近我编写了一个项目,以便使用 Arduino 和一个 IR Remote 和一个在 PC 上运行的 PowerScript 来控制媒体播放器,以通过串行端口从 Arduino 接收信息。

问题是我如何编写 PowerShell 脚本以便仅在它从 Arduino 获得一些输入时才执行某些操作。因为现在我在powershell脚本中有一个延迟循环,每次执行代码时,它都会读取并检查它是否从Arduino获得了一些输入。但是只有在 Arduino 发送一些东西时,我才能不等待而是触发 PowerShell 脚本呢?

这是我的 PowerShell 脚本:

$console = $host.UI.RawUI
$console.BackgroundColor = "darkgreen"
$console.ForegroundColor = "black"
$size = $console.WindowSize
$size.Width = 15
$size.Height = 2
$console.WindowSize = $size
$buffer = $console.BufferSize
$buffer.Width = 15
$buffer.Height = 2
$console.BufferSize = $buffer

Write-Host "PC Media Remote"

function IRinputText {
Param([string]$serialread,[string]$combo,[string]$key)
Process{If ($IRbuffer -like ("*"+$serialread)) {IRbufferReset;$wshell.SendKeys($combo+"{"+$key+"}")}}
}
function IRinputHEX {
Param([string]$serialread,[string]$key)
Process{If ($IRbuffer -like ("*"+$serialread)) {IRbufferReset;$wshell.SendKeys([char]+$key)}}
}
function IRbufferReset {
$Script:IRbuffer=""
$Script:IRbuffer= $Script:IRbuffer.PadLeft(15,'0')
}

IRbufferReset

$port= new-Object System.IO.Ports.SerialPort COM4,9600,None,8,one
$wshell = New-Object -ComObject wscript.shell;
$port.open()
while($true)
{
$inputvar= $port.ReadExisting() -replace "`n","" -replace "`r",""
If(($inputvar -ne "")){
$IRbuffer= If($IRbuffer.Length -ge $inputvar.Length){$IRbuffer.Substring($inputvar.Length) }
$IRbuffer= $IRbuffer + $inputvar
}
If($IRbuffer.Length -gt 15){$IRbuffer= $IRbuffer.Substring($IRbuffer.Length - 15);}
IRinputText -serialread "CH+" -combo "^" -key "UP"
IRinputText -serialread "CH-" -combo "^" -key "DOWN"
IRinputText -serialread "100+" -combo "^" -key "LEFT"
IRinputText -serialread "200+" -combo "^" -key "RIGHT"
IRinputText -serialread "CH" -key "f"
IRinputHEX -serialread "VOL+" -key "0xAF"
IRinputHEX -serialread "VOL-" -key "0xAE"
IRinputHEX -serialread "PLAY/PAUSE" -key "0xB3"
IRinputHEX -serialread "PREV" -key "0xB1"
IRinputHEX -serialread "NEXT" -key "0xB0"
IRinputHEX -serialread "EQ" -key "0xAD"
If ($IRbuffer -like ("*1532")) {IRbufferReset;Stop-Computer}

Start-Sleep -m 20
}

最佳答案

如果您在 Arduino 上使用 Firmata 协议(protocol),那么我有 PowerShell 事件驱动接口(interface)的工作示例,通过使用名为 Solid.Arduino (https://github.com/SolidSoils/Arduino) 的 .Net 库作为 GPIO 设备与 Arduino 进行通信

add-type -path '.\Documents\WindowsPowerShell\Solid.Arduino.dll'
$connection = New-Object Solid.Arduino.SerialConnection("COM4",[Solid.Arduino.SerialBaudRate]::Bps_57600)
$session = New-Object Solid.Arduino.ArduinoSession($connection, 2000)
$session.SetDigitalPinMode(4,[Solid.Arduino.Firmata.PinMode]::DigitalInput)
#
# Define the function that is called when a digitial pin event happens
#
function inputevent($event){
#Write-Host "Something happened on port $($event.value.port) Type $($event.value.pins)"
if($event.value.pins -band 4) #using binary AND incase multiple inputs are activated
{
# put code here to switch ATEM Aux or VideoHub routing
Write-host "Cam 1 preview selected"
}
}
#
# set up the event to montor digital pin state change
#
$session.SetDigitalReportMode(0,$true) #enable events for pins on port 0
Unregister-Event eventBitChange -ErrorAction SilentlyContinue #incase we are re-running the script
$ArduinoEvent = Register-ObjectEvent -InputObject $session -EventName DigitalStateReceived -SourceIdentifier eventBitChange -Action {inputevent($eventArgs)}

https://ianmorrish.wordpress.com/category/v-ise/arduino/

关于powershell - Arduino 通过 PowerShell 脚本与 PC 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44482341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com