Multithreading 结构化xqueue freeRTOS示例

Multithreading 结构化xqueue freeRTOS示例,multithreading,queue,esp32,freertos,Multithreading,Queue,Esp32,Freertos,我来找你是因为我对我的问题感到绝望。我已经发表了一篇前一篇文章(),但是我已经尝试过了,但是没有得到预期的结果 结果: len:-1184515756 数据: #include <stdio.h> #include <freertos/FreeRTOS.h> #include <freertos/task.h> #include <freertos/queue.h> #include <esp_system.h> #include &l

我来找你是因为我对我的问题感到绝望。我已经发表了一篇前一篇文章(),但是我已经尝试过了,但是没有得到预期的结果

结果:

len:-1184515756

数据:

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <driver/uart.h>

#define COMMAND_DEVICE_LIST 0x15
#define COMMAND_STATUS 0x00
#define COMMAND_DESCRIPTION 0x43
#define COMMAND_UNRECOGNIZED 0x11
#define COMMAND_ADDGROUP 0x60
#define COMMAND_BIND 0x30

#define SIZE_TYPE_DEVICE 2
#define SIZE_CLUSTER 2
#define SIZE_SHORT_ADD 2
#define SIZE_IEEE_ADD 8
#define START_DATA 7
#define SIZE_MIN_NB_DEVICE 2

#define CHAR_END_FRAME 0x03
#define CHAR_CONFIG_FRAME 0x02
#define CHAR_VAL_MAX_CONFIG_FRAME 0x10

#define STACK_SIZE 2048

//storage info
struct device{
    char type_id[SIZE_TYPE_DEVICE];
    char manufacturer[32]; //attribute 4
    char name[32]; // attribute 5
    char short_add[SIZE_SHORT_ADD];
    char ieee_add[SIZE_IEEE_ADD];
    char cluster_input[32][SIZE_CLUSTER];
    char cluster_output[32][SIZE_CLUSTER];
};
struct get_device_list{
    int len;
    struct device device[];
}dev_list;

//list device type
char list_device_type[2][SIZE_TYPE_DEVICE]={{0x01,0x0C},{0x08,0x20}};

// Setup UART buffered IO with event queue
const int uart_buffer_size = 2048;
// Queue used to send and receive complete struct message structures. 
QueueHandle_t uart_queue = NULL;
const int uart_num = UART_NUM_2;

struct message{
    char cmd[128];
    int len;
};

SemaphoreHandle_t xMutexUart;

/************************************************CONFIG***********************************************/

/**
 * @brief Configuraiton of UART and set pin that uart use.
 * 
 * @return [int] 0 if is successed
 */
int uart_setup()
{
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    // Configure UART parameters
    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, 18, 19, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
    // Install UART driver using an event queue here
    ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, \
                                        uart_buffer_size, 20, &uart_queue, 0));
    return 0;
}


/************************************************UTILS***********************************************/
int cmdlen(char* cmd){
    int i = 0;
    while(cmd[i]!='\n'){
        i++;
    }
    return i+1;
}


int device_shortadd_compare(char* search_add){
    int res=-1,val=0;
    for(int i=0;i < dev_list.len;i++){
        val = 0;
        for(int l=0;l<SIZE_TYPE_DEVICE;l++){
            if(search_add[l]==dev_list.device[i].short_add[l]){
                val++;
            }
        }
        if(val == 2){
            res=i;
        }
    }
    return res;
}


int parse_translate(char* data,int len,char res[][64]){
    int i=0,j=0,end=0,index=0;
    while(len - j != 0){
        if (data[j]==CHAR_CONFIG_FRAME)
        {
            j++; //skip 02
            data[j]^=0x10; //10xor10=00
            res[end][i-index]= data[j];
            printf("%02x ",res[end][i-index]);
            i++;
                    
        }else if (data[j]==CHAR_END_FRAME)
        {
            res[end][i-index]=data[j];
            res[end][i+1-index]='\n';
            res[end][i+2-index]='\0';
            printf("%02x\n",res[end][i-index]);
            i=i+2;
            index=i;
            end++;
            
        }else{
            res[end][i-index]= data[j];
            printf("%02x ",res[end][i-index]);
            i++;
        }
        j++;
        
    }
    return end;
}


