-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial.ps1
56 lines (47 loc) · 1.5 KB
/
serial.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
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
# Add to C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
# for function to be avaiable always in Powershell
# Use: Serial-Monitor
# Use: Serial-Monitor COM5 115200
# Ctrl-C to end it.
function Serial-Monitor {
param(
[Parameter(Mandatory=$false)]
[String] $COMPort,
[Parameter(Mandatory=$false)]
[ValidateRange(0, 115200)]
[Int] $BaudRate
)
if ((-not ($PSBoundParameters.ContainsKey('COMPort'))) -and (-not ($PSBoundParameters.ContainsKey('BaudRate')))) {
echo "Usage: Serial-Monitor <COMPort> <BaudRate>"
echo " eg.: Serial-Monitor COM5 115200"
echo ""
echo "Getting name and device ID of COM Ports..."
Get-CimInstance -Class Win32_SerialPort | Select-Object Name, Description, DeviceID
return
}
echo "Exit with Ctrl-C"
echo ""
$port = new-Object System.IO.Ports.SerialPort $COMPort, $BaudRate, None, 8, one
$port.ReadTimeOut = 1000;
$port.Open()
[console]::TreatControlCAsInput = $true
do {
if ([console]::KeyAvailable) {
$key = [system.console]::readkey($true)
if (($key.modifiers -band [consolemodifiers]"control") -and ($key.key -eq "C")) {
echo ""
echo "Exititng"
break;
}
}
else {
try {
$line = $port.ReadLine()
Write-Host $line
}
catch {}
}
}
while ($port.IsOpen)
$port.Close()
}