diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c index f4b1ac4..19ca6b6 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/loop_uart_proto.c @@ -150,23 +150,19 @@ uint16_t lup_build_factory_init(uint8_t *buf) /* * 0x8A — 读灵敏度列表 - * Req: 7F 00 04 8A 00 00 ... + * Req: 7F 00 03 8A 00 00 xx xx + * LEN=3: CMD(1) + R/W(1) + Amount(1) */ uint16_t lup_build_sensitivity_read(uint8_t *buf) { buf[0] = LUP_MAGIC; buf[1] = LUP_ADDR_DEFAULT; - buf[2] = 0x04; // LEN = 4 (CMD + R/W + Amount + reserved?) + buf[2] = 0x03; // LEN = 3 (CMD + R/W + Amount) buf[3] = LUP_CMD_SENSITIVITY; // 0x8A - // Value: R/W + Amount (2 bytes, all zeros for read) - // 协议说 R/W: 1 byte, Amount: 1 byte, 后面是 Amount * (SensityIn + SensityOut) - // 对于 Read: R/W=0, Amount 忽略 - buf[4] = LUP_SENS_RW_READ; + buf[4] = LUP_SENS_RW_READ; // 0x00 buf[5] = 0x00; // Amount (ignored for read) - buf[6] = 0x00; // padding? - buf[7] = 0x00; // padding? lup_append_checksum(buf); - return 10; // Magic(1)+Header(3)+Value(4)+Check(2) + return 5 + buf[2]; // = 8 } /* @@ -179,8 +175,9 @@ uint16_t lup_build_sensitivity_write(uint8_t *buf, const LUP_Sensitivity *sens) buf[idx++] = LUP_MAGIC; buf[idx++] = LUP_ADDR_DEFAULT; - // LEN field: 2 + 2*sens->amount - uint8_t val_len = 2 + 2 * sens->amount; + // LEN = CMD(1) + R/W(1) + Amount(1) + Amount*(SensIn(2)+SensOut(2)) + // = 3 + sens->amount * 4 + uint8_t val_len = 3 + sens->amount * 4; buf[idx++] = val_len; buf[idx++] = LUP_CMD_SENSITIVITY; @@ -194,12 +191,13 @@ uint16_t lup_build_sensitivity_write(uint8_t *buf, const LUP_Sensitivity *sens) buf[idx++] = (uint8_t)((sens->sens_out[i] >> 8) & 0xFF); } lup_append_checksum(buf); - return idx + 2; // +2 for checksum bytes + return 5 + val_len; // Magic(1)+Addr(1)+LEN(1)+Value(val_len)+Check(2) } /* * 0x63 — 设置多路参数 - * Value: AutoMode(1) + Amount(1) + Amount*(Param[5]) + * Value: AutoMode(1) + Amount(1) + Amount * Param[5] + * LEN = CMD(1) + AutoMode(1) + Amount(1) + Amount*5 = 3 + 5*Amount */ uint16_t lup_build_set_param(uint8_t *buf, const LUP_ParamSet *ps) { @@ -208,8 +206,7 @@ uint16_t lup_build_set_param(uint8_t *buf, const LUP_ParamSet *ps) buf[idx++] = LUP_MAGIC; buf[idx++] = LUP_ADDR_DEFAULT; - // LEN = 2 + 5 * Amount - uint8_t val_len = 2 + 5 * ps->amount; + uint8_t val_len = 3 + 5 * ps->amount; // CMD + AutoMode + Amount + Amount*Param buf[idx++] = val_len; buf[idx++] = LUP_CMD_SET_PARAM; @@ -224,7 +221,7 @@ uint16_t lup_build_set_param(uint8_t *buf, const LUP_ParamSet *ps) buf[idx++] = ps->params[i].direction_mode; } lup_append_checksum(buf); - return idx + 2; + return 5 + val_len; // Magic(1)+Addr(1)+LEN(1)+Value(val_len)+Check(2) } /* @@ -305,26 +302,28 @@ int lup_parse_factory_init_resp(const uint8_t *pkg, uint16_t len, uint8_t *succe /* * Parse 0x8A Response: - * Value: Amount (1byte) + Amount * SensityValue (2bytes each) + * Value: Ret(1) + Amount(1) + Amount*(SensIn(2) + SensOut(2)) + * Ret = 0x10 (Read return) | 0x11 (Write return) + * + * eg: 7F 80 13 8A 10 04 A2 00 51 00 51 00 36 00 ... + * Ret=0x10=Read, Amount=4 + * Lv0: SensIn=0x00A2, SensOut=0x0051 */ int lup_parse_sensitivity_resp(const uint8_t *pkg, uint16_t len, LUP_Sensitivity *sens) { uint8_t i; - if (len < 6) return -1; + if (len < 8) return -1; // min: 7F+Addr+LEN+CMD+Ret+Amount+XOR+SUM = 8 if (pkg[0] != LUP_MAGIC || pkg[3] != LUP_CMD_SENSITIVITY) return -2; const uint8_t *val = pkg + 4; - uint8_t amount = val[0]; + sens->rw = val[0]; // 0x10 or 0x11 + sens->amount = val[1]; - if (amount > LUP_COIL_COUNT) amount = LUP_COIL_COUNT; - sens->amount = amount; - sens->rw = 0; // response, not request + if (sens->amount > LUP_COIL_COUNT) sens->amount = LUP_COIL_COUNT; - for (i = 0; i < amount; i++) { - uint16_t sv = val[1 + i * 2] | ((uint16_t)val[2 + i * 2] << 8); - // Response returns unified SensityValue (not split in/out) - sens->sens_in[i] = sv; - sens->sens_out[i] = sv; + for (i = 0; i < sens->amount; i++) { + sens->sens_in[i] = val[2 + i * 4] | ((uint16_t)val[3 + i * 4] << 8); + sens->sens_out[i] = val[4 + i * 4] | ((uint16_t)val[5 + i * 4] << 8); } return 0; } diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c index 63f835c..080c57b 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c @@ -992,16 +992,20 @@ static void json_check_pending(void) { memset(&sens, 0, sizeof(sens)); int ret = lup_parse_sensitivity_resp(g_lup_cmd.resp_buf, g_lup_cmd.resp_len, &sens); if (ret == 0) { - char data_json[512]; + char data_json[1024]; char *p = data_json; int rem = sizeof(data_json); - int w = snprintf(p, rem, "{\"amount\":%d,\"channels\":[", sens.amount); + int w = snprintf(p, rem, + "{\"ret\":\"%s\",\"amount\":%d,\"channels\":[", + (sens.rw == 0x10) ? "read" : (sens.rw == 0x11) ? "write" : "?", + sens.amount); p += w; rem -= w; uint8_t i; for (i = 0; i < sens.amount; i++) { w = snprintf(p, rem, - "%s{\"ch\":%d,\"sens_value\":%d}", - (i > 0) ? "," : "", i + 1, sens.sens_in[i]); + "%s{\"ch\":%d,\"sens_in\":%d,\"sens_out\":%d}", + (i > 0) ? "," : "", i + 1, + sens.sens_in[i], sens.sens_out[i]); p += w; rem -= w; } snprintf(p, rem, "]}"); @@ -1013,21 +1017,24 @@ static void json_check_pending(void) { "Failed to parse sensitivity"); } } else if (strcmp(g_json_pending.cmd, "loop_sens_write") == 0) { - // 0x8A Write response returns sensitivity list LUP_Sensitivity sens; memset(&sens, 0, sizeof(sens)); int ret = lup_parse_sensitivity_resp(g_lup_cmd.resp_buf, g_lup_cmd.resp_len, &sens); if (ret == 0) { - char data_json[512]; + char data_json[1024]; char *p = data_json; int rem = sizeof(data_json); - int w = snprintf(p, rem, "{\"amount\":%d,\"channels\":[", sens.amount); + int w = snprintf(p, rem, + "{\"ret\":\"%s\",\"amount\":%d,\"channels\":[", + (sens.rw == 0x10) ? "read" : (sens.rw == 0x11) ? "write" : "?", + sens.amount); p += w; rem -= w; uint8_t i; for (i = 0; i < sens.amount; i++) { w = snprintf(p, rem, - "%s{\"ch\":%d,\"sens_value\":%d}", - (i > 0) ? "," : "", i + 1, sens.sens_in[i]); + "%s{\"ch\":%d,\"sens_in\":%d,\"sens_out\":%d}", + (i > 0) ? "," : "", i + 1, + sens.sens_in[i], sens.sens_out[i]); p += w; rem -= w; } snprintf(p, rem, "]}"); diff --git a/vd960DBN/docs/DLD960Loop_串口通信协议.md b/vd960DBN/docs/DLD960Loop_串口通信协议.md index b9a8023..b68fda6 100644 --- a/vd960DBN/docs/DLD960Loop_串口通信协议.md +++ b/vd960DBN/docs/DLD960Loop_串口通信协议.md @@ -4,7 +4,7 @@ ## 1.1 通信方式 -串口通信,波特率:115200 +串口通信,波特率:192000 # 2 协议格式 @@ -102,8 +102,8 @@ Package | | Header | R/W,Amount, (Amount \* (SensityIn + SensityOut)) | | --- | --- | --- | -| **Value(Hex)** | 00 04 8A | xx | -| **Length** | 3 Byte | (2 + 2\*x )Byte | +| **Value(Hex)** | ADDR xx 8A | xx | +| **Length** | 3 Byte | (1+ 1 + Amount \*(2 + 2 ))Byte | > R/W: 1字节,0 Read, 1 Write @@ -115,10 +115,28 @@ Package 返回 -| | Header | Amount,  (Amount \* SensityValue) | +| | Header | Ret,Amount,  (Amount \* (SensityIn + SensityOut)) | | --- | --- | --- | -| **Value(Hex)** | ADDR 03 8A | xx | -| **Length** | 3 Byte | (1 + 2\*x) Byte | +| **Value(Hex)** | ADDR xx 8A | xx | +| **Length** | 3 Byte | (1+ 1 + Amount \*(2 + 2 ))Byte | + +> Ret: 0x10 | R/W,如果是读指令,返回0x10,如果是写指令,返回0x11。 + +```python +7F 80 13 8A 10 04 A2 00 51 00 51 00 36 00 36 00 20 00 1C 00 12 00 81 2F + +7F Magic Byte +80 Addr, 00/80 default +13 Len, 0x13=19字节(Cmd + Data) +8A Cmd +10 读指令返回 +04 四级灵敏度(包括进入灵敏度 和 离开灵敏度,分别都是两个字节) +A2 00 51 00: 第0级灵敏度,0x00A2为进入 162,0x0051为离开 81 +51 00 36 00:第1级灵敏度,0x0051为进入 81, 0x0036为离开 54 +36 00 20 00:第2级灵敏度,0x0036为进入 54, 0x0020 为离开 32 +1C 00 12 00:第3级灵敏度,0x001C为进入 28,0x0012 为离开 18 +81 2F 校验码 +``` ## 3.05 设置车检器多路参数(0x63)