/************************************************COMMUNICATION***********************************************/
/**
 * @brief [Thread]Send commande to Zigbee module by UART
 * 
 */
void vTaskSend( void * pvParameters )
{
    while(1){
        int err;
        struct message rx_msg;
        if (xQueueReceive(uart_queue, &rx_msg, portMAX_DELAY) == pdPASS){
            printf("len : %d\n", rx_msg.len);
            for(int i=0;i< rx_msg.len;i++){
                printf("%02x ", rx_msg.cmd[i]);
            }
            printf("\n");
            xSemaphoreTake(xMutexUart, portMAX_DELAY);
            err = uart_write_bytes(uart_num, (const char *) rx_msg.cmd,  rx_msg.len); // Write data to UART.
            ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 5000)); // wait timeout is 5000 RTOS ticks (TickType_t)
            xSemaphoreGive(xMutexUart);
            if(err !=  rx_msg.len){
                printf("Err, not all bytes send : %d/%d\n",err, rx_msg.len);
            }
            //vPortFree(rx_msg);  // free the structure memory*/
            vTaskDelay(100 / portTICK_RATE_MS);
            //taskYIELD();
        }
        xSemaphoreGive(xMutexUart);
        vTaskDelay(100 / portTICK_RATE_MS);
    }
}

/**
 * @brief [Thread]Read response from UART
 * 
 */
void vTaskRead( void * pvParameters )
{
    char data[512];
    int length = 0;
    while(1){   
        xSemaphoreTake(xMutexUart, portMAX_DELAY);
        ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
        length = uart_read_bytes(uart_num, (uint8_t*)data, length, 100);
        xSemaphoreGive(xMutexUart);
        if(length>0){
            printf("[R] message size :  %d\n ",length);
            char res[8][64];
            int nb_cmd=parse_translate(data,length,res);
            for(int i=0;i<nb_cmd;i++){
                switch(res[i][2]){
                    case COMMAND_STATUS:
                        if(res[6]==COMMAND_STATUS){
                            printf("Command status success\n");
                        }
                        break;
                    case COMMAND_DEVICE_LIST:
                        dev_list.len=(cmdlen(res[i])-8)/13;
                        printf("Nb device find : %d\n",dev_list.len);
                        int start = START_DATA; //start of device info in frame
                        for(int j=0;j<dev_list.len;j++){
                            struct device dev;
                            printf("short: ");
                            for(int j=0;j<SIZE_SHORT_ADD;j++){
                                dev.short_add[j] = res[i][start+j];
                                printf("%02x ",dev.short_add[j]);
                            }
                            printf("\n");
                            printf("ieee: ");
                            for(int l=0;l<SIZE_IEEE_ADD;l++){
                                dev.ieee_add[l]=res[i][start+2+l];
                                printf("%02x ",dev.ieee_add[l]);
                            }
                            printf("\n");
                            dev_list.device[j]=dev;
                            vTaskDelay(100 / portTICK_RATE_MS);
                            start=start+SIZE_SHORT_ADD+SIZE_IEEE_ADD+2+1; //info of device 1 not used + id device 2 not used
                        }
                        break;
                    case COMMAND_DESCRIPTION:
                        printf(" ");
                        char add[SIZE_TYPE_DEVICE];
                        for(int j=0;j<SIZE_TYPE_DEVICE;j++){
                                add[j]=res[i][8+j];
                        }
                        int id = device_shortadd_compare(add);
                        if(id != -1){
                            printf("type: ");
                            for(int j=0;j<SIZE_TYPE_DEVICE;j++){
                                dev_list.device[id].type_id[j]=res[i][14+j];
                                printf("%02x ",dev_list.device[id].type_id[j]);
                            }
                            printf("\n");
                            if(dev_list.device[id].type_id[0]==0x00 && dev_list.device[id].type_id[1] ==0x00){
                                printf("Device %d not found, if it's a button press it\n",i);
                            }
                        }
                        break;                        
                    case COMMAND_ADDGROUP:
                        printf("Device add to group!\n");
                        break;
                    case COMMAND_BIND:
                        printf("Press the button, hurry up!!\n");
                        break;
                }
            }
                
        }
        vTaskDelay(100 / portTICK_RATE_MS);
        //taskYIELD();
    }
    vTaskDelay(1000 / portTICK_RATE_MS);
    ESP_ERROR_CHECK(uart_flush(uart_num));
}


