From 1ff4910f55a7c13163366f033d954db0429de2cf Mon Sep 17 00:00:00 2001 From: wjw Date: Wed, 16 Jul 2025 15:24:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Diccid=E4=B8=8A=E6=8A=A5?= =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom/jt808/inc/jt808_util.h | 8 ++++++++ custom/jt808/src/jt808_set_TermParam.c | 17 ++++++++++++++++- custom/jt808/src/jt808_util.c | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/custom/jt808/inc/jt808_util.h b/custom/jt808/inc/jt808_util.h index 6c6c12d..ede15eb 100644 --- a/custom/jt808/inc/jt808_util.h +++ b/custom/jt808/inc/jt808_util.h @@ -34,4 +34,12 @@ uint8_t *BcdToStr(uint8_t *str, const uint8_t *bcd, int bcd_len); // strlen 为原始BCD数据字节数 uint8_t *rawBcdToStr(uint8_t *str, const uint8_t *bcd, int bcd_len); +/** + * @brief 将ASCII字符串转换为BCD编码 + * @param ascii 输入字符串 + * @param len 字符串长度 + * @param bcd_out 输出BCD数组 + */ +void ascii_to_bcd(const char* ascii, size_t len, uint8_t* bcd_out); + #endif // JT808_UTIL_H_ diff --git a/custom/jt808/src/jt808_set_TermParam.c b/custom/jt808/src/jt808_set_TermParam.c index 431c796..c3511d2 100644 --- a/custom/jt808/src/jt808_set_TermParam.c +++ b/custom/jt808/src/jt808_set_TermParam.c @@ -667,7 +667,7 @@ void jt808_set_term_param_init(void){ memcpy(jt808_term_param_item.big_term_attr_resp.manufacturer_id, "LAT01", 5); // 制造商ID memcpy(jt808_term_param_item.big_term_attr_resp.term_model, jt808_term_param_item.big_reg_info.term_model, 20); // 终端型号 memcpy(jt808_term_param_item.big_term_attr_resp.term_id, jt808_term_param_item.big_reg_info.term_id, 7); // 终端ID - memcpy(jt808_term_param_item.big_term_attr_resp.term_ICCID+4, jt808_term_param_item.phone_BCDnum, 6); // 终端手机号码 + // memcpy(jt808_term_param_item.big_term_attr_resp.term_ICCID+4, jt808_term_param_item.phone_BCDnum, 6); // 终端手机号码 char str_hw_ver[] = "1.0.0"; // 硬件版本 char str_fw_ver[] = "1.0.1"; // 固件版本 原本为1.0.0 @@ -771,6 +771,21 @@ void jt808_set_term_param_init(void){ JT808_DEBUG_DATA("%02X ",jt808_term_param_item.phone_BCDnum[i]); } JT808_DEBUG_DATA("\n"); + + size_t iccid_len = strlen(cm_iccid); + if(iccid_len > 0) { + // 确保不超过term_ICCID容量(10字节=20位BCD) + if(iccid_len > 20) iccid_len = 20; + + // 将ASCII ICCID转换为BCD格式 + uint8_t bcd_buffer[10] = {0}; + ascii_to_bcd(cm_iccid, iccid_len, bcd_buffer); + + // 复制到终端属性结构体 + memcpy(jt808_term_param_item.big_term_attr_resp.term_ICCID, + bcd_buffer, + sizeof(jt808_term_param_item.big_term_attr_resp.term_ICCID)); + } }while(0); do{// 注册信息初始化 char read_buf[35] = {0}; diff --git a/custom/jt808/src/jt808_util.c b/custom/jt808/src/jt808_util.c index 6a26864..0822216 100644 --- a/custom/jt808/src/jt808_util.c +++ b/custom/jt808/src/jt808_util.c @@ -91,3 +91,20 @@ uint8_t *rawBcdToStr(uint8_t *str, const uint8_t *bcd, int bcd_len){ } return str; } + + +/** + * @brief 将ASCII字符串转换为BCD编码 + * @param ascii 输入字符串 + * @param len 字符串长度 + * @param bcd_out 输出BCD数组 + */ +void ascii_to_bcd(const char* ascii, size_t len, uint8_t* bcd_out) { + size_t bcd_len = (len + 1) / 2; // 计算需要的BCD字节数 + + for(size_t i = 0; i < bcd_len; i++) { + uint8_t high = (i*2 < len) ? (ascii[i*2] - '0') : 0; + uint8_t low = (i*2+1 < len) ? (ascii[i*2+1] - '0') : 0; + bcd_out[i] = (high << 4) | low; + } +} \ No newline at end of file