我在互联网上找到了这个命令:
PS C:\> Get-CimInstance Win32_SerialPort | Select Name, Description, DeviceID
Name Description DeviceID
---- ----------- --------
Standard Serial over Bluetooth link (COM4) Standard Serial over Bluetooth link COM4
Standard Serial over Bluetooth link (COM5) Standard Serial over Bluetooth link COM5
Intel(R) Active Management Technology - SOL (COM3) Intel(R) Active Management Technology - SOL COM3
PS C:\>
但它没有向我显示任何 USB COM 端口。
您可以在 PowerShell 中列出串行端口,如下所示:
[System.IO.Ports.SerialPort]::GetPortNames()
您也可以以类似的方式与他们互动。下面从端口读取一行,例如:
function Read-SerialPort {
param(
[Parameter(Mandatory=$true)]
[string]$PortName,
[int]$BaudRate = 9600,
[System.IO.Ports.Parity]$Parity = [System.IO.Ports.Parity]::None,
[int]$DataBits = 8,
[System.IO.Ports.StopBits]$StopBits = [System.IO.Ports.StopBits]::One
)
try {
$port = [System.IO.Ports.SerialPort]::new($PortName, $BaudRate, $Parity, $DataBits, $StopBits)
$port.Open()
$data = $port.ReadLine()
return $data
}
catch {
Write-Error "Error reading from port: $_"
}
finally {
if ($port -and $port.IsOpen) {
$port.Close()
$port.Dispose()
}
}
}
Read-SerialPort -PortName COM5