With OPTA only Modbus RTU sniffing

Hey everyone,
i'm new to this so i will try to get it as detailed as possible.
I have a PV inverter with a smart meter so the inverter can track the needs of energy. Because it's not easy to get the Data directly from the Inverter I thought about sniffing the Data from the Modbus RTU connection and process them. So I did code a program for an Arduino R3 with which I could sniff all the data I wanted. I know the address of the message so I can look up if it's a request or an answer and if I check how long the answer is, I can cut the Data where I want it to.

#include <SPI.h>
#include <MsTimer2.h>
#include <stdlib.h>
#include <Wire.h>


void loop() {
  char received;
  //get Data
  if (Serial.available() > 0) {
    received = Serial.read();
    RecvBuffer[AnzMsg][recvIndex++] = received;
  }

  //length of the message
  if (recvIndex == 4 & !anfrageData) {
    Wert2 = (uint8_t)RecvBuffer[AnzMsg][2];
    if (Wert2 <= 1) {
      lengthofMsg[AnzMsg] = 8;
    } else if (Wert2 > 1) {
      lengthofMsg[AnzMsg] = (uint16_t)Wert2 + 5;
    }
    anfrageData = true;
    // reinitialize vars for next detection/dump
    dumpData = false;
    MsTimer2::stop();
    timerStarted = false;
  }
  //do some stuff
}

But because I wanted to get some relays and other stuff running I thought about switching to the OPTA and had there I didn't get this code runnign so I tried to switch to the code from the example and wanted to change that, but my knowledge with the libareys are too small. I can get the OPTA to request data from the Smart Meter and to read the Data which comes back with something like:

float getT_Float(int dev_address, int base_reg) {
  int l;
  l = ModbusRTUClient.requestFrom(dev_address, INPUT_REGISTERS, base_reg - 30001, 2);

  if (l > 0) {
    while (!ModbusRTUClient.available()) {}
    uint32_t rawreg = ModbusRTUClient.read() << 16 | ModbusRTUClient.read();
    float reg;
    memcpy(&reg, &rawreg, sizeof(float));
    return reg;
  } else {
    return 0;
  }
}

But because the inverter is requesting all the time I can't do that. I don't know how I can only read everything that will be sent on the Modbus RTU libary for OPTA.

Greeting
Simon

Instead of using ModbusRTUClient, manually read data from Serial1:

void loop() {
  if (Serial1.available()) {
    uint8_t received = Serial1.read();
    Serial.write(received);  // Print to serial monitor for debugging
  }
}

This will print everything that is being transmitted on the Modbus RTU bus.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.