Arduino通过PowerShell脚本与PC通信

Arduino通过PowerShell脚本与PC通信,powershell,arduino,port,Powershell,Arduino,Port,最近我写了一个项目,用Arduino和一个IR遥控器以及一个运行在PC上的PowerScript控制媒体播放器,通过串口接收Arduino的信息 问题是,只有当PowerShell从Arduino获得一些输入时,我如何编写PowerShell脚本才能执行某些操作。因为现在我在powershell脚本中有一个延迟循环,每次执行代码时,它都会读取并检查是否从Arduino获得了一些输入。但是,我怎么能不等待,而只在Arduino发送某些内容时触发PowerShell脚本呢 以下是我的PowerShe

最近我写了一个项目,用Arduino和一个IR遥控器以及一个运行在PC上的PowerScript控制媒体播放器,通过串口接收Arduino的信息

问题是,只有当PowerShell从Arduino获得一些输入时,我如何编写PowerShell脚本才能执行某些操作。因为现在我在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协议,那么我有一个PowerShell事件驱动接口的工作示例,通过使用名为Solid.Arduino()的.Net库与Arduino作为GPIO设备进行通信


您可以将Powershell订阅到SerialPort类公开的DataReceived事件处理程序。(我就是这样用我的Arduinos的)@Bluf你能再详细一点吗?我是PowerShell的绝对初学者。谢谢你也应该在这里发布相关代码,而不仅仅是用链接来回答。没有代码的答案往往会因为质量低而被删除。
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)}