-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSend-ArduinoSerial.ps1
28 lines (28 loc) · 980 Bytes
/
Send-ArduinoSerial.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Send-ArduinoSerial {
param ( [parameter(Mandatory = $true, ValueFromPipeline = $true)] [int[]] $byte )
#Find Arduino COM Port
$PortName = (Get-WmiObject Win32_SerialPort | Where-Object { $_.Name -match "Arduino" }).DeviceID
if ( $PortName -eq $null ) { throw "Arduino Not Found" }
#Create SerialPort and Configure
$port = New-Object System.IO.Ports.SerialPort
$port.PortName = $PortName
$port.BaudRate = "9600"
$port.Parity = "None"
$port.DataBits = 8
$port.StopBits = 1
$port.ReadTimeout = 2000 #Milliseconds
$port.open() #open serial connection
Start-Sleep -Milliseconds 100 #wait 0.1 seconds
$port.Write($byte) #write $byte parameter content to the serial connection
try {
#Check for response
if (($response = $port.ReadLine()) -gt 0)
{ $response }
}
catch [TimeoutException] {
"Time Out"
}
finally {
$port.Close() #close serial connection
}
}