野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2882|回复: 3

类似于TCP三次握手、超时检测以及自动重发机制的通信任务该如何设计

[复制链接]
发表于 2020-1-17 14:44:03 | 显示全部楼层 |阅读模式
我的设备的USART1和一个小模块进行通信,但是通信之前需要握手,过程如下:

        第一步:  我的设备先定时(定时200毫秒时间,时间不需要非常准确)给小模块发送握手请求报文A
                    设备正常收到小模块的应答报文后,跳转到第二步;否则重复发送请求报文A
        第二步:   我的设备先定时(定时200毫秒时间,时间不需要非常准确)给小模块发送握手请求报文B
                    设备正常收到小模块的应答报文后,跳转到第三步;
                    否则重复发送请求报文B,如果自动重发次数超过3,则跳转到第一步,重新发送握手请求报文A。
       第三步: 我的设备开始定时(定时500毫秒时间,时间不需要非常准确)给小模块发送请求报文C。
                    请求报文C中包含要发送的数据从USART2通信和USART3通信以及其它任务中读取。
                    如果长时间(假如1分钟,时间不需要非常准确)没有收到小模块的应答报文(心跳包报文),则跳转到第一步,重新发送握手请求报文A。

     请问:我该如何设计和规划通信任务?
回复

使用道具 举报

 楼主| 发表于 2020-1-17 14:44:32 | 显示全部楼层
