-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserial-toys.psm1
More file actions
204 lines (170 loc) · 5.1 KB
/
Copy pathserial-toys.psm1
File metadata and controls
204 lines (170 loc) · 5.1 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
function Test-Port {
param(
$port
)
try {
$portObj = new-Object System.IO.Ports.SerialPort $port,115200,None,8,one
$portObj.DtrEnable = $true;
$portObj.RtsEnable = $true;
$portObj.open()
$portObj.Close()
}
catch
{
Write-Error "Failed to connect. ${$_.Exception.Message}"
Write-Error "Unable to connect to $port Serial Port " -ErrorAction Stop
Exit 5
}
}
function CopyFileFromMicrocontrollerToPc {
# This uses ampy to copy a file from the microcontroller to the PC
param(
$file,
$port
)
while ($null -eq $port)
{
try {
$port = Find-MicrocontrollerPort
}
catch {
$err = "Error. $_"
Write-Progress -Activity "Detecting COM Port" -Status $err
}
}
ampy --port $port get $file > $file
}
function CopyFileFromPcToMicrocontroller {
# This uses ampy to copy a file from the PC to the microcontroller
param(
$file,
$port
)
while ($null -eq $port)
{
try {
$port = Find-MicrocontrollerPort
}
catch {
$err = "Error. $_"
Write-Progress -Activity "Detecting COM Port" -Status $err
}
}
ampy --port $port put $file
}
function Find-MicrocontrollerPort {
$acceptableDescriptions = @(
'USB Serial Device',
'USB-SERIAL CH340K',
'Prolific USB-to-Serial Comm Port',
'USB-Enhanced-SERIAL CH9102',
'USB-SERIAL CH340',
'Silicon Labs CP210x USB to UART Bridge'
)
$SerialPorts = Get-WmiObject Win32_PnPEntity | Where-Object Name -match 'COM\d+' | Select-Object Name, Description, DeviceID
$Names = $SerialPorts | Where-Object { $acceptableDescriptions -contains $_.Description } | Select-Object -ExpandProperty Name
$Names | ForEach-Object {
# Extract the port number from the name
if ($_ -match 'COM\d+') {
$port = $Matches.0
$desc = $_
}
}
if ($null -eq $port) {
# Print a list of serial ports
Write-Host "Available Serial Ports:"
$SerialPorts | ForEach-Object { Write-Host "$($_.Name) - $($_.Description)" }
throw "Unable to find serial port"
}
if ('COM1' -eq $port) {
Write-Host "Available Serial Ports:"
Get-WmiObject Win32_PnPEntity | Where-Object Name -match 'COM\d+' | Select-Object Name | ForEach-Object { Write-Host "$($_.Name)" }
Write-Host "Please connect the microcontroller to the PC and try again."
throw "Only found COM1, which is not a valid port"
}
Write-Host "Detected $port - $desc"
return $port
}
function Show-SerialLog {
param(
$port
)
while ($null -eq $port)
{
try {
$port = Find-MicrocontrollerPort
}
catch {
$err = "Error. $_"
Write-Progress -Activity "Detecting COM Port" -Status $err
}
}
Write-Host "-------------------------------------------------------------------------------"
Write-Host "Listening on $port. Serial Log follows - Press any key to disconnect!"
Write-Host "-------------------------------------------------------------------------------"
$portObj = new-Object System.IO.Ports.SerialPort $port,115200,None,8,one
$portObj.ReadTimeout = 1000
$portObj.DtrEnable = $true;
$portObj.RtsEnable = $true;
$portObj.Open()
while (! [console]::KeyAvailable) {
try {
if (! $portObj.IsOpen) {
$portObj.Open()
}
$data = $portObj.ReadLine()
if ($data -ne "") {
Write-Output $data
Write-Progress -Completed -Activity "Connecting"
}
}
catch [TimeoutException] {
continue;
}
catch {
$err = "Error. $_"
Write-Progress -Activity "Connecting" -Status $err
}
#python -m serial.tools.miniterm $port 115200 2> $null
}
Write-Host "Disconnecting from port $port"
$portObj.Close()
}
function Restart-Microcontroller {
param(
$port
)
Test-Port $port
$portObj = new-Object System.IO.Ports.SerialPort $port,115200,None,8,one
$portObj.DtrEnable = $true;
$portObj.RtsEnable = $true;
$portObj.open()
$portObj.WriteLine("$([char] 2)")
$portObj.WriteLine("$([char] 3)")
$portObj.WriteLine("$([char] 4)")
$portObj.WriteLine("import machine\r\n")
$portObj.WriteLine("machine.reset()\r\n")
$portObj.Close()
esptool --port $port chip_id
}
function Step-Version {
# increment the version
$raw = Get-Content -Path .\version -Raw
$v = [version]$raw
$nv = [version]::New($v.Major,$v.Minor,$v.Build+1,0)
$newVersion = "$($nv)";
$newVersion = $newVersion.Substring(0, $newVersion.Length - 2)
Write-Host "Version is now: $newVersion"
Write-Output $newVersion | Out-File -encoding ascii .\version
}
function Xargs {
param(
$Cmd
)
process {
$args += ,$_
}
end {
& $Cmd @args
}
}