在Arduino上使用Modbus TCP

问题描述 投票:0回答:2

我有一个连接到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()));

}
arduino sensor arduino-uno i2c modbus-tcp
2个回答
2
投票

我看过你的问题。在我看来,你首先要创建一个本地服务器,就像在诸如thingpace(https://thingspace.verizon.com/)或其他在线本地服务器之类的地方,你可以从那里轻松处理来自传感器的数据。

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

希望我的ans帮助你谢谢!


0
投票

ModbusIP库希望您提供寄存器的值。 AirSensor库为您提供该值。

将寄存器值设置为Href:

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

我在没有传感器库的情况下测试了你的草图,它正在工作。客户端是我的java测试客户端,用于测试对光伏系统的Modbus TCP寄存器的访问。

确保客户端调用“0x03 - 读取保持寄存器”并测试地址100和101,因为某些modbus客户端偏移量是从1开始的。

© www.soinside.com 2019 - 2024. All rights reserved.