Modbus TCP using Mudbus Library - Very slow connection


Currently I'm working on a project where I need a connection between an Arduino and a PLC operating on Codesys software. For this connection I've chosen to use an Arduino Ethernet shield and Modbus to communciate with the PLC.

I've got the connection set up and I can set registers with the Arduino, and read registers with the PLC, but there is a problem: the PLC only updates the variables every 4 seconds. I've read some online forums where people talked about a TCP Push bit and a 'nodelay' activation or something, but i can't seem to find this anywhere in the Mudbus Library. Any ideas how to get a faster connection?

Hardware and software:

#include <SPI.h>
#include <Ethernet.h>

#include "Mudbus.h"

Mudbus Mb;
//Function codes 1(read coils), 3(read registers), 5(write coil), 6(write register)
//signed int Mb.R[0 to 125] and bool Mb.C[0 to 128] MB_N_R MB_N_C
//Port 502 (defined in Mudbus.h) MB_PORT

void setup()
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 172, 16, 40, 215 };
  uint8_t gateway[] = { 172, 16, 40, 254 };
  uint8_t subnet[]  = { 255, 255, 0, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);
  //Avoid pins 4,10,11,12,13 when using ethernet shield


void loop()
  Mb.R[0] = Mb.R[0] + 123;
  Mb.R[1] = Mb.R[1] + 123;

Well, any "run" method combined with a delay() call is suspicious...

@aarg Deleted the delay(), works like a charm. Thank you!

Do you perhaps know why this happens?

Yes, "run" methods are a feature of round-robin multitasking. Tasks can only be shared in real time, if they are called in real time. Delays defeat real time operation by slowing it down. In order for it to work, the loop() function must complete in a few milliseconds at most. If that doesn't happen, tasks are stalled.

That explains it, thanks!