RS-485 modbus library

Hello Folks,
I will share some of my experiences with mod-bus libraries test that I have done so far, kindly ask suggestion for a problem that I having right now with particle photon.

My intention is pulling some data from holding register on VFD Control techniques.
For some reason i din’t have success using this library,Smarmengol-Modbus Library, the problem I have no idea how to address a holding register like this one 401811, if i try to set 1811 register the arduino or photon
hangs and do nothing.

With this library “4-20ma” work fine with arduino… I use software serial to connect the MAX485, and that work perfect, I was able to read any register the VFD… I tried that same library with the ESP8266 and works too… the problem… if one disconnect the max485 from the ESP8266 or simple disconnect the cable from slave, the ESP8266 crash after a few intents of read the registers…timeouts…
I tried to determine the problem… but I am not so advanced programmer to dig into the library … so I gave up … I read somewhere thi is because the library Software Serial + mod-bus disable the GPIO pin interrupts very often . Now I move to try with Particle photon. initially I was having a problem very often got timeout while trying to read 10 holding registers… mysteriously solved
for my test I just used the “4-20ma” library without any modification.
this is the sample code:

#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      A3
#define MAX485_RE_NEG  A2
#define CT_VFD(m,n)  (m *100+ n-1)  // formula to acces VFD registers

// instantiate ModbusMaster object
ModbusMaster node;
#define reg_qty 11
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(19200);
  Serial1.begin(19200);
  // Modbus slave ID 1, Serial1 on the Particle Photon
  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);

  // read   holding register VFD Control_techniques  starting at Pr 1811  ( modbus 401811- 401814)
   result=node.readHoldingRegisters(CT_VFD(18,11), reg_qty);
  if (result == node.ku8MBSuccess)
  {
    Serial.println("Data: ");
   // Serial.println(node.getResponseBuffer(1));
    for (uint8_t j = 0; j < reg_qty; j++)  Serial.println(node.getResponseBuffer(j),DEC);

                  Serial1.flush();
                 // node.clearTransmitBuffer();
  }
  else {
    Serial.println("NO- RESPONSE"); // i got not response  very oftem
    //mySerial.flush();

  }
 //node.clearResponseBuffer();
//  node.clearTransmitBuffer();
  delay(5000);
 Serial.println("running...");
}

Questions that I have so maybe some one can provide some advise
-The modbus master library ported by @peekay123 do not work for me . i got some errors… I see this library was ported 3 years ago… so maybe that is the reason… Not clear if 4-20ma library specially ported for spark has any advantage over the un-ported 4-20ma master library.
-this is another diferent library Modbus_RS485 that seems pretty interesting… but as is it right now I can’t get it to read any modbus registers on the VFD using photon not sure if i did right to addressee the register using this line -->telegram[1].u16RegAdd = 1811; // start address in slave

1 Like