通信要求如下:

      步骤1:
                 发送请求报文A,然后等待应答报文;
                 (1)、如果收到应答报文,跳转到步骤2.
                 (2)、如果没有收到应答,定时重发发送请求报文A。
      步骤2:
                 发送请求报文B,然后等待应答报文;
                 (1)、如果收到应答报文,跳转到步骤3.
                 (2)、如果超时未等到应答报文,重发发送请求报文B,如果连续3次超时,则跳转到步骤1。
      步骤3:
                 (1)、等待接收USART2的通信数据。
                 (2)、等待接收USART3的通信数据。
                 (3)、IF (先后收到USART2的通信数据和USART3的通信数据)
                       {
                          发送请求报文C
                         }

                 (4)、等待应答报文。
                  (5)、如果连续1分钟没有收到应答报文,则跳转到步骤1



  我的任务设计如下,是否可行,请指教!
       如果有缺陷,请指出好的方法!            


   事先创建2个消息队列,以下简称消息队列A和消息队列B
       消息队列A用来接收应答报文
       消息队列B用来接收USART2和USART3的通信数据


      void   my_task(void)
     {
            static int step = 0;   // 0=发送请求报文A,1=发送请求报文B,2=发送请求报文C
            static int count = 0;  //计数器,自动重发超过3次,跳转到step=0,转去发送请求报文A
            static int time = 0;   //计时,连续1分钟计时
            static int OK=0;       //是否收齐USART2和USART3的通信数据


//-------------------------定时发送USART1的请求报文部分
            switch (step)
            {
                   case 0:
                       调用子程序,发送发送请求报文A;
                       break;

                   case 1:
                         count++;
                         if (count > 3)   //自动重发超过3次,跳转到step=0
                        {
                             count =0;
                             step=0;
                         }
                         else
                         {
                              调用子程序,发送发送请求报文B;
                         }
                       break;   
                  
                    case 2:
                  
                       通过消息队列B,等待USART2的消息队列通信数据,如果收到OK|=0x01;
                       通过消息队列B,等待USART3的消息队列通信数据,如果收到OK|=0x02;
                       if (k==0x03) //收齐USART2和USART3的通信数据
                       {
                          OK=0;
                          调用子程序,发送发送请求报文C;
                       }

              }            


   
//-------------------------等待USART1的应答报文部分

             通过消息队列A,等待usart1的消息队列(最大等待时间=500ms)
             switch (step)
             {
                   case 0:
                        if (消息队列收到消息)
                        {
                           解析报文
                           if (报文ok)
                             step = 1;
                        }
                        break;
                   case 1:
                        if (消息队列收到消息)
                        {
                           解析报文
                           if (报文ok)
                           {
                               count =0;//自动重发次数清零
                               step = 2;
                            }
                        else
                        {
                            count++:   //自动重发次数+1
                        }
                        break;   
                   case 2:
                        if (消息队列收到消息)
                        {
                            解析报文
                            time = 0x00;//连续1分钟计时单元清零
                        }
                        else
                        {
                           time++;//连续1分钟计时单元+1
                           if (time >= 20) //如果1分钟到时
                           {
                               OK=0;
                               count=0;
                               time=0;
                           }
                         }
                         break;
            }      
      }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-17 14:45:33 | 显示全部楼层
疑问:
       1、在上线握手过程中,USART2和USART3的通信数据导致消息队列B满该怎么办呢?(注:USART2和USART3的通信数据本机无法控制)。
       2、对系统有影响吗?
       3、该如何消除?
       4、是增加限制条件吗?(即:握手不成功时,USART2和USART3的通信任务不发送消息)
               这种增加限制条件,即条件不满足时不发送消息队列数据,是否太牵强了!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-12 16:39:05 | 显示全部楼层
ba_wang_mao 发表于 2020-1-17 14:45
疑问:
       1、在上线握手过程中,USART2和USART3的通信数据导致消息队列B满该怎么办呢?(注:USART2和 ...

经过一段时间的学习,这个问题我已经编程解决了,代码如下


  1. void hex_to_string(uint8_t hex_buffer[] , char *string , uint16_t n)
  2. {
  3.         uint16_t i;       
  4.         char *p = string;


  5.         for (i = 0 ; i < n ; i++)
  6.                 *p++ = hex_buffer[i];

  7.         *p = '\0';
  8. }





  9. uint8_t IOT_Connect_Buffer_Init(char send_string[] , uint8_t send_buffer[])
  10. {
  11.         char *q = send_string;       
  12.         uint8_t *p = send_buffer;
  13.         uint8_t len;
  14.        
  15.         while (*q != '\0')
  16.                 *p++ = *q++;
  17.         len = strlen(send_string);
  18.        
  19.         return (len);
  20. }





  21. void IOT_4G_Poll(void)
  22. {
  23.         const TickType_t xTicksToWait = pdMS_TO_TICKS( 200UL );
  24.         BaseType_t xResult;
  25.         BaseType_t xStatus;
  26.         static uint16_t IOT_4G_Outtime = 0x00;
  27.         static uint16_t IOT_TCP_port_count = 0x00;
  28.         uint8_t Buffer[10];
  29.        
  30.        
  31.         switch (IOT_4G_isr)
  32.         {
  33.                 case 0:
  34.                         strcpy(IOT_send_string , "AT\r");
  35.                         MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);                               
  36.                         USART2_Begin_Send();
  37.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  38.                         if (xResult == pdTRUE)
  39.                         {
  40.                                 strcpy(IOT_target_string , "AT\r\r\nOK\r\n");
  41.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  42.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  43.                                 {
  44.                                         IOT_4G_Outtime = 0x00;
  45.                                         IOT_4G_isr = 1;
  46.                                         vTaskDelay(100);
  47.                                         break;
  48.                                 }                                       
  49.                         }
  50.                         vTaskDelay(100);
  51.                         break;
  52.                        
  53.                 case 1:                               
  54.                         if (++IOT_4G_Outtime > 2)
  55.                         {
  56.                                 IOT_4G_Outtime = 0x00;
  57.                                 IOT_4G_isr = 0;
  58.                                 break;
  59.                         }
  60.                         strcpy(IOT_send_string , "ATE1\r");
  61.                         MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);                               
  62.                         USART2_Begin_Send();
  63.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  64.                         if (xResult == pdTRUE)
  65.                         {
  66.                                 strcpy(IOT_target_string , "ATE1\r\r\nOK\r\n");
  67.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  68.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  69.                                 {
  70.                                         IOT_4G_Outtime = 0x00;
  71.                                         IOT_4G_isr = 2;
  72.                                         vTaskDelay(100);
  73.                                         break;
  74.                                 }                                       
  75.                         }
  76.                         vTaskDelay(100);
  77.                         break;
  78.                        
  79.                 case 2:
  80.                         if (++IOT_4G_Outtime > 2)
  81.                         {       
  82.                                 IOT_4G_Outtime = 0x00;                                               
  83.                                 IOT_4G_isr = 0;
  84.                                 break;
  85.                         }
  86.                         strcpy(IOT_send_string , "AT+CPIN?\r");
  87.                         MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);                               
  88.                         USART2_Begin_Send();
  89.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  90.                         if (xResult == pdTRUE)
  91.                         {
  92.                                 strcpy(IOT_target_string , "AT+CPIN?\r\r\n+CPIN: READY\r\n\r\nOK\r\n");
  93.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  94.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  95.                                 {
  96.                                         IOT_4G_Outtime = 0x00;
  97.                                         IOT_4G_isr = 3;
  98.                                         vTaskDelay(100);
  99.                                         break;
  100.                                 }
  101.                         }
  102.                         vTaskDelay(100);
  103.                         break;
  104.                        
  105.                 case 3:                       
  106.                         if (++IOT_4G_Outtime > 2)
  107.                         {       
  108.                                 IOT_4G_Outtime = 0x00;                                               
  109.                                 IOT_4G_isr = 0;
  110.                                 break;
  111.                         }               
  112.                         strcpy(IOT_send_string , "AT+CREG=?\r");
  113.                         MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);                               
  114.                         USART2_Begin_Send();
  115.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  116.                         if (xResult == pdTRUE)
  117.                         {
  118.                                 strcpy(IOT_target_string , "AT+CREG=?\r\r\n+CREG:");
  119.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  120.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  121.                                 {
  122.                                         IOT_4G_Outtime = 0x00;
  123.                                         IOT_4G_isr = 4;
  124.                                         vTaskDelay(100);
  125.                                         break;
  126.                                 }                                       
  127.                         }       
  128.                         vTaskDelay(100);                       
  129.                         break;
  130.                        
  131.                 case 4:
  132.                         if (++IOT_4G_Outtime > 2)
  133.                         {       
  134.                                 IOT_4G_Outtime = 0x00;                                               
  135.                                 IOT_4G_isr = 0;
  136.                                 break;
  137.                         }                       
  138.                         strcpy(IOT_send_string , "AT+CSQ\r");
  139.                         MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);                               
  140.                         USART2_Begin_Send();
  141.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  142.                         if (xResult == pdTRUE)
  143.                         {
  144.                                 strcpy(IOT_target_string , "AT+CSQ\r\r\n+CSQ:");
  145.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  146.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  147.                                 {
  148.                                         IOT_4G_Outtime = 0x00;
  149.                                         IOT_4G_isr = 5;
  150.                                         vTaskDelay(100);
  151.                                         break;
  152.                                 }                                       
  153.                         }       
  154.                         vTaskDelay(100);                       
  155.                         break;
  156.                        
  157.                 case 5:                       
  158.                         if (++IOT_4G_Outtime > 2)
  159.                         {       
  160.                                 IOT_4G_Outtime = 0x00;                                               
  161.                                 IOT_4G_isr = 0;
  162.                                 break;
  163.                         }       
  164.                         strcpy(IOT_send_string , "AT+QIDEACT=1\r");
  165.                         MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);                               
  166.                         USART2_Begin_Send();
  167.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  168.                         if (xResult == pdTRUE)
  169.                         {
  170.                                 strcpy(IOT_target_string , "AT+QIDEACT=1\r\r\nOK\r\n");
  171.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  172.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  173.                                 {
  174.                                         IOT_4G_Outtime = 0x00;
  175.                                         IOT_4G_isr = 7;
  176.                                         vTaskDelay(100);
  177.                                         break;
  178.                                 }                                       
  179.                         }       
  180.                         vTaskDelay(100);                       
  181.                         break;                       
  182.        

  183.                 case 7:
  184.                         strcpy(IOT_send_string , "AT+QIOPEN=1,0,"TCP","192.168.1.10",5000,0,2\r\0");
  185.                         MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);                               
  186.                         USART2_Begin_Send();                       
  187.                         vTaskDelay(15000 / portTICK_RATE_MS);               
  188.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  189.                         if (xResult == pdTRUE)
  190.                         {
  191.                                 strcpy(IOT_target_string , "\r\nCONNECT\r\n");
  192.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  193.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  194.                                 {
  195.                                         IOT_4G_Outtime = 0x00;
  196.                                         IOT_TCP_port_count = 0x00;
  197.                                         IOT_4G_isr = 20;
  198.                                         vTaskDelay(100);
  199.                                         KK4++;
  200.                                         break;
  201.                                 }
  202.                         }
  203.                        
  204.                         if (++IOT_TCP_port_count >= 8)
  205.                         {
  206.                                 IOT_TCP_port_count = 0x00;
  207.                                 IOT_4G_isr = 10;
  208.                                 KK5++;
  209.                                 break;
  210.                         }
  211.                        
  212.                         IOT_4G_isr = 0;                       
  213.                         KK6++;
  214.                         vTaskDelay(100);
  215.                         break;                                                                                       
  216.                        
  217.                 case 20:                       
  218.                         xStatus = xQueueReceive( xQueue_EC20, &Buffer, xTicksToWait);//portMAX_DELAY);
  219.                         if( xStatus == pdPASS )
  220.                         {                       
  221.                                 switch (Buffer[0])
  222.                                 {
  223.                                         case 1:
  224.                                                 IOT_DI1_1 = Buffer[1];
  225.                                                 IOT_DI1_2 = Buffer[2];
  226.                                                 break;
  227.                                         case 2:
  228.                                                 IOT_DI2_1 = Buffer[1];
  229.                                                 IOT_DI2_2 = Buffer[2];                                                       
  230.                                                 break;
  231.                                         case 3:
  232.                                                 IOT_Clock.year = Buffer[1];
  233.                                                 IOT_Clock.month = Buffer[2];
  234.                                                 IOT_Clock.day = Buffer[3];
  235.                                                 IOT_Clock.hour = Buffer[4];
  236.                                                 IOT_Clock.minute = Buffer[5];
  237.                                                 IOT_Clock.second = Buffer[6];
  238.                                                 IOT_Clock.week = Buffer[7];                                                                               
  239.                                                 break;
  240.                                 }//switch (Buffer[0])
  241.                         }//if( xStatus == pdPASS )
  242.                                                                
  243.                         MB_USART2.send_buffer[0] = IOT_DI1_1;
  244.                         MB_USART2.send_buffer[1] = IOT_DI1_2;
  245.                         MB_USART2.send_buffer[2] = IOT_DI2_1;
  246.                         MB_USART2.send_buffer[3] = IOT_DI2_2;               
  247.                         MB_USART2.send_buffer[4] = 0x20;
  248.                         MB_USART2.send_buffer[5] = IOT_Clock.year;
  249.                         MB_USART2.send_buffer[6] = IOT_Clock.month;
  250.                         MB_USART2.send_buffer[7] = IOT_Clock.day;
  251.                         MB_USART2.send_buffer[8] = IOT_Clock.hour;
  252.                         MB_USART2.send_buffer[9] = IOT_Clock.minute;
  253.                         MB_USART2.send_buffer[10] = IOT_Clock.second;
  254.                         MB_USART2.send_buffer[11] = 0x0D;
  255.                         MB_USART2.send_buffer[12] = 0x0A;
  256.                         MB_USART2.sendCount = 13;
  257.                         USART2_Begin_Send();
  258.                         //vTaskDelay(50);       
  259.                         xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
  260.                         if (xResult == pdTRUE)
  261.                         {
  262.                                 strcpy(IOT_target_string , "\r\nNO CARRIER\r\n");
  263.                                 hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
  264.                                 if (strstr(IOT_source_string , IOT_target_string) != NULL)
  265.                                 {
  266.                                         IOT_4G_Outtime = 0x00;
  267.                                         IOT_4G_isr = 10;
  268.                                         vTaskDelay(100);
  269.                                         break;
  270.                                 }
  271.                         }
  272.                         vTaskDelay(1000);
  273.                         break;
  274.                        
  275.                 default:
  276.                         vTaskDelay(100);
  277.                         break;
  278.         }//switch (IOT_4G_isr)
  279. }
复制代码
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-20 10:57 , Processed in 0.031914 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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