/************************************************ZIGBEE***********************************************/

/**
 * @brief Configuration of Zigbee module (channel,type) and start network
 * 
 */
void zigbee_config_with_pemit_join(){
    
    struct message *ptx_msg =pvPortMalloc(sizeof (struct message));
    //Set Channel
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x21, 0x02, 0x10, 0x02, 0x14, 0x2D, 0x02, 0x10, 0x02, 0x10, 0x02, 0x18, 0x02, 0x10, 0x03);
    ptx_msg->len = strlen(ptx_msg->cmd);
    printf("len : %d\n", ptx_msg->len);
            for(int i=0;i<ptx_msg->len;i++){
                printf("%02x ",ptx_msg->cmd[i]);
            }
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Set channel to 11\n");
    vTaskDelay(100 / portTICK_RATE_MS);
    //Set Type
    //ptx_msg = pvPortMalloc(sizeof (struct message));
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x23, 0x02, 0x10, 0x02, 0x11, 0x22, 0x02, 0x10, 0x03);
    ptx_msg->len =strlen(ptx_msg->cmd);
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Set as Coordinator\n");
    vTaskDelay(100 / portTICK_RATE_MS);
    //Start Network
    //ptx_msg =pvPortMalloc(sizeof (struct message));
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x24, 0x02, 0x10, 0x02, 0x10, 0x24, 0x03);
    ptx_msg->len =strlen(ptx_msg->cmd);
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Start Network\n");
    //Permit join 
    //ptx_msg =pvPortMalloc(sizeof (struct message));
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x49, 0x02, 0x10, 0x02, 0x14, 0xB0, 0xFF, 0xFC, 0xFF, 0x02, 0x10, 0x03);
    ptx_msg->len =strlen(ptx_msg->cmd);
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Permit join: If you have button press it 2sec.\n");
    vTaskDelay(6000 / portTICK_RATE_MS);
}

void vTaskZigbee( void * pvParameters )
{
    if (uart_setup() == -1){
        printf("Err during uart setup\n");
    }
    zigbee_config_with_pemit_join();
    //taskYIELD();
    while(1){
        vTaskDelay(100000 / portTICK_RATE_MS);
    }
    
}

/************************************************MAIN***********************************************/

void app_main() {
    
    /* Create the queue used to send complete struct message structures. */
    uart_queue = xQueueCreate(20, sizeof(struct message ));
    xMutexUart = xSemaphoreCreateMutex();

    BaseType_t xReturned;
    TaskHandle_t xHandle = NULL;

    /* Create the task, storing the handle. */
    xReturned = xTaskCreate(
                    vTaskZigbee,       /* Function that implements the task. */
                    "Zigbee",          /* Text name for the task. */
                    STACK_SIZE,      /* Stack size in words, not bytes. */
                    NULL,    /* Parameter passed into the task. */
                    tskIDLE_PRIORITY,/* Priority at which the task is created. */
                    &xHandle);      /* Used to pass out the created task's handle. */
    xReturned = xTaskCreate( vTaskSend, "Send", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle);
    xReturned = xTaskCreate( vTaskRead, "Read", STACK_SIZE*2, NULL, tskIDLE_PRIORITY, &xHandle); 
}
我也试过这两个教程,但仍然没有效果。。。

在几次尝试之后,我没有真正理解我是如何让某些东西几乎正常工作的,但我最终得到了一个专家错误负载

代码:

