Skip to content

Reading Holding Register with length of 2 and unit id 3 #364

Open
@MeisterQ

Description

@MeisterQ
/*
  Modbus-Arduino Example - Master Modbus IP Client (ESP8266/ESP32)
  Read Holding Register from Server device

  (c)2018 Alexander Emelianov ([email protected])
  https://github.com/emelianov/modbus-esp8266
*/

#ifdef ESP8266
 #include <ESP8266WiFi.h>
#else
 #include <WiFi.h>
#endif
#include <ModbusIP_ESP8266.h>

const int REG = 30775;               // Modbus Hreg Offset
IPAddress remote(192, 168, 188, 152);  // Address of Modbus Slave device
const int LOOP_COUNT = 10;

ModbusIP mb;  //ModbusIP object

void setup() {
  Serial.begin(115200);
 
  WiFi.begin("---", "------");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  mb.client();
}

uint16_t res = 0;
uint8_t show = LOOP_COUNT;

void loop() {
  if (mb.isConnected(remote)) {   // Check if connection to Modbus Slave is established
 // Serial.print("Conencted to Modbus server ");
    mb.readHreg(remote, 30775, &res, 3);  // Initiate Read Hregs from Modbus Server

  } else {
    mb.connect(remote);           // Try to connect if no connection
    Serial.println("Not connected");
  }
  mb.task();                      // Common local Modbus task
  delay(100);                     // Pulling interval
  if (!show--) {                   // Display Slave register value one time per second (with default settings)
    Serial.println(res);
    show = LOOP_COUNT;
  }
}

Im trying to connect to an Inverter and read register 30775 with the length of 2. But it gives me back 0. If i connect with qmodmaster it shows a value.

Device got Unit Id 3. I dont exactly get how you define it in mb.readHred. Ive i look at the describtion i need to fill more informations. Like register length, a NULL and so on?

image

image

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions