lib内部printf_usb缓冲区长度64,不够打印超长字符串。需要自定义分包发送:
-
-
- #include "stc.h"
- #include "usb.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdarg.h>
-
-
- #define MAIN_Fosc 24000000L //定义主时钟
-
- char *USER_DEVICEDESC = NULL;
- char *USER_PRODUCTDESC = NULL;
- char *USER_STCISPCMD = "@STCISP#"; //设置自动复位到ISP区的用户接口命令
-
-
- void sys_init()
- {
- WTST = 0; //设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
- EAXFR = 1; //扩展寄存器(XFR)访问使能
- CKCON = 0; //提高访问XRAM速度
-
- P0M1 = 0x00; P0M0 = 0x00; //设置为准双向口
- P1M1 = 0x00; P1M0 = 0x00; //设置为准双向口
- P2M1 = 0x00; P2M0 = 0x00; //设置为准双向口
- P3M1 = 0x00; P3M0 = 0x00; //设置为准双向口
- P4M1 = 0x00; P4M0 = 0x00; //设置为准双向口
- P5M1 = 0x00; P5M0 = 0x00; //设置为准双向口
- P6M1 = 0x00; P6M0 = 0x00; //设置为准双向口
- P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
- }
-
- //========================================================================
- // 函数: void delay_ms(u8 ms)
- // 描述: 延时函数。
- // 参数: ms,要延时的ms数, 这里只支持1~255ms. 自动适应主时钟.
- // 返回: none.
- // 版本: VER1.0
- // 日期: 2021-3-9
- // 备注:
- //========================================================================
- void delay_ms(u8 ms)
- {
- u16 i;
- do{
- i = MAIN_Fosc / 6000;
- while(--i); //6T per loop
- }while(--ms);
- }
-
- // 自定义printf,每次只传递64字节给printf,实现分包输出
- void my_printf(const char *format, ...)
- {
- va_list args;
- char xdata buffer[1024] = {0}; // 临时缓冲区,根据实际需求调整大小
- u16 len;
- u16 pos = 0;
- va_start(args, format);
- len = vsprintf(buffer, format, args);
- va_end(args);
-
- // 每次输出64字节
- while (pos < len)
- {
- u8 chunk_len = 64;
- if (pos + chunk_len > len)
- {
- chunk_len = len - pos;
- }
- USB_SendData(buffer + pos, chunk_len);
- pos += chunk_len;
- }
- }
- int main()
- {
- u8 xdata Send_buff[1024]={0};
- // u8 xdata buff[255]={0};
-
- u8 xdata json_buf[1024] = {0}; // 使用1024字节缓冲区
- u16 pos = 0;
-
-
- u32 xdata Work_ID_number = 1000;
-
- u16 xdata Work_TRC_TEMP1 = 230;
- u16 xdata Work_TRC_TEMP2 = 230;
- u16 xdata Work_TRC_TEMP3 = 230;
- u16 xdata Work_TRC_TEMP4 = 230;
-
- u16 xdata Work_TRC_RH1 = 555;
- u16 xdata Work_TRC_RH2 = 555;
- u16 xdata Work_TRC_RH3 = 555;
- u16 xdata Work_TRC_RH4 = 555;
-
- u16 xdata Work_TRC_CO2_1 = 995;
- u16 xdata Work_TRC_CO2_2 = 995;
- u16 xdata Work_TRC_CO2_3 = 995;
- u16 xdata Work_TRC_CO2_4 = 995;
-
- float xdata Work_DB_U = 223.2;
- float xdata Work_DB_I = 1.2;
- float xdata Work_DB_Freq = 49.7;
- float xdata Work_DB_P = 100.1;
- float xdata Work_DB_Q = 200.1;
- float xdata Work_DB_S = 300.1;
- float xdata Work_DB_FP = 1.0;
- float xdata Work_DB_EP = 123;
- /*
- u16 xdata Work_DB_U = 2232;
- u16 xdata Work_DB_I = 12;
- u16 xdata Work_DB_Freq = 497;
- u16 xdata Work_DB_P = 1001;
- u16 xdata Work_DB_Q = 2001;
- u16 xdata Work_DB_S = 3001;
- u16 xdata Work_DB_FP = 10;
- u16 xdata Work_DB_EP = 123;
- */
- u8 xdata Work_PR_state = 1;
-
- float xdata Work_PR_Volt = 12.1;
- //u16 xdata Work_PR_Volt = 121;
-
- u8 xdata Work_Setout[12]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
-
- u8 xdata Work_SetKTONOFF = 1;
- u8 xdata Work_SetKTMODE = 1;
- u16 xdata Work_SetKTTEMP = 23;
- u16 xdata Work_SetKTRH = 50;
-
- u8 xdata Work_Smoke_state = 1;
- u8 xdata Work_Setbj = 1;
-
-
-
- sys_init(); //系统初始化
- usb_init(); //USB CDC 接口配置
- EA = 1;
-
-
-
- while(1)
- {
- if(DeviceState != DEVSTATE_CONFIGURED) //等待USB完成配置
- continue;
-
- if(bUsbOutReady)
- {
- usb_OUT_done(); //接收应答(固定格式)
- //USB_SendData(UsbOutBuffer,OutNumber); //发送数据缓冲区,LEN
- memset(json_buf,0,sizeof(json_buf));
-
- // 步骤1:构建JSON头部 - code字段
- pos += sprintf(json_buf + pos, "{"code":"XYT-GY%09ld",", Work_ID_number);
- my_printf("步骤1(code):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤2:拼接type+data开头
- pos += sprintf(json_buf + pos, ""type":31,"data":{");
- my_printf("步骤2(type):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤3:拼接TEMP字段
- pos += sprintf(json_buf + pos, ""TEMP":[%d,%d,%d,%d],",
- Work_TRC_TEMP1, Work_TRC_TEMP2, Work_TRC_TEMP3, Work_TRC_TEMP4);
- my_printf("步骤3(TEMP):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤4:拼接RH字段
- pos += sprintf(json_buf + pos, ""RH":[%d,%d,%d,%d],",
- Work_TRC_RH1, Work_TRC_RH2, Work_TRC_RH3, Work_TRC_RH4);
- my_printf("步骤4(RH):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤5:拼接CO2字段
- pos += sprintf(json_buf + pos, ""CO2":[%d,%d,%d,%d],",
- Work_TRC_CO2_1, Work_TRC_CO2_2, Work_TRC_CO2_3, Work_TRC_CO2_4);
- my_printf("步骤5(CO2):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤6:拼接DB字段(浮点数)
- // pos += sprintf(json_buf + pos, ""DB":[%.1f,%.1f,%.1f,%.1f,",
- // Work_DB_U, Work_DB_I, Work_DB_Freq, Work_DB_P);
-
- pos += sprintf(json_buf + pos, ""DB":[");
- pos += sprintf(json_buf + pos, "%.1f,", Work_DB_U);
- pos += sprintf(json_buf + pos, "%.1f,", Work_DB_I);
- pos += sprintf(json_buf + pos, "%.1f,", Work_DB_Freq);
- pos += sprintf(json_buf + pos, "%.1f,", Work_DB_P);
- pos += sprintf(json_buf + pos, "%.1f,", Work_DB_Q);
- pos += sprintf(json_buf + pos, "%.1f,", Work_DB_S);
- pos += sprintf(json_buf + pos, "%.1f,", Work_DB_FP);
-
- my_printf("步骤6(DB):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤7:拼接Power字段
- pos += sprintf(json_buf + pos, ""Power":[%d,%.1f],",
- Work_PR_state, Work_PR_Volt);
- my_printf("步骤7(Power):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤8:拼接OUTstate字段(12个整数)
-
- pos += sprintf(json_buf + pos, ""OUTstate":[%d,",Work_Setout[0]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[1]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[2]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[3]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[4]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[5]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[6]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[7]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[8]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[9]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[10]);
- pos += sprintf(json_buf + pos, "%d,",Work_Setout[11]);
-
- my_printf("步骤8(OUTstate):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤9:拼接KTstate字段
- pos += sprintf(json_buf + pos, ""KTstate":[%d,%d,%d,%d],",
- Work_SetKTONOFF, Work_SetKTMODE, Work_SetKTTEMP, Work_SetKTRH);
- my_printf("步骤9(KTstate):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤10:拼接YWstate字段
- pos += sprintf(json_buf + pos, ""YWstate":[%d],",
- Work_Smoke_state);
- my_printf("步骤10(YWstate):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤11:拼接BJstate字段(最后一个字段,无逗号)
- pos += sprintf(json_buf + pos, ""BJstate":[%d]",
- Work_Setbj);
- my_printf("步骤11(BJstate):%s\nLEN:%u\n\n", json_buf, pos);
-
- // 步骤12:闭合JSON结构
- pos += sprintf(json_buf + pos, "}}\r\n");
- my_printf("步骤12(最终完整JSON):%s\n总LEN:%u\n", json_buf, pos);
- }
- }
- }
-
-
-
-
-
-
-
-
复制代码
|