docs: 补充 V2.8~V3.1 开发日志 (编译修复/车间距/misc_counter/上电抑制)

This commit is contained in:
wangfq
2026-07-03 17:01:09 +08:00
parent acb999a881
commit a73a9392bd
2 changed files with 162 additions and 0 deletions
+125
View File
@@ -0,0 +1,125 @@
# vd960DBN 开发日志
> MCU: CH32V208 (RISC-V, WCH) | 通信: BLE + ETH (WCHNET) + UART2→Loop MCU | TCP JSON 端口: 5960
>
> 项目定位: DLD960 通信板 — BLE 配网、TCP JSON 协议服务、Loop MCU 串口桥接
---
## 2026-06-26 — TCP JSON 协议框架搭建
### 1. TCP JSON Server 基础实现
- WCHNET TCP listen socket 创建(端口 5960PROTO_TYPE_TCP
- 参考 WCH 官方 EVT/EXAM/ETH/TCPServer 例程
- `g_net_state.flag` 状态机: `ETH_LibInit→1, WCHNET_CreateUdpSocket→2, WCHNET_CreateTcpSocket→3`
- SSC 禁用时 (`NET_SSC_ENABLE=0`) 手动设 `flag=2` 跳过 UDP 初始化
### 2. 鉴权 + 15 条命令
- `pwd_verify` 鉴权,3次错误 → 60s 锁定
- 命令表驱动分发: `dev_info_query`, `ssc_net_set/query`, `iot_net_set/query`, `iot_topic_set/query`, `pwd_set`, `factory_reset`, `device_reset`, `loop_param_set/query`, `loop_version_query`, `loop_factory_init`, `loop_sens_read/write`
- Deferred 响应模式: Loop MCU 命令异步 → `TcpJsonPending` 挂起 → `json_check_pending()` 轮询
### 3. simple_json 解析器修复
- **6 个 bug 修复**: NULL 解引用崩溃(plain values)、缺少 null terminator、buffer unsafe clear、数组支持缺失
- 字符串值提取时**不带引号**(调用方无需手动 strip quotes
### 4. WCHNET TCP 踩坑
| 问题 | 修复 |
|------|------|
| listen socket 与数据 socket 混用 → 收不到数据 | CONNECT 发到 N, RECV/DISCONNECT 发到 N+1 |
| `WCHNET_SocketSend` 在 listen socket 静默失败 | 改用 `g_json_socket_listen + 1` |
| 接收缓冲区与帧缓冲区重叠 → 数据损坏 | 分离 WCHNET 内部 buf + 帧累加 buf |
| `#if NET_SSC_ENABLE` 误包共享函数 | 移出 `mStopIfError`/`GetMacAddr`/`get_ipstr_to_array` |
---
## 2026-06-30 — Loop MCU 串口协议 (0x7F) + 传感器上报
### 1. USART2 Loop MCU 通信
- 波特率 **192000**(文档写 115200,实际硬件 192000
- 0x7F 协议帧解析器: `lup_feed_byte()` 逐字节状态机 + LEN-based 帧边界
- 校验字节站位: `total_len = 5 + LEN`(CMD 已在 LEN 中,勿重复计)
- 命令: `lup_cmd_send()` 发送 → `lup_cmd_check_timeout()` 轮询超时
### 2. 0xC0 传感器数据上报
- Loop MCU 主动推送 0xC0 帧 → `lup_process_frame()` 校验 → 回调 `json_sensor_callback`
- TCP JSON 输出格式: `{"sens_type":"multi_coil","coils":[...]}`
- `g_report_active` 开关控制上报启停
- 0xC0 帧通过回调直接驱动网络上报,不经 `uart_srv` 阻塞
### 3. 0xC0 帧时间量字段
- `misc_type=0``passtime_ms`(通过时间 / 车间距,根据 `car_state` 区分)
- `misc_type=1``cut_amount`(线圈断开次数)
- `misc_type=2``flow_amount`(车流量)
- `misc_type=3``relay_count`(继电器动作次数)
### 4. 协议文档
- `docs/vd960_loop_protocol_v1.0x.md` — 0x7F 帧格式、校验算法、命令参考
- 波特率修正: 115200 → 192000
---
## 2026-07-01 — passtime_ms5 字段改名
`vd960Loop` 侧时间戳从 5ms 改 50ms 后,DBN 侧同步:
- `passtime_ms5``passtime_ms`(字段名去 5 后缀)
- `loop_uart_proto.h/c` 结构体 + 解析 + `tcp_json_srv.c` JSON 字段同步
---
## 2026-07-02~03 — TCP Server 超时自动重启机制
### 1. 三项超时触发条件
| 条件 | 时间 | 处理 |
|------|------|------|
| 无任何连接 | 5min | `do_restart` → 计数重启 |
| 无数据交互 | 5min | `do_restart` → 计数重启 |
| Auth 密码错 3 次 | 立即 | `tcp_json_restart()` 直接重启 |
### 2. 重启计数保护
- 最多连续重启 **3 次**,超过进入 **10 分钟冷却期**
- `CONNECT` 成功时 `restart_count` 清零(正常连接重置计数,不算异常)
- Auth timeout 不消耗计数器(正常运维行为)
### 3. WCHNET 限制 - listen socket 不可关闭重建
**根因**: `WCHNET_SocketClose(listen)` 后端口 5960 仍标记"已占用"`SocketCreat` 返回 `ERR_ISCONN(0x1D)`
**最终方案**: `tcp_json_restart()` 只关数据 socket(N+1) 断开客户端,listen socket 保持不动,仅重置应用层状态。WCHNET 自动处理下一个 CONNECT。
### 4. Auth timeout 死循环修复
原 Auth timeout 手工 `WCHNET_SocketClose` 后未设 `g_json_socket_listen = 0xFF`,下次 poll 条件仍满足 → 无限打印。改用统一的 `do_restart` 路径后修复。
### 5. Auth 3次失败 → 直接重启
去掉 60s Auth timeout 倒计时。改为连接后 3 次鉴权失败(密码错/格式错)即 `tcp_json_restart()`。连接后无交互由 5min 空闲检查覆盖。
---
## 修订记录
| 版本 | 时间 | 说明 |
|------|------|------|
| V2.5 | 2026-07-03 | tcp_json_restart 只关数据socket, listen保持不动 (WCHNET限制) |
| V2.4 | 2026-07-03 | 去掉 Auth 60s timeout, 改为3次失败即重启 |
| V2.3 | 2026-07-03 | Auth timeout 不计入重启限额, 修复冷却期阻断连接 |
| V2.2 | 2026-07-03 | Auth timeout 死循环打印修复 → goto do_restart |
| V2.1 | 2026-07-03 | TCP Server 自动重启机制 (3条件 + 3次限额 + 10min冷却) |
| V2.0 | 2026-07-01 | passtime_ms5 → passtime_ms 字段改名 |
| V1.5 | 2026-06-30 | 0xC0 传感器上报 + 时间量字段完善 + 协议文档 |
| V1.4 | 2026-06-30 | USART2 Loop MCU 0x7F 协议实现 |
| V1.3 | 2026-06-30 | simple_json 6 bug 修复 |
| V1.2 | 2026-06-30 | WCHNET buffer 分离 + SocketSend listen→data 修正 |
| V1.1 | 2026-06-30 | NET_SSC_ENABLE 隔离 + #if guard 共享函数修复 |
| V1.0 | 2026-06-26 | TCP JSON 协议框架 (鉴权 + 15条命令) |
+37
View File
@@ -168,10 +168,47 @@ misc_value = (report_counter - passtime_start) / 2;
--- ---
## 2026-07-03 — ARMCC 编译修复 + 时间戳/上报完善 (V2.8~V3.1)
### 1. ARMCC 编译错误修复 (V2.8)
- **#991**: `g_loop_states` 初始化去多余嵌套大括号 `{{0}, {{{0}}}}``{0}`
- **#177-D**: 删除未引用变量 `_counter1_init`
- **#188-D**: 7处 `at32_led_*()` 调用加 `(led_type)` 强转
### 2. 车间距时间补齐 (V2.9)
`Loop154_Unit` 新增 `last_exit_tick` 字段:
- **进场**: `misc_value = misc_counter - last_exit_tick`(车间距,首车=0
- **离场**: 记录 `last_exit_tick = misc_counter``misc_value = misc_counter - passtime_start`(通过时间)
- 四通道独立计算,50ms tick 单位
### 3. report_counter 清零导致下溢修复 (V3.0)
**根因**: `report_counter` 被 UART 上报清零后,`last_exit_tick`/`passtime_start` 存旧值,再次进场时差值下溢 → `0xFFFFFFF7`
**修复**: 新增 `misc_counter` 独立自由运行计数器(50ms,永不归零),所有时间戳操作改用 `misc_counter`
```
Loop154_States:
report_counter → 上报间隔调度 (会清零)
misc_counter → 时间戳 (永不清零)
```
`report_counter` 只负责上报调度,`misc_counter` 负责时间量计算,互不干扰。
### 4. 上电 3 秒抑制上报 (V3.1)
`uart_report_packet_loop_acs` 开头新增检查:`misc_counter < 60`3s @50ms)时跳过上报并清零 `report_counter`,等线圈基线稳定后再开始主动上报。
---
## 修订记录 ## 修订记录
| 版本 | 时间 | 说明 | | 版本 | 时间 | 说明 |
|------|------|------| |------|------|------|
| V3.1 | 2026-07-03 | 上电3秒抑制主动上报 |
| V3.0 | 2026-07-03 | misc_counter 自由运行替代 report_counter 做时间戳 |
| V2.9 | 2026-07-03 | 补齐车间距时间 (gap time) |
| V2.8 | 2026-07-03 | ARMCC 编译修复 (多余大括号/未引用变量/枚举混用) |
| V2.7 | 2026-07-03 | report_counter 改 50ms tick,时间量单位 10ms→50ms | | V2.7 | 2026-07-03 | report_counter 改 50ms tick,时间量单位 10ms→50ms |
| V2.6 | 2026-07-03 | misc_value 时间量单位 5ms→10ms | | V2.6 | 2026-07-03 | misc_value 时间量单位 5ms→10ms |
| V2.5 | 2026-06-29 | 稳定期窗口 100 快速收敛 | | V2.5 | 2026-06-29 | 稳定期窗口 100 快速收敛 |