| | |
| | | |
| | | |
| | | |
| | | //#define EUART_RX_BUF_SIZE 100 |
| | | //#define EUART0_RX_BUF_SIZE 100 |
| | | #define Label_id_local 1 //标签id |
| | | #define data_buff_MAX 50 //基站数量 |
| | | #define Lora_TXD_bff_MAX 220 //发送缓存区大小 >4+8+4*基站数量+2=14+4*基站数量 |
| | |
| | | |
| | | void SendComMap(uint8_t data_length, uint8_t index)//表示成功读取的回应包 |
| | | { |
| | | static uint8_t send_frame[EUART_RX_BUF_SIZE]; |
| | | static uint8_t send_frame[EUART0_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | |
| | | |
| | | } |
| | | double d_value; |
| | | void UsartParseDataHandler(uint8_t data) |
| | | uint16_t test_calculate,pack_checksum_test; |
| | | extern uint32_t tag_id_authorization_list[1024]; |
| | | extern uint16_t bind_distance,button_determinate_distance,change_by_frequency_distance; |
| | | extern uint8_t tag_near_frequency; |
| | | extern tag_id_authorization_list_num; |
| | | #define ID_SIZE 4 |
| | | uint32_t temp_key; |
| | | uint8_t required_size_temp; |
| | | uint8_t delete_count_temp; |
| | | int low_temp,high_temp,mid_temp; |
| | | void delete_authorization_list(uint8_t* delete_list,uint32_t*tag_authorization_idlist,uint32_t*authorization_list_num) |
| | | { |
| | | if (*authorization_list_num == 0) { |
| | | // for (int i = 0; i < taglist_num; i++) { |
| | | // tag_authorized_List[i] = 0; |
| | | // } |
| | | return; |
| | | } |
| | | uint8_t delete_count = delete_list[0]; |
| | | delete_count_temp=delete_count; |
| | | // 遍历需要查找的每个ID |
| | | for (int i = 0; i < delete_count; i++) { |
| | | uint32_t key=0; |
| | | uint8_t required_size = 1 + i * ID_SIZE; |
| | | required_size_temp= required_size; |
| | | memcpy(&key,&delete_list[required_size],4); |
| | | int low = 0; |
| | | int high = *authorization_list_num - 1; |
| | | int found = 0; // 标记是否找到 |
| | | int index=0; |
| | | // 在授权列表中执行二分查找 |
| | | low_temp=low;//测试 |
| | | high_temp=high;//测试 |
| | | temp_key=key; |
| | | while (low <= high) { |
| | | int mid = low + (high - low) / 2; // 防止整数溢出 |
| | | |
| | | if (tag_id_authorization_list[mid] == key) { |
| | | found = 1; // 找到匹配项 |
| | | index=mid; |
| | | break; |
| | | } else if (tag_id_authorization_list[mid] < key) { |
| | | low = mid + 1; // 在右半部分继续查找 |
| | | } else { |
| | | high = mid - 1; // 在左半部分继续查找 |
| | | } |
| | | mid_temp=mid;//测试 |
| | | } |
| | | |
| | | if(found) |
| | | { |
| | | int elements_to_move = *authorization_list_num - index - 1; |
| | | if (elements_to_move > 0) { |
| | | // 计算源地址和目标地址 |
| | | void *src = (uint32_t *)tag_authorization_idlist + (index + 1); |
| | | void *dest =(uint32_t *)tag_authorization_idlist + index; |
| | | |
| | | // 使用memmove进行内存块移动(处理重叠内存区域) |
| | | memmove(dest, src, elements_to_move * 4); |
| | | *authorization_list_num=*authorization_list_num-1;//成功删除一个更新数量 |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | void Usart0ParseDataHandler(uint8_t data) |
| | | { |
| | | // 全局变量 |
| | | static Usart0ReceiveState usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | |
| | | static uint8_t index = 0; // 缓冲区索引 |
| | | static uint8_t pack_cmd_type = 0; // 指令类型 |
| | | static uint8_t pack_msg_type = 0; // 消息类型 |
| | | static uint8_t pack_data_len = 0; // 数据长度 |
| | | static uint8_t pack_param_addr = 0; // 参数地址 |
| | | static uint16_t pack_checksum = 0; // 接收到的校验和 |
| | | static uint16_t calculated_checksum = 0; // 计算得到的校验和 |
| | | static uint8_t send_frame[256]; // 用于计算校验和的帧 |
| | | static uint8_t frame_index = 0; // 帧缓冲区索引 |
| | | static uint8_t pack_datalen = 0; |
| | | |
| | | LOG_INFO(TRACE_MODULE_APP,"收到 %#x\r\n",data); |
| | | switch(usart0_receive_state) { |
| | | case Usart0ReceiveWaitHead0: |
| | | if(data == 0x55) { |
| | | usart0_receive_state = Usart0ReceiveWaitHead1; |
| | | // 初始化帧缓冲区 |
| | | frame_index = 0; |
| | | send_frame[frame_index++] = data; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitHead1: |
| | | if(data == 0xAA) { |
| | | usart0_receive_state = Usart0ReceiveWaitCmdType; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitCmdType: |
| | | if(data == 0x23) { |
| | | pack_cmd_type = data; |
| | | usart0_receive_state = Usart0ReceiveWaitMsgType; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitMsgType: |
| | | if(data == 0x2||data==0x1||data == 0x3||data==0x4||data==0x5) { // 读写回复消息类型 |
| | | pack_msg_type = data; |
| | | usart0_receive_state = Usart0ReceiveWaitDataLen; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitDataLen: |
| | | pack_data_len = data; |
| | | index = 0; |
| | | usart0_receive_state = Usart0ReceiveWaitData; |
| | | send_frame[frame_index++] = data; |
| | | if( pack_data_len-3==0)//若等于0提前调到校验 |
| | | usart0_receive_state=Usart0ReceiveWaitChecksum1; |
| | | break; |
| | | |
| | | // case Usart0ReceiveWaitParamAddr: |
| | | // pack_param_addr = data; |
| | | // if(pack_msg_type==CMD_READ) |
| | | // { |
| | | // usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | // pack_datalen=pack_data_len-6; |
| | | // }else{ |
| | | // usart0_receive_state = Usart0ReceiveWaitData; |
| | | // } |
| | | // send_frame[frame_index++] = data; |
| | | // break; |
| | | |
| | | case Usart0ReceiveWaitData: |
| | | |
| | | send_frame[frame_index++] = data; |
| | | mUsart2ReceivePack[index++] = data; |
| | | |
| | | if(index == pack_data_len-3) { |
| | | usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | //pack_datalen=pack_data_len; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitChecksum1: |
| | | pack_checksum = data; // 低字节 |
| | | pack_checksum_test = data; // 低字节 |
| | | send_frame[frame_index++] = data; |
| | | usart0_receive_state = Usart0ReceiveWaitChecksum2; |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验1 %#x\r\n",data); |
| | | break; |
| | | case Usart0ReceiveWaitChecksum2: |
| | | pack_checksum |= data<<8; // 高字节 |
| | | pack_checksum_test |= data<<8; // 高字节 |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验2 %#x\r\n",data); |
| | | // 计算校验和 (从第3个字节开始,长度为4+数据长度) |
| | | // if(pack_msg_type!=CMD_REPLY) |
| | | // { |
| | | calculated_checksum = Checksum_u16(&send_frame[2],pack_data_len); |
| | | // }else{ |
| | | // //calculated_checksum = Checksum_u16(&send_frame[2],4); |
| | | // |
| | | // } |
| | | test_calculate=calculated_checksum; |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验 %#x,%#x\r\n",calculated_checksum,pack_checksum); |
| | | // 验证校验和 |
| | | if(pack_checksum == calculated_checksum) { |
| | | // 校验通过,处理数据 |
| | | switch(pack_msg_type) |
| | | { |
| | | case BIND_DISTANCE_FOR_TAG: |
| | | memcpy(&bind_distance,&mUsart2ReceivePack[0],2); |
| | | memcpy(&g_com_map[BIND_DISTANCE],&bind_distance,2); |
| | | save_com_map_to_flash(); |
| | | break; |
| | | case DELETE_ALL: |
| | | memset(tag_id_authorization_list,0,sizeof(tag_id_authorization_list)); |
| | | break; |
| | | case DELETE_PART: |
| | | delete_authorization_list(&mUsart2ReceivePack,&tag_id_authorization_list,&tag_id_authorization_list_num);//删表中元素操作 |
| | | break; |
| | | case SET_DISTANCE_DETERMINATION: |
| | | memcpy(&button_determinate_distance,&mUsart2ReceivePack[0],2);//设定开关判断距离 |
| | | memcpy(&g_com_map[DETERMINATION_DISTANCE],&button_determinate_distance,2); |
| | | save_com_map_to_flash(); |
| | | break; |
| | | case SET_FREQUENCE_BY_DISTANCE: |
| | | memcpy(&change_by_frequency_distance,&mUsart2ReceivePack[0],2);//设定附近距离 |
| | | memcpy(&g_com_map[FREQUENCY_DISTANCE],&change_by_frequency_distance,2); |
| | | tag_near_frequency=mUsart2ReceivePack[2];//获取设定频率 |
| | | memcpy(&g_com_map[NERA_FREQUENCY],&tag_near_frequency,1); |
| | | save_com_map_to_flash(); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 重置状态机 |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | index = 0; |
| | | frame_index = 0; |
| | | break; |
| | | |
| | | default: |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | index = 0; |
| | | frame_index = 0; |
| | | break; |
| | | } |
| | | |
| | | |
| | | } |
| | | void Usart1ParseDataHandler(uint8_t data) |
| | | { |
| | | if(state5V_prase_flag&&!g_com_map[MODBUS_MODE]) |
| | | { //升级程序 |
| | |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |