|
各位大神,求助你们arduino modbusRTU通讯读取温湿度传感器数据的问题
读取总不成功
还报地址不合法
读取语句和地址怎么编写好呢
这是我在IDE上输入的程序
#include <ArduinoModbus.h>
float temperature;
float humidity;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Modbus Temperature Humidity Sensor");
if (!ModbusRTUClient.begin(9600))
{
Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
else{
Serial.println("start Modbus RTU Client");
delay(5000);
}
}
void loop() {
if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0000, 2))
{
Serial.print("failed to read registers! ");
Serial.println(ModbusRTUClient.lastError());
} else
{
short rawtemperature = ModbusRTUClient.read();
short rawhumidity = ModbusRTUClient.read();
temperature = rawtemperature / 10.0;
humidity = rawhumidity / 10.0;
Serial.println(temperature);
Serial.println(humidity);
}
delay(5000);
}
|
|