raulx222
raulx222

Reputation: 137

Arduino communicate with PC via PowerShell script

Recently I've written a project in order to control media players using Arduino and an IR remote and a PowerScript running on PC to recevie the information from Arduino via serial port.

The problem is that how can I script the PowerShell in order to do something only when it get some input from Arduino. Because right now I have a loop with delay in powershell script and every time when the code is executed, it reads and check if it got some input from Arduino. But how can I do to not wait but to trigger the PowerShell script only when Arduino sends something?

Here's my PowerShell script:

$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
}

Upvotes: 2

Views: 1964

Answers (1)

user22080
user22080

Reputation: 41

If you use Firmata protocol on Arduino then I have working example of PowerShell event driven interface to communicate with Arduino as a GPIO device by using a .Net library called Solid.Arduino (https://github.com/SolidSoils/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/

Upvotes: 1

Related Questions