[Console]::OutputEncoding = [Text.Encoding]::UTF8 $lines = [IO.File]::ReadAllLines("$env:TEMP\opencode\set123b.tsv") $hex = $null foreach ($l in $lines) { $p = $l -split "`t" if ($p.Count -lt 5 -or -not $p[4]) { continue } if ($p[1] -ne "192.168.123.113") { continue } $h = $p[4] -replace " ", "" if ($h.Length -ne 2168) { continue } if ($h.Substring(0x20 * 2, 4) -ne "0200") { continue } # subtype event if ($h.Substring(0x3C * 2, 2) -ne "01") { continue } # SubSC=01 if ($h.Substring(0x3D * 2, 2) -ne "1a") { continue } # msgLen=26 if ($h.Substring(0x4F * 2, 2) -ne "30") { continue } if ($h.Substring(0x30 * 2, 2) -ne "00") { continue } $hex = $h; break } if (-not $hex) { throw "no SET request frame found" } $n = $hex.Length / 2 $req = New-Object byte[] $n for ($i = 0; $i -lt $n; $i++) { $req[$i] = [Convert]::ToByte($hex.Substring(2 * $i, 2), 16) } $uidS = ($req[0x3E..0x43] | ForEach-Object { $_.ToString("x2") }) -join "" $pidS = $req[0x50].ToString("x2") + $req[0x51].ToString("x2") Write-Host "SET req: ${n}B UID=$uidS PID=$pidS" $udp = New-Object System.Net.Sockets.UdpClient $udp.Client.ReceiveTimeout = 3000 $ep = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse("192.168.58.7"), 31772) $sw = [Diagnostics.Stopwatch]::StartNew() $sent = $udp.Send($req, $req.Length, $ep) Write-Host "sent $sent bytes from $($udp.Client.LocalEndPoint)" while ($true) { try { $rep = $udp.Receive([ref]$ep) if ($rep.Length -lt 0x44) { Write-Host ("* reply {0}B latency {1}ms (42B telemetry)" -f $rep.Length, $sw.ElapsedMilliseconds) continue } $srcS = ($rep[0x33..0x38] | ForEach-Object { $_.ToString("x2") }) -join "" Write-Host ("* reply {0}B latency {1}ms srcUID={2} TN={3:x2} RT={4:x2} CC={5:x2} PID={6:x2}{7:x2} PDL={8}" -f ` $rep.Length, $sw.ElapsedMilliseconds, $srcS, $rep[0x39], $rep[0x3A], $rep[0x3E], $rep[0x3F], $rep[0x40], $rep[0x41]) $mlen = [int]$rep[0x2C]; $sum = 0 for ($i = 0x2A; $i -lt 0x2A + $mlen; $i++) { $sum += $rep[$i] } $ckv = ([int]$rep[0x2A + $mlen] -shl 8) -bor $rep[0x2A + $mlen + 1] $v = if (($sum -band 0xFFFF) -eq $ckv) { "OK" } else { "FAIL" } Write-Host (" checksum calc={0:x4} frame={1:x4} {2}" -f ($sum -band 0xFFFF), $ckv, $v) break } catch { Write-Host "no reply within 3s" break } } $udp.Close()