FreeRTOS下类似于TCP三次握手、超时检测以及自动重发机制的通信任务该如何设计
一、通信要求如下:
步骤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
二、源程序
源程序已经在stm32f103vc单片机下验证通过。
void hex_to_string(uint8_t hex_buffer[] , char *string , uint16_t n)
{
uint16_t i;
char *p = string;
for (i = 0 ; i < n ; i++)
*p++ = hex_buffer[i];
*p = '\0';
}
uint8_t IOT_Connect_Buffer_Init(char send_string[] , uint8_t send_buffer[])
{
char *q = send_string;
uint8_t *p = send_buffer;
uint8_t len;
while (*q != '\0')
*p++ = *q++;
len = strlen(send_string);
return (len);
}
void IOT_4G_Poll(void)
{
const TickType_t xTicksToWait = pdMS_TO_TICKS( 200UL );
BaseType_t xResult;
BaseType_t xStatus;
static uint16_t IOT_4G_Outtime = 0x00;
static uint16_t IOT_TCP_port_count = 0x00;
uint8_t Buffer[10];
switch (IOT_4G_isr)
{
case 0:
strcpy(IOT_send_string , "AT\r");
MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);
USART2_Begin_Send();
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "AT\r\r\nOK\r\n");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 1;
vTaskDelay(100);
break;
}
}
vTaskDelay(100);
break;
case 1:
if (++IOT_4G_Outtime > 2)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 0;
break;
}
strcpy(IOT_send_string , "ATE1\r");
MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);
USART2_Begin_Send();
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "ATE1\r\r\nOK\r\n");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 2;
vTaskDelay(100);
break;
}
}
vTaskDelay(100);
break;
case 2:
if (++IOT_4G_Outtime > 2)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 0;
break;
}
strcpy(IOT_send_string , "AT+CPIN?\r");
MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);
USART2_Begin_Send();
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "AT+CPIN?\r\r\n+CPIN: READY\r\n\r\nOK\r\n");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 3;
vTaskDelay(100);
break;
}
}
vTaskDelay(100);
break;
case 3:
if (++IOT_4G_Outtime > 2)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 0;
break;
}
strcpy(IOT_send_string , "AT+CREG=?\r");
MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);
USART2_Begin_Send();
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "AT+CREG=?\r\r\n+CREG:");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 4;
vTaskDelay(100);
break;
}
}
vTaskDelay(100);
break;
case 4:
if (++IOT_4G_Outtime > 2)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 0;
break;
}
strcpy(IOT_send_string , "AT+CSQ\r");
MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);
USART2_Begin_Send();
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "AT+CSQ\r\r\n+CSQ:");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 5;
vTaskDelay(100);
break;
}
}
vTaskDelay(100);
break;
case 5:
if (++IOT_4G_Outtime > 2)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 0;
break;
}
strcpy(IOT_send_string , "AT+QIDEACT=1\r");
MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);
USART2_Begin_Send();
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "AT+QIDEACT=1\r\r\nOK\r\n");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 7;
vTaskDelay(100);
break;
}
}
vTaskDelay(100);
break;
case 7:
strcpy(IOT_send_string , "AT+QIOPEN=1,0,\"TCP\",\"192.168.1.10\",5000,0,2\r\0");
MB_USART2.sendCount = IOT_Connect_Buffer_Init(IOT_send_string , MB_USART2.send_buffer);
USART2_Begin_Send();
vTaskDelay(15000 / portTICK_RATE_MS);
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "\r\nCONNECT\r\n");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_TCP_port_count = 0x00;
IOT_4G_isr = 20;
vTaskDelay(100);
KK4++;
break;
}
}
if (++IOT_TCP_port_count >= 8)
{
IOT_TCP_port_count = 0x00;
IOT_4G_isr = 10;
KK5++;
break;
}
IOT_4G_isr = 0;
KK6++;
vTaskDelay(100);
break;
case 20:
xStatus = xQueueReceive( xQueue_EC20, &Buffer, xTicksToWait);//portMAX_DELAY);
if( xStatus == pdPASS )
{
switch (Buffer[0])
{
case 1:
IOT_DI1_1 = Buffer[1];
IOT_DI1_2 = Buffer[2];
break;
case 2:
IOT_DI2_1 = Buffer[1];
IOT_DI2_2 = Buffer[2];
break;
case 3:
IOT_Clock.year = Buffer[1];
IOT_Clock.month = Buffer[2];
IOT_Clock.day = Buffer[3];
IOT_Clock.hour = Buffer[4];
IOT_Clock.minute = Buffer[5];
IOT_Clock.second = Buffer[6];
IOT_Clock.week = Buffer[7];
break;
}//switch (Buffer[0])
}//if( xStatus == pdPASS )
MB_USART2.send_buffer[0] = IOT_DI1_1;
MB_USART2.send_buffer[1] = IOT_DI1_2;
MB_USART2.send_buffer[2] = IOT_DI2_1;
MB_USART2.send_buffer[3] = IOT_DI2_2;
MB_USART2.send_buffer[4] = 0x20;
MB_USART2.send_buffer[5] = IOT_Clock.year;
MB_USART2.send_buffer[6] = IOT_Clock.month;
MB_USART2.send_buffer[7] = IOT_Clock.day;
MB_USART2.send_buffer[8] = IOT_Clock.hour;
MB_USART2.send_buffer[9] = IOT_Clock.minute;
MB_USART2.send_buffer[10] = IOT_Clock.second;
MB_USART2.send_buffer[11] = 0x0D;
MB_USART2.send_buffer[12] = 0x0A;
MB_USART2.sendCount = 13;
USART2_Begin_Send();
//vTaskDelay(50);
xResult = xSemaphoreTake(xSemaphore , xTicksToWait);//portMAX_DELAY);
if (xResult == pdTRUE)
{
strcpy(IOT_target_string , "\r\nNO CARRIER\r\n");
hex_to_string(MB_USART2.mscomm_buffer , IOT_source_string , MB_USART2.receCount);
if (strstr(IOT_source_string , IOT_target_string) != NULL)
{
IOT_4G_Outtime = 0x00;
IOT_4G_isr = 10;
vTaskDelay(100);
break;
}
}
vTaskDelay(1000);
break;
default:
vTaskDelay(100);
break;
}//switch (IOT_4G_isr)
}
本文地址:https://blog.csdn.net/ba_wang_mao/article/details/109005580
上一篇: RTX 2060 Super拆解图赏:这用料没SEI了
下一篇: 红米10x和红米k30i的区别