Arduino Nano Modbus RTU

Moderator: Benj

Post Reply
jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Arduino Nano Modbus RTU

Post by jhill »

I have stumbled across a bit of an odd problem with using the Modbus RTU component on Arduino Nano's both Genuine and clones. It is repeatable over several samples so I don’t believe it to be hardware related.

If the device is flashed via the USB port, then Modbus RTU queries are subsequently answered over the USB(serial) port without issue. If the device is reset using the reset button, it continues to work. If the device is power cycled by un-plugging, and plugging back in, Modbus RTU communication over USB no longer works. If I then re-flash the device, Modbus starts to work again. After the power cycle, the program is running (its not the well know bootloader problem), its just the modbus serial com’s is broken.

This seems to be specific to the Nano, (both 9600 and 115200 programming types). Uno's and Mega's dont have this issue.

I did think it might be the com port speed not being reset correctly but this does not seem to be the case.

Any advice would be gratefully received!

Best Regards

James

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: Arduino Nano Modbus RTU

Post by EtsDriver »

Hi!

Have you tried loopback test at your modbus RTU speed and putty to test out that the serial passthrough really works?
Are you sure the USB serial adapter has not reset the speed for some reason?
Have you tried disabling and then enabling the USB adapter again, will it allow the serial speed again?

Have you tried to add some delay 100-500ms before initializing the modbus RTU component? Do you run the "Arduino Optiboot" bootloader? Is reset pin floating?

One thing that comes to mind is that the buffer on FTDI chip or driver has some leftovers and jams the communication after optiboot bootloader runs... Can you try resetting the device and flushing the RS232 buffer (like pressing enter before RTU communication)?

I unfortunately don't have FC8 but have some experience with other modbus RTU and TCP/IP devices, so I'll try to help you out with the tips i have on my mind!

Best Regards;
Jari
Ill just keep the good work up!

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: Arduino Nano Modbus RTU

Post by jhill »

Hi JAri.
Thank you very much for your advice!

I have created a simple loop back program as per your suggestion, this works without issue after a power cycle.
As far as i can see, the speed is not being reset. I have a genuine Nano with FTDI converter and programmed at 115200, and a clone with ch340 converter and programmed at 9600. I am running Modbus RTU at 9600, but have tried 115200 in case it is reverting. The problem persists across both devices and speeds.
I have tried disabling and re-enabling the device in device manager, it does not restore communication.
I suspected an initialisation problem, so tried various delays, but this didn’t help ether. I am running the standard arduino bootloader, i believe this is optiboot. I am aware there can be an issue with this, but as the actual program is executing correctly, its just the modbus (after a cold start) that isn’t working, I think the boot loader is working and the device isn’t being held in reset.

Given the loopback test works ok after a power cycle, it seems the communications side is working, however the definitely seems to be something that happens on a cold start that blocks modbus communications. It seem to be on the microntroller side as I can see the receive LED blinking in time with the modbus requests, that telsl me the interface is working.

The problem is also specific to the Arduino Nano, exactly the same project works without issue after a power cycle on an arduino uno PDIP R3.

Although they are both ATmga328's, is there something different in the flowcode target configuration that could be causing this issue?

Best Regards

James

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: Arduino Nano Modbus RTU

Post by EtsDriver »

Can you try to generate the program for Atmega328p target and program the generated .hex to arduino with Arduino Xloader tool (you might not be able to do this if you don't have AVR pack) ? This way you would make the effectivily same code that could run on AtMega328p on Nano and Uno? This would rule that problem is only on Arduino Nano target generated code...

Is your code shareable/would you mind sharing it with us to help pinpoint the problem bit more?

As you say, the program was working correctly on the Uno target, so it sounds that the generated code has some underlying problem on Arduino Nano target.
Ill just keep the good work up!

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: Arduino Nano Modbus RTU

Post by jhill »

Hi Jari,
Thank you again for your thoughts! Interestingly i had tried using xloader, but with the hex file created using the Arduino Nano target, and this yielded the same results.
I have now however done as you suggested, and switched to a AtMega328p target (I have the AVR Pack fortunately) and loaded the HEX file with xloader. This results no modbus communications at all.
It seem that I have been thinking about this the wrong way around, there is something that the flowcode flasher is doing something that leaves this working, that is then lost on a power cycle, that xloader does not?
I have reduced the program to its most minimal, leaving only the modbus part, that show this issue.
Best Regards
James
Attachments
Fan controller V0.97_modbus test_minimal.fcfx
(16.07 KiB) Downloaded 249 times

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: Arduino Nano Modbus RTU

Post by EtsDriver »

Hi, did you check the fusebits were correct for nano? (clockspeed etc?)

Can you try to generate the .hex for Uno target and then program Nano with it and check does it still work same way?
Ill just keep the good work up!

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: Arduino Nano Modbus RTU

Post by jhill »

Hi Jari,

Good idea, i will give that a try in the new year, unfortunatly i dont have access to the hardware for a few days....

Best Regards

James

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: Arduino Nano Modbus RTU

Post by jhill »

Hi,

Unfortunately i had to deploy this project for a while without modbus RTU so it has taken we a bit of time to get back to it. To follow up on Jari's suggestions,

Yes, generating the HEX file for uno, then loading with xloader results in the same behaviour.

In my previous attempt to compile to hex for the non Arduino atmega328p I had indeed neglected to adjust the fuse bits in flowcode.

I have tried:-
Low fuse = 0xFF (B11111111)
High fuse = 0xDA (B11011110) & 0xDE (B11011110)
Extended fuse = 0x05 (B00000101)
as detailed here http://www.martyncurrey.com/arduino-atm ... -settings/

But unfortunately nether result in running firmware.

Best Regards,

James.

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: Arduino Nano Modbus RTU

Post by EtsDriver »

Hi James!

I generated your software for Atmega328P target on my Flowcode 7 and changed the name to shorter one to get it into my Arduino Uno.

Without any changes, it works on my Uno as specified.

I unfortunately dont have Nano at hand, but two things come into my mind:

You could add a start-up delay to try prevent the modbus Slave RS232 buffer being filled partly if device was on bootloader mode when Modbus Master tried to poll it...

Try to "clean" RS232 buffer before initializing modbus.

I programmed it with following CMD line, as Xloader freezes on new Win10:

Code: Select all

c:\Xloader\avrdude.exe -C C:\Xloader\avrdude.conf -v -v -v -v -p atmega328p -c stk500 -P \\.\COM10 -b 115200 -D -U flash:w:C:\hexfiles\FAN.hex:i
Default fuses for Arduino Uno 328P are
LOW = 0xff
HIGH = 0xde
EXT = 0x05
Could someone with Arduino Nano try program this Hex file and try read input registers from slave 10?

Modpoll can be used to test RS232.
Attachments
fan_modbus.JPG
fan_modbus.JPG (75.31 KiB) Viewed 9891 times
FAN.hex
(8.37 KiB) Downloaded 203 times
Ill just keep the good work up!

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: Arduino Nano Modbus RTU

Post by jhill »

Hi,

Thank you again for your help with this!

I programmed your HEX file to a NANO but as with my previous attemps I was unable to get any reponse at all. Has anybody else been able to try this, or indeed have modbus serial working after a power cycle on a nano?

Best Reagrds

James

Post Reply