找回密码
 立即注册
查看: 320|回复: 3

字符串拼接异常?| 可以了

[复制链接]
  • 打卡等级:以坛为家II
  • 打卡总天数:458
  • 最近打卡:2026-03-27 09:06:07
已绑定手机

39

主题

326

回帖

942

积分

高级会员

积分
942
发表于 2025-12-17 22:53:02 | 显示全部楼层 |阅读模式
  1. 输出异常!!!!
复制代码


截图202512172251359579.jpg

USB_CDC JSON 拼接.zip

130.87 KB, 下载次数: 6

回复

使用道具 举报 送花

  • 打卡等级:以坛为家II
  • 打卡总天数:493
  • 最近打卡:2026-04-02 08:56:15
已绑定手机

104

主题

4229

回帖

9395

积分

荣誉版主

无情的代码机器

积分
9395
发表于 2025-12-18 00:21:57 | 显示全部楼层
lib内部printf_usb缓冲区长度64,不够打印超长字符串。需要自定义分包发送:
  1. #include "stc.h"
  2. #include "usb.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8. #define MAIN_Fosc       24000000L   //定义主时钟
  9. char *USER_DEVICEDESC = NULL;
  10. char *USER_PRODUCTDESC = NULL;
  11. char *USER_STCISPCMD = "@STCISP#";                      //设置自动复位到ISP区的用户接口命令
  12. void sys_init()
  13. {
  14.     WTST = 0;  //设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
  15.     EAXFR = 1; //扩展寄存器(XFR)访问使能
  16.     CKCON = 0; //提高访问XRAM速度
  17.     P0M1 = 0x00;   P0M0 = 0x00;   //设置为准双向口
  18.     P1M1 = 0x00;   P1M0 = 0x00;   //设置为准双向口
  19.     P2M1 = 0x00;   P2M0 = 0x00;   //设置为准双向口
  20.     P3M1 = 0x00;   P3M0 = 0x00;   //设置为准双向口
  21.     P4M1 = 0x00;   P4M0 = 0x00;   //设置为准双向口
  22.     P5M1 = 0x00;   P5M0 = 0x00;   //设置为准双向口
  23.     P6M1 = 0x00;   P6M0 = 0x00;   //设置为准双向口
  24.     P7M1 = 0x00;   P7M0 = 0x00;   //设置为准双向口
  25. }
  26. //========================================================================
  27. // 函数: void delay_ms(u8 ms)
  28. // 描述: 延时函数。
  29. // 参数: ms,要延时的ms数, 这里只支持1~255ms. 自动适应主时钟.
  30. // 返回: none.
  31. // 版本: VER1.0
  32. // 日期: 2021-3-9
  33. // 备注:
  34. //========================================================================
  35. void delay_ms(u8 ms)
  36. {
  37.      u16 i;
  38.      do{
  39.           i = MAIN_Fosc / 6000;
  40.           while(--i);   //6T per loop
  41.      }while(--ms);
  42. }
  43. // 自定义printf,每次只传递64字节给printf,实现分包输出
  44. void my_printf(const char *format, ...)
  45. {
  46.     va_list args;
  47.     char xdata buffer[1024] = {0}; // 临时缓冲区,根据实际需求调整大小
  48.                 u16 len;
  49.                 u16 pos = 0;
  50.     va_start(args, format);
  51.     len = vsprintf(buffer, format, args);
  52.     va_end(args);
  53.    
  54.     // 每次输出64字节
  55.     while (pos < len)
  56.     {
  57.         u8 chunk_len = 64;
  58.         if (pos + chunk_len > len)
  59.         {
  60.             chunk_len = len - pos;
  61.         }
  62.         USB_SendData(buffer + pos, chunk_len);
  63.         pos += chunk_len;
  64.     }
  65. }
  66. int main()
  67. {       
  68.         u8 xdata Send_buff[1024]={0};
  69. //        u8 xdata buff[255]={0};
  70.        
  71.         u8 xdata json_buf[1024] = {0};  // 使用1024字节缓冲区
  72.     u16 pos = 0;
  73.        
  74.        
  75.         u32 xdata Work_ID_number = 1000;
  76.        
  77.         u16 xdata Work_TRC_TEMP1 = 230;
  78.         u16 xdata Work_TRC_TEMP2 = 230;
  79.         u16 xdata Work_TRC_TEMP3 = 230;
  80.         u16 xdata Work_TRC_TEMP4 = 230;
  81.        
  82.         u16 xdata Work_TRC_RH1 = 555;
  83.         u16 xdata Work_TRC_RH2 = 555;
  84.         u16 xdata Work_TRC_RH3 = 555;
  85.         u16 xdata Work_TRC_RH4 = 555;
  86.        
  87.         u16 xdata Work_TRC_CO2_1 = 995;
  88.         u16 xdata Work_TRC_CO2_2 = 995;
  89.         u16 xdata Work_TRC_CO2_3 = 995;
  90.         u16 xdata Work_TRC_CO2_4 = 995;
  91.        
  92.         float xdata Work_DB_U = 223.2;       
  93.         float xdata Work_DB_I = 1.2;
  94.         float xdata Work_DB_Freq = 49.7;
  95.         float xdata Work_DB_P = 100.1;
  96.         float xdata Work_DB_Q  = 200.1;
  97.         float xdata Work_DB_S  = 300.1;
  98.         float xdata Work_DB_FP = 1.0;
  99.         float xdata Work_DB_EP = 123;
  100. /*       
  101.         u16 xdata Work_DB_U = 2232;       
  102.         u16 xdata Work_DB_I = 12;
  103.         u16 xdata Work_DB_Freq = 497;
  104.         u16 xdata Work_DB_P = 1001;
  105.         u16 xdata Work_DB_Q  = 2001;
  106.         u16 xdata Work_DB_S  = 3001;
  107.         u16 xdata Work_DB_FP = 10;
  108.         u16 xdata Work_DB_EP = 123;
  109. */       
  110.         u8 xdata Work_PR_state = 1;
  111.        
  112.         float xdata Work_PR_Volt = 12.1;
  113.         //u16 xdata Work_PR_Volt = 121;
  114.        
  115.         u8 xdata Work_Setout[12]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  116.        
  117.         u8 xdata Work_SetKTONOFF = 1;
  118.         u8 xdata Work_SetKTMODE = 1;
  119.         u16 xdata Work_SetKTTEMP = 23;
  120.         u16 xdata Work_SetKTRH = 50;
  121.        
  122.         u8 xdata Work_Smoke_state = 1;
  123.         u8 xdata Work_Setbj = 1;
  124.        
  125.        
  126.        
  127.         sys_init();  //系统初始化
  128.     usb_init();  //USB CDC 接口配置
  129.     EA = 1;
  130.        
  131.        
  132.        
  133.         while(1)
  134.         {
  135.                 if(DeviceState != DEVSTATE_CONFIGURED)  //等待USB完成配置
  136.             continue;
  137.                
  138.                  if(bUsbOutReady)
  139.          {               
  140.                         usb_OUT_done();    //接收应答(固定格式)           
  141.                         //USB_SendData(UsbOutBuffer,OutNumber);    //发送数据缓冲区,LEN       
  142.                          memset(json_buf,0,sizeof(json_buf));
  143.                 // 步骤1:构建JSON头部 - code字段
  144.                         pos += sprintf(json_buf + pos, "{"code":"XYT-GY%09ld",", Work_ID_number);
  145.                         my_printf("步骤1(code):%s\nLEN:%u\n\n", json_buf, pos);
  146.                        
  147.                         // 步骤2:拼接type+data开头
  148.                         pos += sprintf(json_buf + pos, ""type":31,"data":{");
  149.                         my_printf("步骤2(type):%s\nLEN:%u\n\n", json_buf, pos);
  150.                        
  151.                         // 步骤3:拼接TEMP字段
  152.                         pos += sprintf(json_buf + pos, ""TEMP":[%d,%d,%d,%d],",
  153.                                                   Work_TRC_TEMP1, Work_TRC_TEMP2, Work_TRC_TEMP3, Work_TRC_TEMP4);
  154.                                          my_printf("步骤3(TEMP):%s\nLEN:%u\n\n", json_buf, pos);
  155.                        
  156.                         // 步骤4:拼接RH字段
  157.                         pos += sprintf(json_buf + pos, ""RH":[%d,%d,%d,%d],",
  158.                                                   Work_TRC_RH1, Work_TRC_RH2, Work_TRC_RH3, Work_TRC_RH4);
  159.                         my_printf("步骤4(RH):%s\nLEN:%u\n\n", json_buf, pos);
  160.                        
  161.                                 // 步骤5:拼接CO2字段
  162.                         pos += sprintf(json_buf + pos, ""CO2":[%d,%d,%d,%d],",
  163.                                                   Work_TRC_CO2_1, Work_TRC_CO2_2, Work_TRC_CO2_3, Work_TRC_CO2_4);
  164.                         my_printf("步骤5(CO2):%s\nLEN:%u\n\n", json_buf, pos);
  165.                        
  166.                         // 步骤6:拼接DB字段(浮点数)
  167.                   //  pos += sprintf(json_buf + pos, ""DB":[%.1f,%.1f,%.1f,%.1f,",
  168.                   //                Work_DB_U, Work_DB_I, Work_DB_Freq, Work_DB_P);
  169.                                                   
  170.                         pos += sprintf(json_buf + pos, ""DB":[");
  171.                         pos += sprintf(json_buf + pos, "%.1f,", Work_DB_U);
  172.                         pos += sprintf(json_buf + pos, "%.1f,", Work_DB_I);
  173.                         pos += sprintf(json_buf + pos, "%.1f,", Work_DB_Freq);
  174.                         pos += sprintf(json_buf + pos, "%.1f,", Work_DB_P);
  175.                         pos += sprintf(json_buf + pos, "%.1f,", Work_DB_Q);
  176.                         pos += sprintf(json_buf + pos, "%.1f,", Work_DB_S);
  177.                         pos += sprintf(json_buf + pos, "%.1f,", Work_DB_FP);
  178.                         my_printf("步骤6(DB):%s\nLEN:%u\n\n", json_buf, pos);
  179.                          
  180.                         // 步骤7:拼接Power字段
  181.                         pos += sprintf(json_buf + pos, ""Power":[%d,%.1f],",
  182.                                                   Work_PR_state, Work_PR_Volt);
  183.                         my_printf("步骤7(Power):%s\nLEN:%u\n\n", json_buf, pos);
  184.                        
  185.                           // 步骤8:拼接OUTstate字段(12个整数)
  186.                           
  187.                         pos += sprintf(json_buf + pos, ""OUTstate":[%d,",Work_Setout[0]);
  188.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[1]);
  189.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[2]);
  190.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[3]);
  191.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[4]);
  192.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[5]);
  193.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[6]);
  194.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[7]);
  195.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[8]);
  196.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[9]);
  197.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[10]);
  198.                         pos += sprintf(json_buf + pos, "%d,",Work_Setout[11]);
  199.                                                                   
  200.                         my_printf("步骤8(OUTstate):%s\nLEN:%u\n\n", json_buf, pos);
  201.                        
  202.                         // 步骤9:拼接KTstate字段
  203.                         pos += sprintf(json_buf + pos, ""KTstate":[%d,%d,%d,%d],",
  204.                                                   Work_SetKTONOFF, Work_SetKTMODE, Work_SetKTTEMP, Work_SetKTRH);
  205.                         my_printf("步骤9(KTstate):%s\nLEN:%u\n\n", json_buf, pos);
  206.                        
  207.                         // 步骤10:拼接YWstate字段
  208.                         pos += sprintf(json_buf + pos, ""YWstate":[%d],",
  209.                                                   Work_Smoke_state);
  210.                         my_printf("步骤10(YWstate):%s\nLEN:%u\n\n", json_buf, pos);
  211.                        
  212.                          // 步骤11:拼接BJstate字段(最后一个字段,无逗号)
  213.                         pos += sprintf(json_buf + pos, ""BJstate":[%d]",
  214.                                                   Work_Setbj);
  215.                         my_printf("步骤11(BJstate):%s\nLEN:%u\n\n", json_buf, pos);
  216.                        
  217.                         // 步骤12:闭合JSON结构
  218.                         pos += sprintf(json_buf + pos, "}}\r\n");
  219.                         my_printf("步骤12(最终完整JSON):%s\n总LEN:%u\n", json_buf, pos);
  220.                 }
  221.         }
  222. }
复制代码

三天不学习,赶不上刘少奇~
回复

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:236
  • 最近打卡:2026-04-02 08:05:52

16

主题

234

回帖

478

积分

中级会员

积分
478
发表于 2025-12-18 08:38:15 | 显示全部楼层
我正在尝试做JSON的解析
回复

使用道具 举报 送花

  • 打卡等级:以坛为家II
  • 打卡总天数:458
  • 最近打卡:2026-03-27 09:06:07
已绑定手机

39

主题

326

回帖

942

积分

高级会员

积分
942
发表于 2025-12-18 09:11:32 | 显示全部楼层
erci*** 发表于 2025-12-18 00:21
lib内部printf_usb缓冲区长度64,不够打印超长字符串。需要自定义分包发送:

...

测试可以了。感谢,我还一直在sprintf函数上找问题呢。方向都搞错了
回复

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2026-4-2 21:06 , Processed in 0.106251 second(s), 61 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表