void app_main() {

/* Create the queue used to send complete struct message structures. */
    uart_queue = xQueueCreate(20, sizeof(struct message));
    xMutexUart = xSemaphoreCreateMutex();
   
    ....

struct message *ptx_msg =pvPortMalloc(sizeof (struct message));
    //Set Channel
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x21, 0x02, 0x10, 0x02, 0x14, 0x2D, 0x02, 0x10, 0x02, 0x10, 0x02, 0x18, 0x02, 0x10, 0x03);
    ptx_msg->len = strlen(ptx_msg->cmd);
    printf("len : %d\n", ptx_msg->len);
            for(int i=0;i<ptx_msg->len;i++){
                printf("%02x ",ptx_msg->cmd[i]);
            }
    xSemaphoreTake(xMutexUart, portMAX_DELAY);
    xQueueSend(uart_queue, (void*)&ptx_msg, portMAX_DELAY);
    xSemaphoreGive(xMutexUart);

    //repeat X time

    ....
}
void app_main(){
/*创建用于发送完整结构消息结构的队列*/
uart_queue=xQueueCreate(20,sizeof(结构消息));
xMutexUart=xSemaphoreCreateMutex();
....
结构消息*ptx_msg=pvPortMalloc(sizeof(结构消息));
//设置频道
sprintf(ptx_msg->cmd,“%c%c%c%c%c%c%c%c%c%c%c%c%c\n”,0x01,0x02,0x10,0x21,0x02,0x10,0x02,0x14,0x2D,0x02,0x10,0x02,0x02,0x10,0x02,0x18,0x02,0x10,0x03);
ptx_msg->len=strlen(ptx_msg->cmd);
printf(“len:%d\n”,ptx\u msg->len);
for(int i=0;ilen;i++){
printf(“%02x”,ptx_msg->cmd[i]);
}
xSemaphoreTake(xMutexUart,端口最大延迟);
xQueueSend(uart_队列,(void*)和ptx_消息,端口最大延迟);
xSemaphoreGive(xMutexUart);
//重复X次
....
}
void vTaskSend(void*pvParameters)
{
结构消息*rx_消息;
INTERR;
而(1){
xSemaphoreTake(xMutexUart,端口最大延迟);
if(xQueueReceive(uart\U队列和rx\U消息,端口最大延迟)=pdPASS){
printf(“len:%d\n”,rx\u msg->len);
for(int i=0;ilen;i++){
printf(“%02x”,rx_msg->cmd[i]);
}
printf(“\n”);
err=uart_write_字节(uart_num,(const char*)rx_msg->cmd,rx_msg->len);//将数据写入uart。
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num,5000));//等待超时为5000个RTOS时钟(TickType_t)
如果(错误!=rx\u msg->len){
printf(“Err,并非所有字节发送:%d/%d\n”,Err,rx\u msg->len);
}
vPortFree(rx_msg);//释放结构内存*/
vTaskDelay(100/门滴答率);
//taskYIELD();
}
xSemaphoreGive(xMutexUart);
vTaskDelay(100/门滴答率);
}
}
结果:

len:19

资料:01 02 10 21 02 10 02 14 2d 02 10 02 10 02 18 02 10 03 0a

我想纠正我的问题,请

编辑:

这是我的代码,结果是:

len:54516257561

数据:

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <driver/uart.h>

#define COMMAND_DEVICE_LIST 0x15
#define COMMAND_STATUS 0x00
#define COMMAND_DESCRIPTION 0x43
#define COMMAND_UNRECOGNIZED 0x11
#define COMMAND_ADDGROUP 0x60
#define COMMAND_BIND 0x30

#define SIZE_TYPE_DEVICE 2
#define SIZE_CLUSTER 2
#define SIZE_SHORT_ADD 2
#define SIZE_IEEE_ADD 8
#define START_DATA 7
#define SIZE_MIN_NB_DEVICE 2

#define CHAR_END_FRAME 0x03
#define CHAR_CONFIG_FRAME 0x02
#define CHAR_VAL_MAX_CONFIG_FRAME 0x10

#define STACK_SIZE 2048

//storage info
struct device{
    char type_id[SIZE_TYPE_DEVICE];
    char manufacturer[32]; //attribute 4
    char name[32]; // attribute 5
    char short_add[SIZE_SHORT_ADD];
    char ieee_add[SIZE_IEEE_ADD];
    char cluster_input[32][SIZE_CLUSTER];
    char cluster_output[32][SIZE_CLUSTER];
};
struct get_device_list{
    int len;
    struct device device[];
}dev_list;

//list device type
char list_device_type[2][SIZE_TYPE_DEVICE]={{0x01,0x0C},{0x08,0x20}};

// Setup UART buffered IO with event queue
const int uart_buffer_size = 2048;
// Queue used to send and receive complete struct message structures. 
QueueHandle_t uart_queue = NULL;
const int uart_num = UART_NUM_2;

struct message{
    char cmd[128];
    int len;
};

SemaphoreHandle_t xMutexUart;

/************************************************CONFIG***********************************************/

/**
 * @brief Configuraiton of UART and set pin that uart use.
 * 
 * @return [int] 0 if is successed
 */
