当前位置:   article > 正文

普天视PTS-3130C 模拟网络AHD监控 三维云台键盘 PELCO解析程序_pelcod测试程序

pelcod测试程序

1.协议说明

​​​​PELCO-D协议
注释:
1.校验和为byte2-byte6。
2.命令对应的是键值。

2.PELCO-D解析程序

//--------------------头文件-----------------------------------------
#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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

3.Hex转ACSII解析

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);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

4.使用示例程序

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;
        }
       }
     } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

5.测试结果

在这里插入图片描述

注释说明

pelco解析参考网上代表,并做了补充,如有侵权,请联系本人。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/518683
推荐阅读
  

闽ICP备14008679号