链接: https://pan.baidu.com/s/1J42i3Co-ZO5aiLM59T4FXA 提取码: 7ykt
文档说明
本文档基于Arduino UNO硬件开发板测试
本例程适用北醒单点雷达TF40
准备
硬件:
- 北醒TF40单点测距雷达
- Arduino UNO开发板
- 直流3.3V(1A)供电电源
- Window 10系统电脑一台
- 其它USB、杜邦线等配线
软件
- Arduino 编译软件 测试版本1.8.13
Arduino程序关联库(测试版本)
- ArduinoRS485 1.0.0
- ModbusMaster-2.0.1
接线示意图
== 注 ==
TF40供电不能直接使用UNO板的3.3V,驱动能力不足,会导致无法正常启动,详细见下图
例程:
#include <ModbusMaster.h>
#include<SoftwareSerial.h>//软串口头文件
SoftwareSerial Serial1(2, 3); //定义软串口名称为Serial1,并把pin2定为RX,pin3定为TX
// instantiate ModbusMaster object
ModbusMaster TF40;
unsigned int distance;
boolean receiveComplete = false;
void setup()
{
Serial.begin(38400);
Serial1.begin(38400);//设置雷达与arduino连接串口的波特率
// communicate with Modbus slave ID 2 over Serial (port 0)
TF40.begin(1, Serial1);
}
void loop()
{
GetLidarMesure(&distance,&receiveComplete);
}
void GetLidarMesure(unsigned int* distance, boolean* complete) {
uint8_t j, result;
uint16_t data[2];
result = TF40.readHoldingRegisters(15, 2);
delay(1000);
// do something with data if read is successful
if (result == TF40.ku8MBSuccess)
{
//Serial.println(result);
for (j = 0; j < 2; j++)
{
data[j] = TF40.getResponseBuffer(j);
Serial.print("0x");
Serial.print(data[j], HEX);
Serial.print(";");
}
}
*distance = data[1];
*complete = true;
}