int uart_setup()
{
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    // Configure UART parameters
    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, 18, 19, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
    // Install UART driver using an event queue here
    ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, \
                                        uart_buffer_size, 20, &uart_queue, 0));
    return 0;
}


/************************************************UTILS***********************************************/
int cmdlen(char* cmd){
    int i = 0;
    while(cmd[i]!='\n'){
        i++;
    }
    return i+1;
}


int device_shortadd_compare(char* search_add){
    int res=-1,val=0;
    for(int i=0;i < dev_list.len;i++){
        val = 0;
        for(int l=0;l<SIZE_TYPE_DEVICE;l++){
            if(search_add[l]==dev_list.device[i].short_add[l]){
                val++;
            }
        }
        if(val == 2){
            res=i;
        }
    }
    return res;
}


int parse_translate(char* data,int len,char res[][64]){
    int i=0,j=0,end=0,index=0;
    while(len - j != 0){
        if (data[j]==CHAR_CONFIG_FRAME)
        {
            j++; //skip 02
            data[j]^=0x10; //10xor10=00
            res[end][i-index]= data[j];
            printf("%02x ",res[end][i-index]);
            i++;
                    
        }else if (data[j]==CHAR_END_FRAME)
        {
            res[end][i-index]=data[j];
            res[end][i+1-index]='\n';
            res[end][i+2-index]='\0';
            printf("%02x\n",res[end][i-index]);
            i=i+2;
            index=i;
            end++;
            
        }else{
            res[end][i-index]= data[j];
            printf("%02x ",res[end][i-index]);
            i++;
        }
        j++;
        
    }
    return end;
}


/************************************************COMMUNICATION***********************************************/
/**
 * @brief [Thread]Send commande to Zigbee module by UART
 * 
 */
void vTaskSend( void * pvParameters )
{
    while(1){
        int err;
        struct message rx_msg;
        if (xQueueReceive(uart_queue, &rx_msg, portMAX_DELAY) == pdPASS){
            printf("len : %d\n", rx_msg.len);
            for(int i=0;i< rx_msg.len;i++){
                printf("%02x ", rx_msg.cmd[i]);
            }
            printf("\n");
            xSemaphoreTake(xMutexUart, portMAX_DELAY);
            err = uart_write_bytes(uart_num, (const char *) rx_msg.cmd,  rx_msg.len); // Write data to UART.
            ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 5000)); // wait timeout is 5000 RTOS ticks (TickType_t)
            xSemaphoreGive(xMutexUart);
            if(err !=  rx_msg.len){
                printf("Err, not all bytes send : %d/%d\n",err, rx_msg.len);
            }
            //vPortFree(rx_msg);  // free the structure memory*/
            vTaskDelay(100 / portTICK_RATE_MS);
            //taskYIELD();
        }
        xSemaphoreGive(xMutexUart);
        vTaskDelay(100 / portTICK_RATE_MS);
    }
}

/**
 * @brief [Thread]Read response from UART
 * 
 */
