目录
- 前言
- Benewake(北醒) TF-LC02产品简要说明
- Arduino开发板介绍
- Benewake(北醒) TF-LC02 接口及通讯协议说明
- 接口定义
- 串口协议说明
- 通讯协议说明
- 功能码说明
 
- 接线示意图
- 例程说明
- 配置软硬串口
- 定义获取TOF数据的结构
- 获取雷达距离数据的协议解析
- 通过主循环发送获取距离指令,并打印结果(可根据需求打印结果)
- 打印通用Ascii 码结果
 
- 完整例程分析
前言
本例程仅用作参考
Benewake(北醒) TF-LC02产品简要说明
性能参数
  产品图片及尺寸
产品图片及尺寸
 
Arduino开发板介绍
参考链接:常用Arduino板介绍
Benewake(北醒) TF-LC02 接口及通讯协议说明
接口定义

串口协议说明


通讯协议说明

功能码说明


接线示意图

 注:线路颜色仅供参考,具体参照实际线路颜色定义
例程说明
配置软硬串口
- 引入软串口连接雷达,用硬串口来打印雷达收到并处理后的数据
#include <SoftwareSerial.h>        //软串口头文件
SoftwareSerial Port_Debug(2, 3);   //定义软件口名称和PIN2为RX PIN3为TX
void setup() {
/********************************
-  TOF串口协议:TTL
-  波特率:115200
-  数据位:8
-  停止位:1
-  奇偶校验:无
-  流控:无
**************************************/
 Serial.begin(115200);       //通过硬件串口来获取实时性要求比较高的雷达数据,软串口容易出现掉帧的情况   
 Port_Debug.begin(115200);
}
定义获取TOF数据的结构
- 设置获取距离的指令
u8 cmd[5] = {0x55, 0xAA, 0x81, 0x00, 0xFA};  //获取距离指令
typedef struct {
 int distance;
 u8 ErrorCode; //TOF错误码请参考使用说明书
 boolean receiveComplete;
} TF;
TF Lidar = {0,0,false};
获取雷达距离数据的协议解析
/***************************************
 -  通讯协议:
 -  2 byte : 帧头 0x55 0xAA
 -  1 byte : 功能码 (详细参考使用说明) 例:0x81 获取距离值 单位:mm
 -  1 byte : 后面参数的长度
 -  N byte : 设定参数
 -  1 byte : 帧尾 0xFA
 -  ***************************************
 -  例:获取距离值
 -  Arduino 发送:55 AA 81 00 FA
 -  TOF模组回复 : 55 AA 81 03 01 55 00 FA
 **************************************/
void getLidarData(TF* lidar) {
  static char i = 0;
  static int rx[8];
  while (Port_Debug.available())
  {
    rx[i] = Port_Debug.read();
    if (rx[0] != 0x55)
    {
      i = 0;
    } else if (i == 1 && rx[1] != 0xAA)
    {
      i = 0;
    } else if (i == 7)
    {
      i = 0;
      if (rx[7] == 0xFA)
      {
        lidar->distance = rx[5] + rx[4] * 256;
        lidar->ErrorCode  = rx[6];
        lidar->receiveComplete = true;
      }
    } else
    {
      i++;
    }
  }
}
通过主循环发送获取距离指令,并打印结果(可根据需求打印结果)
void loop() {
  getLidarData(&Lidar);
  if (!Lidar.receiveComplete)
  {
    Port_Debug.write(cmd, 5);
  } else
  {
    Port_Print_Ascii(&Lidar);          // Ascii 打印输出结果
    //Port_Print_Benewake_9Byte(&Lidar);   // 北醒通用9Byte打印
    Lidar.receiveComplete = false;
    delay(33);                       //延时33ms,雷达探测速率最快33ms
  }
}
打印通用Ascii 码结果
void Port_Print_Ascii(TF* lidar)
{
  Serial.print("Dist = ");
  Serial.println(lidar->distance);
  if(lidar->ErrorCode)
  {
    Serial.print("ErrorCode = ");
    Serial.println(lidar->ErrorCode,HEX);
  }
}
串口助手显示

完整例程分析
#include <SoftwareSerial.h>        //软串口头文件
SoftwareSerial Port_Debug(2, 3);   //定义软件口名称和PIN2为RX PIN3为TX
u8 cmd[5] = {0x55, 0xAA, 0x81, 0x00, 0xFA};  //获取距离指令
typedef struct {
  int distance;
  u8 ErrorCode; //TOF错误码请参考使用说明书
  boolean receiveComplete;
} TF;
TF Lidar = {0,0,false};
void getLidarData(TF* lidar) {
  static char i = 0;
  static int rx[8];
  while (Port_Debug.available())
  {
    rx[i] = Port_Debug.read();
    if (rx[0] != 0x55)
    {
      i = 0;
    } else if (i == 1 && rx[1] != 0xAA)
    {
      i = 0;
    } else if (i == 7)
    {
      i = 0;
      if (rx[7] == 0xFA)
      {
        lidar->distance = rx[5] + rx[4] * 256;
        lidar->ErrorCode  = rx[6];
        lidar->receiveComplete = true;
      }
    } else
    {
      i++;
    }
  }
}
void setup() {
  Serial.begin(115200);       //通过硬件串口来获取实时性要求比较高的雷达数据,软串口容易出现掉帧的情况   
  Port_Debug.begin(115200);
}
void loop() {
  getLidarData(&Lidar);
  if (!Lidar.receiveComplete)
  {
    Port_Debug.write(cmd, 5);
  } else
  {
    Port_Print_Ascii(&Lidar);          // Ascii 打印输出结果
    //Port_Print_Benewake_9Byte(&Lidar);   // 北醒通用9Byte打印
    Lidar.receiveComplete = false;
    delay(33);                       //延时33ms,雷达探测速率最快33ms
  }
}
void Port_Print_Ascii(TF* lidar)
{
  Serial.print("Dist = ");
  Serial.println(lidar->distance);
  if(lidar->ErrorCode)
  {
    Serial.print("ErrorCode = ");
    Serial.println(lidar->ErrorCode,HEX);
  }
}
void Port_Print_Benewake_9Byte(TF* lidar)
{
  u8 i = 0;
  u8 CheckSum = 0;
  u8 B_9Byte[9];
  B_9Byte[0] = 0x59;
  B_9Byte[1] = 0x59;
  B_9Byte[2] = lidar->distance & 0xFF;
  B_9Byte[3] = lidar->distance >> 8;
  B_9Byte[4] = 0x00;
  B_9Byte[5] = 0x00;
  B_9Byte[6] = 0x00;
  B_9Byte[7] = 0x00;
  for(i=0;i<7;i++){
    CheckSum += B_9Byte[i];
  }
  B_9Byte[8] = CheckSum & 0xFF;
  Port_Debug.write(B_9Byte,9);
}
//void serialEvent() {
 // getLidarData(&Lidar);
//}


















![[Studio3T]无限试用](https://img-blog.csdnimg.cn/c707e13ef7444e3a99fe739f0371caf5.png)