Arduino通过PowerShell脚本与PC通信

时间:2017-06-11 09:33:51

标签: powershell arduino port

最近我编写了一个项目来控制使用Arduino和IR远程的媒体播放器以及在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
}

1 个答案:

答案 0 :(得分:1)

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

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/