22 lines
552 B
PowerShell
22 lines
552 B
PowerShell
$port = New-Object System.IO.Ports.SerialPort COM5, 115200, None, 8, 1
|
|
$port.ReadTimeout = 1000
|
|
try {
|
|
$port.Open()
|
|
Write-Host "Reading serial port COM5..." -ForegroundColor Cyan
|
|
$startTime = Get-Date
|
|
while (((Get-Date) - $startTime).TotalSeconds -lt 15) {
|
|
try {
|
|
$line = $port.ReadLine()
|
|
Write-Output $line
|
|
} catch [TimeoutException] {
|
|
# normal timeout
|
|
}
|
|
}
|
|
} catch {
|
|
Write-Error $_
|
|
} finally {
|
|
if ($port -ne $null -and $port.IsOpen) {
|
|
$port.Close()
|
|
}
|
|
}
|