Modbus Energy Monitor


I found this library.
I think it would be of great benefit to be port for Particle.
The library supports several types of energy meters: Eastron SDM120, SDM220, SDM530 and SDM630.
I do not know how specific to a AVR platform, I hope that can be easily adjusted to STM (Particle).

[MODBUS] SDM120 Modbus Energy Meter - decoding output

I tried to compile the example.

Problem occurs only with declaration of serial port.
As I can see, there is nothing specific on the AVR platform.
It probably will not be very difficult to make a declaration of the hardware serial port on Electron / Photon/Core.


I managed to ported library Web IDE to compile without problem.
But I have a problem that I do not know how to solve.
Somewhere in the Arduino libraries has SERIAL_TX_BUFFER_SIZE definition but that definition does not in Particle platform.
I now manually define #define SERIAL_TX_BUFFER_SIZE 256 in the file ModbusSensor.h. But I do not know how to find the appropriate required in Particle serial port (256 I define randomly.) :grinning: .


I would use SERIAL_BUFFER_SIZE. You shouldn’t need to include anything to use it. The TX and RX buffers are the same size.


With Arduino compile without problem with SERIAL_TX_BUFFER_SIZE.


With the change of

      // If ((* _hwSerial) .availableForWrite () == SERIAL_TX_BUFFER_SIZE - 1) {// TX buffer empty
  //  with
           if ((* _hwSerial) .availableForWrite () == SERIAL_BUFFER_SIZE - 1) {// TX buffer empty

compile without problem :grinning:


How did you connect the RS485/Modbus kWh to the Particle?


@developer_bt Care to share? I’ve made the same changes, including the following:

#define MB_SERIAL_PORT &Serial1   // Arduino has only one serial port, Mega has 3 serial ports

However I’m getting all values printed as zeros. Same modbus settings as other (successful) libraries, so I know these are correct.



I’ve also updated my config to match my device:

#define MB_SERIAL_PORT &Serial1   // Arduino has only one serial port, Mega has 3 serial ports.
// if use Serial 0, remember disconect Tx (pin0) when upload sketch, then re-conect
#define MB_BAUDRATE       2400          // b 2400
#define MB_BYTEFORMAT     SERIAL_8E1    // Prty n
#define TxEnablePin       A1

#define ID_1  1