void vTaskRead( void * pvParameters )
{
    char data[512];
    int length = 0;
    while(1){   
        xSemaphoreTake(xMutexUart, portMAX_DELAY);
        ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
        length = uart_read_bytes(uart_num, (uint8_t*)data, length, 100);
        xSemaphoreGive(xMutexUart);
        if(length>0){
            printf("[R] message size :  %d\n ",length);
            char res[8][64];
            int nb_cmd=parse_translate(data,length,res);
            for(int i=0;i<nb_cmd;i++){
                switch(res[i][2]){
                    case COMMAND_STATUS:
                        if(res[6]==COMMAND_STATUS){
                            printf("Command status success\n");
                        }
                        break;
                    case COMMAND_DEVICE_LIST:
                        dev_list.len=(cmdlen(res[i])-8)/13;
                        printf("Nb device find : %d\n",dev_list.len);
                        int start = START_DATA; //start of device info in frame
                        for(int j=0;j<dev_list.len;j++){
                            struct device dev;
                            printf("short: ");
                            for(int j=0;j<SIZE_SHORT_ADD;j++){
                                dev.short_add[j] = res[i][start+j];
                                printf("%02x ",dev.short_add[j]);
                            }
                            printf("\n");
                            printf("ieee: ");
                            for(int l=0;l<SIZE_IEEE_ADD;l++){
                                dev.ieee_add[l]=res[i][start+2+l];
                                printf("%02x ",dev.ieee_add[l]);
                            }
                            printf("\n");
                            dev_list.device[j]=dev;
                            vTaskDelay(100 / portTICK_RATE_MS);
                            start=start+SIZE_SHORT_ADD+SIZE_IEEE_ADD+2+1; //info of device 1 not used + id device 2 not used
                        }
                        break;
                    case COMMAND_DESCRIPTION:
                        printf(" ");
                        char add[SIZE_TYPE_DEVICE];
                        for(int j=0;j<SIZE_TYPE_DEVICE;j++){
                                add[j]=res[i][8+j];
                        }
                        int id = device_shortadd_compare(add);
                        if(id != -1){
                            printf("type: ");
                            for(int j=0;j<SIZE_TYPE_DEVICE;j++){
                                dev_list.device[id].type_id[j]=res[i][14+j];
                                printf("%02x ",dev_list.device[id].type_id[j]);
                            }
                            printf("\n");
                            if(dev_list.device[id].type_id[0]==0x00 && dev_list.device[id].type_id[1] ==0x00){
                                printf("Device %d not found, if it's a button press it\n",i);
                            }
                        }
                        break;                        
                    case COMMAND_ADDGROUP:
                        printf("Device add to group!\n");
                        break;
                    case COMMAND_BIND:
                        printf("Press the button, hurry up!!\n");
                        break;
                }
            }
                
        }
        vTaskDelay(100 / portTICK_RATE_MS);
        //taskYIELD();
    }
    vTaskDelay(1000 / portTICK_RATE_MS);
    ESP_ERROR_CHECK(uart_flush(uart_num));
}


/************************************************ZIGBEE***********************************************/

/**
 * @brief Configuration of Zigbee module (channel,type) and start network
 * 
 */
void zigbee_config_with_pemit_join(){
    
    struct message *ptx_msg =pvPortMalloc(sizeof (struct message));
    //Set Channel
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x21, 0x02, 0x10, 0x02, 0x14, 0x2D, 0x02, 0x10, 0x02, 0x10, 0x02, 0x18, 0x02, 0x10, 0x03);
    ptx_msg->len = strlen(ptx_msg->cmd);
    printf("len : %d\n", ptx_msg->len);
            for(int i=0;i<ptx_msg->len;i++){
                printf("%02x ",ptx_msg->cmd[i]);
            }
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Set channel to 11\n");
    vTaskDelay(100 / portTICK_RATE_MS);
    //Set Type
    //ptx_msg = pvPortMalloc(sizeof (struct message));
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x23, 0x02, 0x10, 0x02, 0x11, 0x22, 0x02, 0x10, 0x03);
    ptx_msg->len =strlen(ptx_msg->cmd);
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Set as Coordinator\n");
    vTaskDelay(100 / portTICK_RATE_MS);
    //Start Network
    //ptx_msg =pvPortMalloc(sizeof (struct message));
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x24, 0x02, 0x10, 0x02, 0x10, 0x24, 0x03);
    ptx_msg->len =strlen(ptx_msg->cmd);
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Start Network\n");
    //Permit join 
    //ptx_msg =pvPortMalloc(sizeof (struct message));
    sprintf(ptx_msg->cmd,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",0x01, 0x02, 0x10, 0x49, 0x02, 0x10, 0x02, 0x14, 0xB0, 0xFF, 0xFC, 0xFF, 0x02, 0x10, 0x03);
    ptx_msg->len =strlen(ptx_msg->cmd);
    xQueueSend(uart_queue, (void*)ptx_msg, portMAX_DELAY);
    printf("[S] Permit join: If you have button press it 2sec.\n");
    vTaskDelay(6000 / portTICK_RATE_MS);
}

void vTaskZigbee( void * pvParameters )
{
    if (uart_setup() == -1){
        printf("Err during uart setup\n");
    }
    zigbee_config_with_pemit_join();
    //taskYIELD();
    while(1){
        vTaskDelay(100000 / portTICK_RATE_MS);
    }
    
}

/************************************************MAIN***********************************************/

