58 lines
2.7 KiB
PowerShell
58 lines
2.7 KiB
PowerShell
|
|
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
|||
|
|
$ErrorActionPreference = "Stop"
|
|||
|
|
function H2B($hex) {
|
|||
|
|
if (-not $hex) { return $null }
|
|||
|
|
$n = $hex.Length / 2
|
|||
|
|
$b = New-Object byte[] $n
|
|||
|
|
for ($i = 0; $i -lt $n; $i++) { $b[$i] = [Convert]::ToByte($hex.Substring(2*$i, 2), 16) }
|
|||
|
|
return $b
|
|||
|
|
}
|
|||
|
|
function HexR($b, $o, $l) { if ($o + $l -gt $b.Length) { $l = $b.Length - $o }; return (($b[$o..($o+$l-1)] | ForEach-Object { $_.ToString("x2") }) -join " ") }
|
|||
|
|
$lines = [IO.File]::ReadAllLines("$env:TEMP\opencode\set123.tsv")
|
|||
|
|
$sets = New-Object System.Collections.ArrayList
|
|||
|
|
$resps = New-Object System.Collections.ArrayList
|
|||
|
|
foreach ($l in $lines) {
|
|||
|
|
$p = $l -split "`t"; if ($p.Count -lt 5 -or -not $p[4]) { continue }
|
|||
|
|
$b = H2B $p[4]; if ($b.Length -lt 0x40) { continue }
|
|||
|
|
$t = [double]$p[0]
|
|||
|
|
# 主机→设备 事件帧, CC=0x30 (SET) @0x4F
|
|||
|
|
if ($p[1] -eq "192.168.123.113" -and $b.Length -eq 1084 -and [int]$b[4] -eq 4 -and $b[0x20] -eq 2 -and $b[0x21] -eq 0 -and $b.Length -gt 0x52 -and $b[0x4F] -eq 0x30) {
|
|||
|
|
[void]$sets.Add(@{ t=$t; b=$b })
|
|||
|
|
}
|
|||
|
|
# 设备→主机 应答, CC=0x31 (SET_RESPONSE) @0x2A... 应答帧 RDM 区在 0x2A 起, CC@0x2A, msgLen@0x2B, ... CC字段在 0x2A
|
|||
|
|
if ($p[1] -eq "192.168.123.1" -and $b.Length -lt 300 -and $b.Length -gt 0x30 -and $b[0x2A] -eq 0xCC -and $b[0x3A] -eq 0x31) {
|
|||
|
|
[void]$resps.Add(@{ t=$t; b=$b })
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
Write-Host ("SET 请求 (CC=0x30): {0} SET 应答 (CC=0x31): {1}" -f $sets.Count, $resps.Count)
|
|||
|
|
Write-Host "`n=== SET 请求逐帧解析 ==="
|
|||
|
|
foreach ($s in $sets) {
|
|||
|
|
$b = $s.b
|
|||
|
|
$flag = $b[0x30]
|
|||
|
|
$rdmlen = [int]$b[0x34] + [int]$b[0x35]*256
|
|||
|
|
$uid = (HexR $b 0x3E 6) -replace " ",""
|
|||
|
|
$tn = $b[0x4A]
|
|||
|
|
$cc = $b[0x4F]
|
|||
|
|
$pidH = "{0:x2}{1:x2}" -f $b[0x50], $b[0x51]
|
|||
|
|
$pdl = [int]$b[0x52]
|
|||
|
|
$pd = HexR $b 0x53 ([Math]::Min($pdl, 24))
|
|||
|
|
Write-Host ("t={0,8:F3} flag={1:x2} rdmlen={2} UID={3} TN={4:x2} CC={5:x2} PID={6} PDL={7} PD=[{8}]" -f $s.t, $flag, $rdmlen, $uid, $tn, $cc, $pidH, $pdl, $pd)
|
|||
|
|
}
|
|||
|
|
Write-Host "`n=== SET 应答逐帧解析 (CC=0x31) ==="
|
|||
|
|
foreach ($r in $resps) {
|
|||
|
|
$b = $r.b
|
|||
|
|
$ml = [int]$b[0x2B]
|
|||
|
|
$uid = (HexR $b 0x33 6) -replace " ",""
|
|||
|
|
$tn = $b[0x39]
|
|||
|
|
$rt = $b[0x3A]
|
|||
|
|
$pidH = "{0:x2}{1:x2}" -f $b[0x3F], $b[0x40]
|
|||
|
|
$pdl = [int]$b[0x41]
|
|||
|
|
$pd = ""
|
|||
|
|
if ($pdl -gt 0 -and $b.Length -gt 0x42) { $pd = HexR $b 0x42 ([Math]::Min($pdl, 16)) }
|
|||
|
|
# checksum 校验
|
|||
|
|
$sum = 0; for ($i = 0x2A; $i -lt 0x2A + $ml; $i++) { $sum += $b[$i] }
|
|||
|
|
$cs = ([int]$b[0x2A+$ml] -shl 8) -bor [int]$b[0x2A+$ml+1]
|
|||
|
|
$ok = if (($sum -band 0xFFFF) -eq $cs) { "OK" } else { "BAD" }
|
|||
|
|
Write-Host ("t={0,8:F3} len={1} UID={2} TN={3:x2} RT={4:x2} PID={5} PDL={6} PD=[{7}] cs={8}" -f $r.t, $b.Length, $uid, $tn, $rt, $pidH, $pdl, $pd, $ok)
|
|||
|
|
}
|