-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial-toys.psm1
139 lines (117 loc) · 3.76 KB
/
serial-toys.psm1
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
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 Find-MicrocontrollerPort {
$SerialPorts = Get-WmiObject Win32_PnPEntity | Where-Object Name -match 'COM\d+' | Select-Object Name, Description, DeviceID
$Name = $SerialPorts | Where-Object { ($_.Description -eq 'USB Serial Device') -or ($_.Description -eq 'USB-SERIAL CH340K') -or ($_.Description -eq 'Prolific USB-to-Serial Comm Port') -or ($_.Description -eq 'USB-Enhanced-SERIAL CH9102') -or ($_.Description -eq 'USB-SERIAL CH340') } | Select-Object -ExpandProperty Name
$Name -match "COM\d+" > $null
$port = $Matches.0
try {
if ($null -eq $port) {
throw "Unable to find serial port"
}
Write-Host "Detected $port"
return $port
}
catch
{
Write-Host "Failed to connect. ${$_.Exception.Message}"
Write-Host "Detected ports:"
Write-Host Get-WmiObject Win32_PnPEntity | Where-Object Name -match 'COM\d+' | Select-Object Name, Description, DeviceID
Write-Host "Aborted." -ErrorAction Stop
Exit 5
}
}
function Show-SerialLog {
param(
$port
)
if ($port -eq $null)
{
Write-Host "No Port Specified, Auto Detecting.."
$port = Find-MicrocontrollerPort
}
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
}
}