我正在尝试使用 PowerShell 设置与 ViewSonic Display 的串行端口通信。我发送打开或关闭显示状态的请求,应该得到 9 位回复 56 48 49 114 108 48 48 49 13 回复开启或 56 48 49 114 108 48 48 48 13 回复关闭 当我运行代码时,我只得到一个数字的回复当我添加第二次读取时,我得到了剩下的 8 个但是当我添加一个断点并使用调试器单步执行它时,我在第一次读取时得到了所有 9 个。任何想法可能导致这种奇怪的行为?关于更好的方法来做到这一点的任何建议?请参阅下面的代码和结果。
PS C:\Users\james\Desktop> [Byte[]] $request = 0x38, 0x30, 0x31, 0x67, 0x6C, 0x30, 0x30, 0x30, 0x0D
$portreturn = [System.Byte[]]::CreateInstance([System.Byte], 9)
$port = new-Object System.IO.Ports.SerialPort COM3, 9600, None, 8, one
$port.Open()
$port.Write($request, 0, $request.Count)
$port.Read($portreturn, 0, $portreturn.Length)
Write-Host "portreturn1" $portreturn -foreground black -BackgroundColor white
$port.Read($portreturn, 0, $portreturn.Length)
Write-Host "portreturn2" $portreturn -foreground black -BackgroundColor white
$port.Close()
1个端口返回1 56 0 0 0 0 0 0 0 0
8 端口返回2 48 49 114 108 48 48 49 13 0
PS C:\Users\james\Desktop> C:\Users\james\Desktop\VStest2a.ps1
在 'C:\Users\james\Desktop\VStest2a.ps1:4' [DBG] 上点击行断点:PS C:\Users\james\Desktop>>
[DBG]: PS C:\Users\james\Desktop>>
[DBG]:PS C:\Users\james\Desktop>> 9
[DBG]: PS C:\Users\james\Desktop>> portreturn1 56 48 49 114 108 48 48 49 13
[DBG]: PS C:\Users\james\Desktop>>
为了清楚起见,链接到图片。 截屏