Another Modbus thread

Hi everyone,

First of all, thanks to all of the contributors I’ve been reading from. I’ve avoided posting as I was quite new, but I’ve been going at this problem for a few weeks now!

I’m trying to read my solar controller registers over Modbus, which I was able to easily do with an arduino. I’ve tried every library port I can find, and have adapted various peoples code to my requirements and can’t get anything out of it. I’ve also tried 2 boards (Sparkfun and max485).

My most recent attempt, which I feel is the one that should definitely work uses this library by peergum https://github.com/peergum/ModbusMaster-Electron, which is an adaptation of the arduino modbus library that I used on the arduino.



Here is the latest code I’ve been trying, any tips would be appreciated so much… a bit gutted after waiting so long to get an electron and doing all the prep on an arduino, only to find the work doesn’t translate!



 #include "ModbusMaster.h"

/*!
  We're using a MAX485-compatible RS485 Transceiver.
  Rx/Tx is hooked up to the hardware serial port at 'Serial'.
  The Data Enable and Receiver Enable pins are hooked up as follows:
*/
#define MAX485_DE      D3
#define MAX485_RE_NEG  D2

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 115200 baud
  Serial.begin(115200);

  // Modbus slave ID 1
  node.begin(1, Serial1);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

bool state = true;

void loop()
{
  uint8_t result;
  uint16_t data[6];
  
  // Toggle the coil at address 0x0002 (Manual Load Control)
  result = node.writeSingleCoil(0x0002, state);
  state = !state;

  // Read 16 registers starting at 0x3100)
  result = node.readInputRegisters(0x3100, 16);
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Vbatt: ");
    Serial.println(node.getResponseBuffer(0x04)/100.0f);
    Serial.print("Vload: ");
    Serial.println(node.getResponseBuffer(0xC0)/100.0f);
    Serial.print("Pload: ");
    Serial.println((node.getResponseBuffer(0x0D) +
                    node.getResponseBuffer(0x0E) << 16)/100.0f);
  }

  delay(1000);
}

What charge controller are you trying to pull data from?

From a tracer 4020bn… Here’s the register list and connection info… https://www.google.co.uk/url?sa=t&source=web&rct=j&url=http://www.solar-elektro.cz/data/dokumenty/1733_modbus_protocol.pdf&ved=2ahUKEwjlwaDEqe_YAhXrL8AKHTtVCfQQFjABegQIDxAB&usg=AOvVaw2l5P3iHoscUNM1t4jzkCSc