首页 > 解决方案 > 在 Arduino 上使用 Modbus TCP

问题描述

我有一个连接到 arduino uno 的 scd30 传感器。scd30 工作在 I2c 协议上。我能够在 arduino IDE 的串行监视器上实时读取数据。我的arduino上有一个以太网屏蔽。我希望 arduino 与现场代理进行通信,该代理会将数据上传到互联网。

我已经尝试了许多 modbus tcp 库,但似乎没有得到任何地方。我可以将我的 arduino 连接到现场代理,但每当它发送数据时,我都会收到 0x02 异常代码 - 非法数据地址。这是使用https://github.com/andresarmento/modbus-arduino/tree/master/libraries/ModbusIP/examples的图书馆

我相信正确的方法是通过保存寄存器,但我不确定在使用 i2c 时如何做到这一点。连接很好,问题是格式。感谢任何帮助。

/*
  Reading CO2, humidity and temperature from the SCD30
  This example prints the current CO2 level, relative humidity, and temperature in C.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>
#include <Wire.h>
#include <Streaming.h>
#include "SparkFun_SCD30_Arduino_Library.h" 

SCD30 airSensor;
//Modbus Registers Offsets (0-9999)
const int SENSOR_ISTS = 100; 
//ModbusIP object
ModbusIP mb;
long ts;    

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("SCD30 Example");

 // The media access control (ethernet hardware) address for the shield
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
    // The IP address for the shield
    byte ip[] = { 000 , 00,0, 00  }; 
    byte gateway[] = { 0, 0, 0, 0 };  
    byte subnet[] = { 255, 255, 255, 0 }; 
    //Config Modbus IP 
    mb.config(mac, ip,gateway,subnet);
    // Add SWITCH_ISTS register - Use addIsts() for digital inputs 
    mb.addHreg(SENSOR_ISTS);
  airSensor.begin(); //This will cause readings to occur every two seconds

}

void loop()
{

mb.task();
   mb.Hreg(SENSOR_ISTS, digitalRead(airSensor.getTemperature()));

}

标签: arduinosensorsarduino-unoi2cmodbus-tcp

解决方案


我已经阅读了你的问题。在我看来,您首先必须创建一个本地服务器,例如thingspace ( https://thingspace.verizon.com/ ) 或其他在线本地服务器,您可以从那里轻松处理来自传感器的数据。

您正在使用库中的代码,因此它必须以任何方式正确。所以,在我看来,你应该检查数据交易。

希望我的ans帮助你谢谢!


推荐阅读