void app_main() {
    
    /* Create the queue used to send complete struct message structures. */
    uart_queue = xQueueCreate(20, sizeof(struct message ));
    xMutexUart = xSemaphoreCreateMutex();

    BaseType_t xReturned;
    TaskHandle_t xHandle = NULL;

    /* Create the task, storing the handle. */
    xReturned = xTaskCreate(
                    vTaskZigbee,       /* Function that implements the task. */
                    "Zigbee",          /* Text name for the task. */
                    STACK_SIZE,      /* Stack size in words, not bytes. */
                    NULL,    /* Parameter passed into the task. */
                    tskIDLE_PRIORITY,/* Priority at which the task is created. */
                    &xHandle);      /* Used to pass out the created task's handle. */
    xReturned = xTaskCreate( vTaskSend, "Send", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle);
    xReturned = xTaskCreate( vTaskRead, "Read", STACK_SIZE*2, NULL, tskIDLE_PRIORITY, &xHandle); 
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义命令\u设备\u列表0x15
#定义命令_状态0x00
#定义命令描述0x43
#定义命令\u无法识别的0x11
#定义命令\u ADDGROUP 0x60
#定义命令绑定0x30
#定义大小\u类型\u设备2
#定义集群2的大小
#定义大小\u短\u添加2
#定义大小\u IEEE\u添加8
#定义START_数据7
#定义大小\u最小\u NB\u设备2
#定义字符结束帧0x03
#定义字符配置帧0x02
#定义字符值最大配置帧0x10
#定义堆栈大小2048
//存储信息
结构设备{
字符类型标识[大小类型设备];
字符制造商[32];//属性4
字符名[32];//属性5
字符短加[大小短加];
字符ieee_添加[大小ieee_添加];
char cluster_input[32][SIZE_cluster];
char cluster_输出[32][SIZE_cluster];
};
结构获取设备列表{
内伦;
结构设备设备[];
}开发清单;
//列出设备类型
字符列表设备类型[2][SIZE设备类型]={{0x01,0x0C},{0x08,0x20};
//使用事件队列设置UART缓冲IO
常量整数缓冲区大小=2048;
//用于发送和接收完整结构消息结构的队列。
QueueHandle\u t uart\u queue=NULL;
常量int uart_num=uart_num_2;
结构消息{
char-cmd[128];
内伦;
};
信号量处理;
/************************************************配置***********************************************/
/**
*@UART的简要配置和UART使用的设置引脚。
* 
*@return[int]0如果成功
*/
int uart_设置()
{
uart\U配置\U uart\U配置={
.波特率=115200,
.data\u位=UART\u数据\u 8位,
.parity=UART\u奇偶校验\u禁用,
.stop\u bits=UART\u stop\u bits\u 1,
.flow\u ctrl=UART\u HW\u flow ctrl\u禁用
};
//配置UART参数
ESP_错误检查(uart_参数配置(uart_数量和uart_配置));
ESP_错误检查(uart_设置_引脚(uart_数量_2,18,19,uart_引脚未更改,uart_引脚未更改));
//在此处使用事件队列安装UART驱动程序
ESP错误检查(uart驱动程序安装)(uart数量2,uart缓冲区大小\
uart_缓冲区大小为20,uart_队列大小为0);
返回0;
}
/************************************************乌提尔斯***********************************************/
int cmdlen(char*cmd){
int i=0;
while(cmd[i]!='\n'){
i++;
}
返回i+1;
}
int设备\u短添加\u比较(字符*搜索\u添加){
int res=-1,val=0;
for(int i=0;ilen=strlen(ptx_msg->cmd);
xQueueSend(uart_队列,(void*)ptx_消息,端口最大延迟);
printf(“[S]设置为协调器\n”);
vTaskDelay(100/门滴答率);
//启动网络
//ptx_msg=pvPortMalloc(sizeof(struct message));
sprintf(ptx_msg->cmd,“%c%c%c%c%c%c%c%c\n”,0x01、0x02、0x10、0x24、0x02、0x10、0x02、0x10、0x10、0x24、0x03);
ptx_msg->len=strlen(ptx_msg->cmd);
xQueueSend(uart_队列,(void*)ptx_消息,端口最大延迟);
printf(“[S]启动网络\n”);
//允许加入
//ptx_msg=pvPortMalloc(sizeof(struct message));
sprintf(ptx_msg->cmd,“%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n”,