Go Down

Topic: MKR1000 and Modbus TCP  (Read 217 times) previous topic - next topic

dano3829

Hello,

Found a Couple of stalled threads on here about MKR1000 and Modbus TCP. Just curious if anyone has figured out how to get Modbus TCP to work MKR1000.

I currently use the https://github.com/andresarmento/modbus-arduino library to get Modbus Serial to work over a USB connection to my Ignition Server. It works, I can read data, Historize it, Chart it.

It appears the Andreas Modbus Library has support for ESP8266, but what is the WIFI chip on the MKR1000. WINC1500?

Also looks like some guys have some Modbus TCP to work with no libraries.
http://en.trialcommand.com/blog/esp8266-slave-modbus-tcpip-no-libraries/

Also Modbus TCP is a bit of a dinosaur. But there's low level devices that talk it. Also the Ignition SCADA system I use has driver for it.

Maybe I need to migrate to MQTT and get away from Modbus?

Juraj

#1
Aug 26, 2018, 02:29 pm Last Edit: Aug 26, 2018, 02:30 pm by Juraj
There is nothing special with Modbus TCP for MKR1000 with WiFi101 library. I have modbus TCP in my project, and I moved it from Uno with WiFiLink to Mega with Ethernet shield 1 or Ethernt shield 2, to esp8266 and then to M0 with Ethernet shield. All proper Arduino networking libraries implement the same base classes Server, Client and UDP.

Code: [Select]
/*
 * return
 *   - 0 is success
 *   - negative is comm error
 *   - positive value is modbus protocol exception code
 *   - error 4 is SLAVE_DEVICE_FAILURE. Check if 'Inverter control via Modbus' is enabled.
 */
int modbusRequest(byte uid, unsigned int addr, byte len, short *regs) {

  const byte CODE_IX = 7;
  const byte ERR_CODE_IX = 8;
  const byte LENGTH_IX = 8;
  const byte DATA_IX = 9;

  int err = modbusConnection();
  if (err != 0)
    return err;

  byte request[] = {0, 1, 0, 0, 0, 6, uid, FNC_READ_REGS, (byte) (addr / 256), (byte) (addr % 256), 0, len};
  modbus.write(request, sizeof(request));

  int respDataLen = len * 2;
  byte response[max((int) DATA_IX, respDataLen)];
  int readLen = modbus.readBytes(response, DATA_IX);
  if (readLen < DATA_IX) {
    modbus.stop();
    return MODBUS_NO_RESPONSE;
  }
  switch (response[CODE_IX]) {
    case FNC_READ_REGS:
      break;
    case (FNC_ERR_FLAG | FNC_READ_REGS):
      return response[ERR_CODE_IX]; // 0x01, 0x02, 0x03 or 0x11
    default:
      return -3;
  }
  if (response[LENGTH_IX] != respDataLen)
    return -2;
  readLen = modbus.readBytes(response, respDataLen);
  if (readLen < respDataLen)
    return -4;
  for (int i = 0, j = 0; i < len; i++, j += 2) {
    regs[i] = response[j] * 256 + response[j + 1];
  }
  return 0;
}

Modbus.ino as a part of my project

Go Up