大学生
最后登录1970-1-1
在线时间 小时
注册时间2015-5-8
|
楼主 |
发表于 2020-6-12 16:39:05
|
显示全部楼层
经过一段时间的学习,这个问题我已经编程解决了,代码如下
- 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)
- }
复制代码 |
|