CEM5861G毫米波雷达

分享作者:wx17466371946072
评测品牌:萤火工场
评测型号:CEM5861G-M11
发布时间:2026-03-09 09:14:15
1
概要
串口通信收发数据帧
开源口碑分享内容

作用:主要用来设置有人运动、有人静止,无人状态的切换

接线



可以通过读取串口的数据帧来判断有人运动、有人静止,无人状态

波特率:115200

字宽: 8
停止位: 1
奇偶校验: none

雷达输出格式:

帧结构定义
1、 帧头,2字节
上位机发送,雷达接收: 0x55 0x5A
雷达发送,上位机接收: 0x55 0xA5
2、 数据长度,2字节,高字节在前,低字节在后。
长度 = 功能码 + 命令码 + 数据 + 校验和
3、  功能码,1字节
读: 0x0
写: 0x1
被动上报: 0x2
主动上报:   0x3
读写命令为上位机向雷达发送指令, 上报命令为雷达向上位机发送信息。
4、 命令码
命令码1 为功能分类, 命令码2 表示具体功能。
5、 数据
N字节
6、  校验和,1字节
为校验和之前所有数据按uint8_t格式相加之和的低8位。







需要改进:

毫米波雷达引出的引脚太小,测量的精度不够高

优点:

配备了上位机,可以可视化调试


代码

// 定义帧头

const uint8_t FRAME_HEADER_TX[2] = {0x55, 0x5A}; // 上位机发送帧头

const uint8_t FRAME_HEADER_RX[2] = {0x55, 0xA5}; // 上位机接收帧头


// 打开串口

int openSerialPort(const char* portName)

{

  int fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);

  if (fd == -1)

  {

      perror("Failed to open serial port");

      return -1;

  }


  struct termios options;

  tcgetattr(fd, &options);

  cfsetispeed(&options, B115200);

  cfsetospeed(&options, B115200);

  options.c_cflag &= ~PARENB; // 无奇偶校验

  options.c_cflag &= ~CSTOPB; // 1位停止位

  options.c_cflag &= ~CSIZE;

  options.c_cflag |= CS8;     // 8位数据位

  options.c_cflag |= (CLOCAL | CREAD);

  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 原始模式

  options.c_iflag &= ~(IXON | IXOFF | IXANY); // 禁用流控

  options.c_oflag &= ~OPOST; // 原始输出


  if (tcsetattr(fd, TCSANOW, &options) != 0)

  {

      perror("Failed to condiv serial port");

      close(fd);

      return -1;

  }

  return fd;

}

void receiveData(int fd)

{

  char buf[128];

  int n = read(fd, buf, sizeof(buf));  

  if (buf[0] != FRAME_HEADER_RX[0] || buf[1] != FRAME_HEADER_RX[1])

  {

      return;

  }

  // 读取数据长度(大端格式)

  uint16_t length = (buf[2] << 8) | buf[3];

  printf("length: %d\n", length);

  printf("id= %d, status=%d, distance=%ucm\n", buf[7], buf[8], ((buf[9] << 8) | buf[10]));

  return;

}

int main()

{

  int fd = openSerialPort("/dev/ttyS0");

  if (fd == -1)

  {

      return 1;

  }

  while(1)

  {

      receiveData(fd);

      usleep(10000);

  }

  return 0;

}



全部评论
暂无评论
0/144