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.
/*
* 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;
}