enet-validator/scripts/session/analyze_branch.ps1

75 lines
3.5 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
}
$lines = [IO.File]::ReadAllLines("$env:TEMP\opencode\ss123.tsv")
# 收集 cmd=24 查询 (0x20==0200, flag==0) 与 其后 100ms 内的脉冲 UID
$queries = New-Object System.Collections.ArrayList
$pulses = 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 0x30) { continue }
$t = [double]$p[0]
if ($p[1] -eq "192.168.123.113" -and $p[2] -eq "192.168.123.1" -and [int]$b[4] -eq 4 -and $b.Length -gt 0x52 -and $b.Length -lt 2000 -and $b[0x20] -eq 2 -and $b[0x21] -eq 0 -and $b[0x30] -eq 0 -and $b[0x3C] -eq 1 -and $b[0x3D] -eq 0x24) {
[void]$queries.Add(@{ t=$t; b=$b })
}
if ($p[1] -eq "192.168.123.1" -and $p[2] -eq "192.168.123.113" -and $b.Length -lt 200 -and $b[0x2A] -eq 0xAA) {
$uid = ""
for ($i = 0; $i -lt 6; $i++) {
$b1 = $b[0x2B + 2*$i]; $b2 = $b[0x2C + 2*$i]
$byte = ([int]$b1 -band 0x55) -bor ([int]$b2 -band 0xAA)
$uid += $byte.ToString("x2")
}
[void]$pulses.Add(@{ t=$t; uid=$uid })
}
}
Write-Host ("cmd=24 查询={0} 脉冲={1}" -f $queries.Count, $pulses.Count)
# cmd=24 查询布局: 打印 0x3C..0x60 区域的不同取值分布
Write-Host "`n--- cmd=24 查询 0x3E..0x55 字段分布 (前 8 条不同样本) ---"
$seen = @{}
$shown = 0
foreach ($q in $queries) {
$key = ($q.b[0x3E..0x55] | ForEach-Object { $_.ToString("x2") }) -join ""
if ($seen.ContainsKey($key)) { continue }
$seen[$key] = 1
$b = $q.b
Write-Host (" t={0,7:F2} 0x3E={1} 0x44={2} 0x4A={3} 0x50={4}" -f $q.t,
(($b[0x3E..0x43] | ForEach-Object { $_.ToString("x2") }) -join ""),
(($b[0x44..0x49] | ForEach-Object { $_.ToString("x2") }) -join ""),
(($b[0x4A..0x4F] | ForEach-Object { $_.ToString("x2") }) -join ""),
(($b[0x50..0x55] | ForEach-Object { $_.ToString("x2") }) -join ""))
$shown++; if ($shown -ge 12) { break }
}
# 关键验证: 每个查询的分支范围 [lo,hi] 与 其后 100ms 内应答脉冲 UID 的关系
# 假设: 0x3E-0x43=下界, 0x44-0x49=上界 (大端 UID 序)
function UidNum($b, $o) {
$v = [double]0
for ($i = 0; $i -lt 6; $i++) { $v = $v * 256 + [int]$b[$o + $i] }
return $v
}
Write-Host "`n--- 分支范围 vs 应答脉冲 (前 25 条有应答/无应答对比) ---"
$fx = @("010734670125", "010734880900", "0899500026c9")
$fxNum = @{}
foreach ($f in $fx) { $n = [double]0; for ($i = 0; $i -lt 6; $i++) { $n = $n * 256 + [Convert]::ToInt32($f.Substring(2*$i, 2), 16) }; $fxNum[$f] = $n }
$n2 = 0
foreach ($q in $queries) {
$lo = UidNum $q.b 0x3E; $hi = UidNum $q.b 0x44
$ans = @($pulses | Where-Object { $_.t -ge $q.t -and $_.t -lt $q.t + 0.1 })
$ansStr = ($ans | ForEach-Object { $_.uid.Substring(8) }) -join ","
$inRange = @()
foreach ($f in $fx) { if ($fxNum[$f] -ge $lo -and $fxNum[$f] -le $hi) { $inRange += $f.Substring(8) } }
Write-Host (" t={0,7:F2} lo={1} hi={2} 范围内灯具=[{3}] 应答=[{4}]" -f $q.t,
(($q.b[0x3E..0x43] | ForEach-Object { $_.ToString("x2") }) -join "").Substring(4),
(($q.b[0x44..0x49] | ForEach-Object { $_.ToString("x2") }) -join "").Substring(4),
($inRange -join ","), $ansStr)
$n2++; if ($n2 -ge 25) { break }
}