Browse Source

增加malloc动态内存申请,数据拼接,上传测试通过

CHENJIE-PC\QiXiang_CHENJIE 4 years ago
parent
commit
cdf0fb208c
1 changed files with 54 additions and 51 deletions
  1. 54 51
      app.c

+ 54 - 51
app.c

@@ -71,14 +71,8 @@ uint16_t Uart_Rece_Batt_MaxcellU;
 uint16_t Uart_Rece_Batt_MincellU;
 uint8_t Can_Rece_buffer[8];
 
-uint8_t battbuffer[67] = {  0x14,0x0B,0x07,0x0E,0x39,0x37,
-                            0x80,0x14,0x0B,0x07,0x0E,0x39,0x37,
-                            0x1A,0x00,0x00,0x00,0x00,0x00,0x02,0x15,0x02,0x15,0x03,0x43,0x64,
-                            0x00,0x00,0x00,0x00,0x0E,
-                            0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,
-                            0x05,0x13,0x13,0x13,0x13,0x13,0x00,
-                            0x00};
-uint16_t data_len = 0x0000;
+uint8_t battbuffer[67];
+uint16_t data_index = 0x0000;
 typedef enum
 {
     PROCESS_STATE_IDLE = 0,
@@ -111,7 +105,7 @@ static UINT8                    gProcessTaskStack3[PROC_TASK_STACK_SIZE];
 processSM 	    gProc0State = PROCESS_STATE_IDLE;
 process_Uart 	gProc1State = PROCESS_Uart_STATE_IDLE;
 processSM 	    gProc2State = PROCESS_STATE_IDLE;
-processSM_NB	    gProc3State = PROCESS_NB_STATE_IDLE;
+process_NB	    gProc3State = PROCESS_NB_STATE_IDLE;
 #define PROC_Task_STATE_SWITCH(a)  (gProc0State = a) //任务调度切换
 #define PROC_Uart_STATE_SWITCH(a)  (gProc1State = a) //uart 状态切换
 #define PROC_Can_STATE_SWITCH(a)  (gProc2State = a) //can 状态切换
@@ -138,17 +132,6 @@ unsigned int crc_chk(uint8_t* data, uint8_t length)
     }
     return reg_crc;
 }
-uint8_t bcc_chk(uint8_t* data, uint8_t length)
-{
-    uint8_t bcc_chk_return = 0x00;
-    uint8_t count = 0;
-    while (count<length)
-    {
-        bcc_chk_return^=data[count];
-        count++;
-    }
-    return  bcc_chk_return;
-}
 uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg)
 {
     uint8_t Uart_Rece_buffer[Uart_Rece_LEN];
@@ -292,7 +275,7 @@ static void process0AppTask(void* arg)
                 if (Can_index >=100)//Can 1000ms 调用一次
                 {
                     PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK);
-                    PROC_NB_STATE_SWITCH(PROCESS_STATE_CONNECT);
+                    PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_CONNECT);
                     Can_index = 0;
                 }
                 if((Uart_Rece_BattI==0x0000)&&(Can_Rece_buffer[0]==0xff))
@@ -661,6 +644,17 @@ static void process2AppTask(void* arg)
         }
     }
 }
+uint8_t bcc_chk(uint8_t* data, uint8_t length)
+{
+    uint8_t bcc_chk_return = 0x00;
+    uint8_t count = 0;
+    while (count<length)
+    {
+        bcc_chk_return^=data[count];
+        count++;
+    }
+    return  bcc_chk_return;
+}
 void TcpCallBack(void)
 {
     #ifdef USING_PRINTF
@@ -669,7 +663,7 @@ void TcpCallBack(void)
 }
 void Tcp_Data_Assemble(uint8_t datatype)
 {
-    uint8_t data_index;//数据位置索引
+    OsaUtcTimeTValue timestracture;
     appGetSystemTimeUtcSync(&timestracture);
     battbuffer[0] = timestracture.UTCtimer1>>16;
     battbuffer[0] = battbuffer[0] - 0x07D0;
@@ -709,30 +703,33 @@ void Tcp_Data_Assemble(uint8_t datatype)
         battbuffer[28] = 0x00;//均衡状态9-16
         battbuffer[29] = 0x00;//均衡状态1-8
         battbuffer[30] = 0x0E;//单体个数
-        data_index = 30;
+        data_index = 31;
         for (size_t i = 0; i < 0x0e; i++)
         {
-            battbuffer[31+i] = 0x00;
+            battbuffer[data_index] = 0x0E;
             data_index ++;
+            battbuffer[data_index] = 0x53;
+            data_index ++;
+            
         }
-        battbuffer[31+2*14] = 0x05;//温度采集个数
+        battbuffer[data_index] = 0x05;//温度采集个数
         data_index++;
         for (size_t i = 0; i < 0x05; i++)
         {
-            battbuffer[32+2*14+i] = 0x00;
+            battbuffer[data_index] = 0x00;
             data_index++;
         }
         battbuffer[data_index] = 0x00;//电池状态
         data_index++;
         battbuffer[data_index] = 0x00;//是否加热
+        data_index++;
+
         break;
     }
     default:
         break;
     }
