-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWinArduino.psm1
More file actions
176 lines (162 loc) · 4.97 KB
/
WinArduino.psm1
File metadata and controls
176 lines (162 loc) · 4.97 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
$GLOBAL:arduinoidepath = 'C:\Program Files (x86)\Arduino\arduino_debug.exe'
$GLOBAL:arduinoport = 'COM3'
function Get-ArduinoIdeExePath {
$GLOBAL:arduinoidepath
}
function Set-ArduinoIdeExePath {
param(
[Parameter(Mandatory=$true, Position=0)]
[ValidateScript({Test-Path $_})]
[string] $Path
)
$GLOBAL:arduinoidepath = $Path
}
function Get-ArduinoPort {
$GLOBAL:arduinoport
}
function Set-ArduinoPort {
param(
[Parameter(Mandatory=$true, position=0)]
[string] $Port='COM3'
)
$GLOBAL:arduinoport=$port
}
function Invoke-ArduinoIdeVerify {
param(
[Parameter(Mandatory=$true, Position=0)]
[ValidateScript({Test-Path $_})]
[string] $Path
)
& $arduinoidepath --verify $path
}
function Invoke-ArduinoIdeUpload {
param(
[Parameter(Mandatory=$true, Position=0)]
[ValidateScript({Test-Path $_})]
[string] $Path
)
& $arduinoidepath --upload $path --port $arduinoport
}
$GLOBAL:ArduinoSerialConnections = new-object System.Collections.Arraylist
function Connect-ArduinoSerial {
param(
[string] $Port = $GLOBAL:arduinoport,
[int] $BaudRate = "9600",
[string] $Parity = "None",
[int] $DataBits = 8,
[int] $StopBits = 1,
[int] $ReadTimeout = 9000,
[switch] $ReadBytes
)
$serial = new-object System.IO.Ports.serialport
$serial.PortName = $port
$serial.BaudRate = $BaudRate
$serial.Parity = $Parity
$serial.DataBits = $DataBits
$serial.StopBits = $StopBits
$serial.ReadTimeout = $ReadTimeout
$serial |add-member -NotePropertyName "ReadBytes" -NotePropertyValue $ReadBytes
try {
$serial.open()
if ($ReadBytes) {
$job = Register-ObjectEvent -InputObject $serial -EventName DataReceived -Action {
$count = $sender.BytesToRead
if ($count) {
$buf = new-object byte[] $count
$sender.Read($buf, 0, $count) |out-null
$buf
}
}
}
else {
$job = Register-ObjectEvent -InputObject $serial -EventName DataReceived -Action {
$count = $sender.BytesToRead
if ($count) {
$buf = new-object char[] $count
$sender.Read($buf, 0, $count) |out-null
$buf -join ''
}
}
}
# TODO This should be a ps 5 class
$registeredserial = new-object psobject -property @{
port = $serial
job = $job
}
$ArduinoSerialConnections.Add($registeredserial) |out-null
$registeredserial
} catch [Exception] {
write-error $_
}
}
function Get-ArduinoSerial {
$GLOBAL:ArduinoSerialConnections.ToArray()
}
function Disconnect-ArduinoSerial {
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateScript({$_.port -and $_.job})]
[psobject] $SerialConnection
)
BEGIN {
$connections = @()
}
PROCESS {
$connections+=$SerialConnection
}
END {
foreach ($connection in $connections) {
$connection.Port.Close()
$connection.Job |stop-job
$connection.Job |remove-job
$GLOBAL:ArduinoSerialConnections.Remove($connection)
}
}
}
function Write-ArduinoSerial {
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateScript({$_.port -and $_.job})]
[psobject] $SerialConnection,
[Parameter(Mandatory=$true, Position=0)]
[string] $InputText
)
PROCESS {
$SerialConnection.Port.Write($InputText)
}
}
function Read-ArduinoSerial {
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateScript({$_.port -and $_.job})]
[psobject] $SerialConnection
)
PROCESS {
if ($SerialConnection.Job.HasMoreData) {
if ($SerialConnection.port.ReadBytes) {
$SerialConnection.Job |Receive-Job
} else {
($SerialConnection.Job |Receive-Job) -join ''
}
}
}
}
function Receive-ArduinoSerial {
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateScript({$_.port -and $_.job})]
[psobject] $SerialConnection
)
PROCESS {
"Receiving Data - Ctrl-C to Break"
while ($true) {
if ($SerialConnection.Job.HasMoreData) {
if ($SerialConnection.port.ReadBytes) {
$SerialConnection.Job |Receive-Job |write-host
} else {
($SerialConnection.Job |Receive-Job) -join '' |write-host -NoNewLine
}
}
}
}
}