赞
踩
注释:
1.校验和为byte2-byte6。
2.命令对应的是键值。
//--------------------头文件----------------------------------------- #pragma once #include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" typedef struct{ unsigned char sync; unsigned char addr; unsigned char cmd1; unsigned char cmd2; unsigned char data1; unsigned char data2; int len; } pelco_d_typ_t; extern pelco_d_typ_t pelco_d_data; int find_PELCO_D(unsigned char *buff_in, unsigned char *buff_out, int len); //--------------------C文件----------------------------------------- #include "PELCO_D.h" pelco_d_typ_t pelco_d_data; static int dump_cmd_data(unsigned char *buff_in,unsigned char *buff_out ,int len) { int cmd_size = len; if(len == 0 )return 0; for(int i= cmd_size;i>0;i--){ buff_out[i] = buff_in[i]; } buff_out[0] = 0xff; return cmd_size; } static int pelco_d_parse_data(unsigned char *buff_in,int len){ if(len < 7)return 0; if(buff_in[0] != 0xff)return 0; pelco_d_data.sync = 0xff; pelco_d_data.addr= buff_in[1]; pelco_d_data.cmd1= buff_in[2]; pelco_d_data.cmd2= buff_in[3]; pelco_d_data.data1= buff_in[4]; pelco_d_data.data2= buff_in[5]; pelco_d_data.len= len; return 0; } int find_PELCO_D(unsigned char *buff_in, unsigned char *buff_out, int len) { unsigned char *tmp = NULL; int cksm = 0; unsigned char ch = 0; int tmp_len = len; int cmd_len = 0; unsigned char *cmd_ptr = NULL; tmp = buff_in; if(len == 0 )return 0; while (tmp_len > 0) { while (*tmp != 0xff) { tmp++; tmp_len--; if (tmp_len <= 0) goto end; } cmd_ptr = tmp; // 第一个字节 ch = *(++tmp); cksm += ch; cmd_len = 2; tmp_len--; while (tmp_len > 0) { ch = *(++tmp); cmd_len++; tmp_len--; if (ch == cksm && cmd_len >= 7) { dump_cmd_data(cmd_ptr,buff_out,cmd_len); pelco_d_parse_data(buff_out,cmd_len); //数据处理 cksm = 0; return cmd_len; } cksm += ch; cksm %= 0x100; } end:; } return cmd_len; }
int sy_map_hex_to_string(uint8_t *str, uint8_t *strout, int in_len) { int i = 0; uint8_t tem; uint8_t tem_l, tem_h; if(in_len == 0) return 0; for (i = 0; i < in_len; i++) { tem = *str; str++; tem_h = (tem & 0xf0) >> 4; tem_l = tem & 0x0f; if (tem_h >= 10) { strout[i * 3] = (tem_h - 10) + 'A'; } else { strout[i * 3] = tem_h + '0'; } if (tem_l >= 10) { strout[i * 3 + 1] = (tem_l - 10) + 'A'; } else { strout[i * 3 + 1] = tem_l + '0'; } strout[i * 3 + 2] = ' '; } return (in_len * 3); }
void main(){ // 缺省 while(1){ int fifo_len = 0; size_t uart2_data_len = 0; //接收键盘串口程序 if(uart_get_buffered_data_len(UART_NUM_2,&uart2_data_len)==ESP_OK){ uart_read_bytes(UART_NUM_2,test_buff,uart2_data_len,200); fifo_len = uart2_data_len; } int pelco_d_len = find_PELCO_D(test_buff, test_buff_out, fifo_len); sy_map_hex_to_string(test_buff_out, rec_buff_out, pelco_d_len); if (pelco_d_data.len > 0) { printf("rec: %s\r\n", rec_buff_out); if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x08)) //up { printf("up\n"); } else if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x10)) //down { printf("down\n"); } else if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x04)) //left { printf("left\n"); } else if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x02)) //right { printf("right\n"); } else if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x0c)) // left+up { printf("left+up\n"); } else if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x14)) // left+down { printf("left+down\n"); } else if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x0A)) // right+up { printf("right+up !!\n"); } else if ((pelco_d_data.cmd1 == 0x00) && (pelco_d_data.cmd2 == 0x12)) // right+down { printf("right+down !!\n"); } pelco_d_data.len = 0; } } }
pelco解析参考网上代表,并做了补充,如有侵权,请联系本人。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。