-
-    data_len = sizeof(battbuffer);
-    return 0;
+    return;
 }
 static void process3AppTask(void* arg)
 {
@@ -741,17 +738,9 @@ static void process3AppTask(void* arg)
     UINT16 serverport = 8712;
     int TcpConnectID = -1;
     int TcpSendID = -1;
+    int NB_send_len=92;
 	PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
-    OsaUtcTimeTValue timestracture;
 
-    uint8_t TcpSendBuffer[92];
-    memset(TcpSendBuffer,0,92);
-    TcpSendBuffer[0] = 0x23;
-    TcpSendBuffer[1] = 0x23;
-    TcpSendBuffer[2] = 0x02;
-    TcpSendBuffer[3] = 0xfe;
-    memcpy(&TcpSendBuffer[4],SN,17);
-    TcpSendBuffer[21] = 0x01;//不加密
     while(1)
     {
         switch(gProc3State)
@@ -771,38 +760,52 @@ static void process3AppTask(void* arg)
             }
             case PROCESS_NB_STATE_WORK:
             {
+                uint8_t* TcpSendBuffer;
+                TcpSendBuffer = (uint8_t *)malloc(NB_send_len);//申请发送的数据内存
+                if (!TcpSendBuffer)
+                {
+                    #ifdef USING_PRINTF
+                        printf("[%d]malloc error! \r\n",__LINE__);
+                    #endif
+                }
+                memset(TcpSendBuffer,0x00,92);
+                *(TcpSendBuffer+0) = 0x23;
+                *(TcpSendBuffer+1) = 0x23;
+                *(TcpSendBuffer+2) = 0x02;
+                *(TcpSendBuffer+3) = 0xfe;
+                memcpy(TcpSendBuffer+4,SN,17);
+                *(TcpSendBuffer+21) = 0x01;//不加密
                 Tcp_Data_Assemble(0x80);
 
                 
-                TcpSendBuffer[22] = data_len>>8;//数据长度
-                TcpSendBuffer[23] = data_len;//数据长度
+                *(TcpSendBuffer+22) = data_index>>8;//数据长度
+                *(TcpSendBuffer+23) = data_index;//数据长度
 
-                memcpy(&TcpSendBuffer[24],battbuffer,data_len);
-                TcpSendBuffer[91] = bcc_chk(TcpSendBuffer,sizeof(TcpSendBuffer)-1);
+                memcpy(TcpSendBuffer+24,battbuffer,data_index);
+                *(TcpSendBuffer+91) = bcc_chk(TcpSendBuffer,NB_send_len-1);
                 #ifdef USING_PRINTF
                     printf("[%d]sizeof:%d \r\n",__LINE__,sizeof(TcpSendBuffer)-1);
                 #endif
                 #ifdef USING_PRINTF
-                    printf("[%d]Tcpchk:%#X \r\n",__LINE__,TcpSendBuffer[91]);
+                    printf("[%d]Tcpchk:%#X \r\n",__LINE__,*(TcpSendBuffer+91));
                 #endif
                 #ifdef USING_PRINTF
-                    printf("[%d]datalen:%#X \r\n",__LINE__,data_len);
-                #endif
-                #ifdef USING_PRINTF
-                    printf("[%d]Time1:%#X TIme2:%#X \r\n",__LINE__,timestracture.UTCtimer1,timestracture.UTCtimer2);
+                    printf("[%d]datalen:%#X \r\n",__LINE__,data_index);
                 #endif
+
                 for (int i = 0; i < 92; i++)
                 {
                     #ifdef USING_PRINTF
-                        printf("%X ",TcpSendBuffer[i]);
+                        printf("%X ",*(TcpSendBuffer+i));
                     #endif
                 }
                 #ifdef USING_PRINTF
                         printf("\r\n ");
                 #endif
-                //TcpSendID  = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,sizeof(TcpSendBuffer),PNULL,PNULL,PNULL);
+                TcpSendID  = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,NB_send_len,PNULL,PNULL,PNULL);
 
                 PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE);
+                free(TcpSendBuffer);
                 break;
             }
             case PROCESS_NB_STATE_SLEEP:
@@ -893,10 +896,10 @@ void process3Init(void)
     osThreadAttr_t task_attr;
 
     memset(&task_attr,0,sizeof(task_attr));
-    memset(gProcessTaskStack3, 0xA5,2048);
+    memset(gProcessTaskStack3, 0xA5,PROC_TASK_STACK_SIZE);
     task_attr.name = "Process3AppTask";
     task_attr.stack_mem = gProcessTaskStack3;
-    task_attr.stack_size = 2048;
+    task_attr.stack_size = PROC_TASK_STACK_SIZE;
     task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
     task_attr.cb_mem = &gProcessTask3;//task control block
     task_attr.cb_size = sizeof(StaticTask_t);//size of task control block