STM32F103 + enc28J60 + Modbus-TCP – Timeouts after x seconds

p2baron
Sun May 13, 2018 10:31 am
Is there anyone having stability issues with the UIPEthernet library?

I’m doing a project which involves reading a modbus slave every few second.

This work perfectly for some time but then stops working. The stm32 seems to lose connectivity with the enc28j60 modules. (tx light stops flashing)
I’m using the ModbusTCP and UIPEthernet libraries.The enc28J60 is powered externally.

Could it be something with the watchdog timer? The ModbusTCP example sketch had some code to disable the watchdog timer which did not compile for the STM32.

Any help is appreciated.

This is my code:

#define ENC28J60 = 1
#include <ModbusTCP.h>
unsigned int param_value_int[7];
#include <UIPEthernet.h>
//#include <avr/wdt.h>

IPAddress ModbusDeviceIP(192, 168, 1, 181);
IPAddress moduleIPAddress(192, 168, 1, 12); // Anything other than the device IP Address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE2 };

#include <Arduino.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

ModbusTCP node(1);

void setup(void) {
Serial2.begin(9600);
Serial2.println("banaan");
//u8g2.begin();
Ethernet.begin(mac);
node.setServerIPAddress(ModbusDeviceIP);
}

void loop(void) {
uint8_t result;
result = node.readHoldingRegisters(2, 1);
delay(100);
Serial2.print("hola: ");
Serial2.println(result, HEX);
if(result != 0)
{
node.MBconnectionFlag = 0;
Serial2.println("TimeOut");
if(result == 5) {
//Ethernet.begin(mac, moduleIPAddress);
//wdt_enable(WDTO_8S);
//WDTCSR |= _BV(WDIE);
}
delay(6000);
}
int len = node.getResponseBufferLength();
Serial2.println("Response Length: " + String(len));
for (byte j = 0; j < len; j++)
{
Serial2.print(node.getResponseBuffer(j));
Serial2.print(" ");
}
Serial2.println();
node.clearResponseBuffer();
delay(100);

}


p2baron
Sun May 13, 2018 10:53 am
Re-initializing the enc28j60 connection solves the issue. I do get a timeout now and then but the connection is automatically reestablished.

if(result == 5) {
Ethernet.begin(mac, moduleIPAddress);
}


RogerClark
Sun May 13, 2018 9:24 pm
WDT is not enabled by default, but you can enable and configure it if necessary, however the code will be differnt to the AVR code as I don’t think the WDT is part of the main Arduino API, and the code in the library you are using is likely to be AVR specific

staticmem
Mon May 14, 2018 12:12 am
Back in the early 00s I had to program up a RS232 implementation of the Modbus protocol for a customer. I remember it had strict timing requirements in servicing responses (or along those lines) or it will time-out. The Modbus protocol should cover the timing requirements.

p2baron
Mon May 14, 2018 7:50 am
Thanks

[RogerClark – Sun May 13, 2018 9:24 pm] –
WDT is not enabled by default, but you can enable and configure it if necessary, however the code will be differnt to the AVR code as I don’t think the WDT is part of the main Arduino API, and the code in the library you are using is likely to be AVR specific

That makes sense. It think the code in the sketch is there to prevent the MCU from resetting during communication.
Also I read that STM32 watchdog cannot be switched off once enabled.

[staticmem – Mon May 14, 2018 12:12 am] –
Back in the early 00s I had to program up a RS232 implementation of the Modbus protocol for a customer. I remember it had strict timing requirements in servicing responses (or along those lines) or it will time-out. The Modbus protocol should cover the timing requirements.

It is the MCU losing connection to the ENC28J60 module. I can succesfully read the registers for some time before the connections drops.
I’ve also tried different modules. Also read that I’ll need to use (external) pull-ups on the i2c line. Guess I’ll have to do some further debugging. :(


stevestrong
Mon May 14, 2018 9:24 am
I used to use enc28 modules, but not anymore. They are not very reliable.
Instead I am currently using W5500 modules, they are faster and Arduino compatible.

p2baron
Mon May 14, 2018 1:30 pm
[stevestrong – Mon May 14, 2018 9:24 am] –
I used to use enc28 modules, but not anymore. They are not very reliable.
Instead I am currently using W5500 modules, they are faster and Arduino compatible.

thanks for the tip!

Too bad I have a bunch enc28 lying around.
Luckily the W5500 modules are dirt-cheap on ebay. Ordered two of them.

In the mean time any recommendations to improve stability with ENC28 modules are welcome!


luca_stm32
Mon May 21, 2018 7:38 pm
Hi p2baron.
Sorry for my late response.
I used this library for ENC28J60 and it is working with blu pill: https://github.com/UIPEthernet/UIPEthernet

Hope this could help you.
Luca


p2baron
Tue May 22, 2018 12:32 pm
[luca_stm32 – Mon May 21, 2018 7:38 pm] –
I used this library for ENC28J60 and it is working with blu pill: https://github.com/UIPEthernet/UIPEthernet

Thanks Luca – I’m already using the latest version from that library. You don’t have any stability issues? Did you do anything special code or wiring-wise?


luca_stm32
Wed May 23, 2018 5:13 am
I can’t see any stability issue, but I have watchdog enabled.
I saw your code but I can’t find where you call Ethernet.maintain(); function: in main loop you have to call it as often as you can.
In your code I noticed also that you have a delay(6000); maybe you can repalece it with:
unit32_t delay_time;
if ((millis() - delay_time) < 6000) Ethernet.maintain();

asmallri
Wed May 23, 2018 10:33 pm
[staticmem – Mon May 14, 2018 12:12 am] –
Back in the early 00s I had to program up a RS232 implementation of the Modbus protocol for a customer. I remember it had strict timing requirements in servicing responses (or along those lines) or it will time-out. The Modbus protocol should cover the timing requirements.

I have done a lot of Modbus RS232 work. I suspect you were dealing with a specific device with these constrains because Modbus itself does not provide strict timing and instead proposes timeouts in the range of 1 second to several seconds depending on the baud rate. From a “what I have observed” I have seen device response times range from less that 0 (one product family started transmitting the response before the stop bit serialization from the sender had completed) ranging up to 2 seconds.


staticmem
Sat May 26, 2018 8:33 am
[asmallri – Wed May 23, 2018 10:33 pm] –

[staticmem – Mon May 14, 2018 12:12 am] –
Back in the early 00s I had to program up a RS232 implementation of the Modbus protocol for a customer. I remember it had strict timing requirements in servicing responses (or along those lines) or it will time-out. The Modbus protocol should cover the timing requirements.

I have done a lot of Modbus RS232 work. I suspect you were dealing with a specific device with these constrains because Modbus itself does not provide strict timing and instead proposes timeouts in the range of 1 second to several seconds depending on the baud rate. From a “what I have observed” I have seen device response times range from less that 0 (one product family started transmitting the response before the stop bit serialization from the sender had completed) ranging up to 2 seconds.

Long time-ago, but yes it was probably time-outs I was actually referring to and not so much at a bit time critical level.


Leave a Reply

Your email address will not be published. Required fields are marked *