Help with ModBus RTU Master-Slave: SimpleModbus [SOLVED]

Juan,

I am trying to use your Simple Modbus lib without success. I have read through all the post here and tried each of the suggestions to no avail. I even order the the resistors and MAX487s from digiKey per your schematic. I have rewired the board twice in hopes of getting it to work. I get more than 200mv across the 120 resistor on the comm line. What is the best way to trouble shoot this? I have tried the serial dump and I get 10 tries and 10 fails. I added the delay(3) to the master cpp code. Still not working.

Oh... I am using IDE 1.0.5 and 2 uno R3 boards.

Any ideas?

Thanks
-=Ryan

Hi ryardman,

Have you used SimpleModbusMasterArduino.ino and SimpleModbusSlaveArduino.ino sketch together? The examples are made to work with each other and with proper wiring this should work "right out of the box". At first do not try to change the example code. Just programm UNOs to these 2 sketches and rewire board. Be carefull how to connect resistors. And maybe if you have an oscilloscope put an image of your "dataflow" here.
Hf, Jakob

Hi Jakob,

Thanks for the reply. Yes I am using those 2 sketches and only made the change to add the delay(3) as you did to the master cpp. I have rewired the board a few times now, still no luck. When you say becareful with the resistors where should they be?

I have a xProtoLab micro oscilloscope... but I not sure where to connect the wires. I tried using the protocol sniffer on the comm lines and it displays a bunch of HEX codes.

I will take some photos of my wiring and upload them in another post.

-=Ryan

photos of my modbus with max487 wiring
-=Ryan

At first resistors between A and B line are meant to be positioned at the end of lines. And for testing on breadboard you could try to connect none or only one resistor at the end of A-B line. Try to remove pull-up and pull-down resistors, and remove everything from the A-B line except 120E resistor. The LEDs are not meant to be there.

Then the software side: the delay I have used in library isn't 3 seconds but it is 3us so use the delayMicroseconds() function.

I will try your suggestions. thanks. What do you mean by the LEDs are not meant to be there? I thought the sketches control the Dimness of LEDs via pin 9?

Without the LEDs how would you know if the modbus is working?

Thanks
-=Ryan

Oh sorry for that. LEDs are correct, I though they are connected to A and B line.
Hf, Jakob

OK found the problem. The code I downloaded had ArduinoSlave ID set to 1, but the master was sending to ID 2. After correcting this it worked.

Thanks

Dear Juan,

Firstly, hello from a fellow South African :smiley: Secondly, thanks so much for all your selfless work on this topic so far. I'm very impressed!

My request:
I have a Midnite Solar Classic Lite charge controller which I want to communicate with:

http://goo.gl/jCLxq4

It is capable of talking MODBUS RTU over RS232 (fixed 19200 baud) or TCP and I have tested both using modpoll (Free Modbus Master Simulator and Test Tool) on my Linux desktop.

As I have a multi-drop RS485 (9600 baud) bus on my site, I am wanting to use an Arduino Uno as a translater/converter from RS232 (using SoftwareSerial) to RS485 (using the Rx/Tx pins).

Although I'm new to MODBUS, from how I understand things MODBUS is MODBUS, irrespective of the underlying physical layer (RS232/485 or TCP). My question to you is this. Is it possible to use SimpleModbus over RS232 and if so what should be changed in your library (if anything) to get this to work?

Many thanks
Roy
P.S. From this post:

https://code.google.com/p/simple-modbus/issues/detail?id=10

it seems like I would have to have the following hardware setup:
Uno -> RS485 transciever -> RS485 to RS232 converter -> charge controller

P.S.2 Another option would be to get an ethernet shield and just communicate via TCP using this library:

Hi Ryan,

My apologies. I have fixed the slave example in SMSV8.

Hi Roy,

Thanks for the South Africa greeting XD

Modbus is the software protocol and RS232/422/485 is the topology.

Since there is a difference in RS232 and TTL232 you will need a level translator to level shift the +-10V RS232 signal to 0 - 5V TTL232 for the arduino.

I had a look at the CLASSIC LITE 250 manual and could not find anything related to modbus RS232 only tcp which is way different.

On the simplemodbus site you will find a diy level shift circuit which I have tested extensively and works very well indeed.

So basically you want to use the arduino to act as a master (using the software serial library) to communicate with the CLASSIC LITE 250 which has only RS232 at 19600 baud. The arduino will than also act as a slave on your RS485 9600 baud bus system. All this communication works using modbus. Is all this correct so far?

If the train of though above is correct than the modbus master library needs to be changed to accommodate the software serial core library. This is easy.

Modbus is the software protocol and RS232/422/485 is the topology.

Yes I am aware of this :slight_smile:

Since there is a difference in RS232 and TTL232 you will need a level translator to level shift the +-10V RS232 signal to 0 - 5V TTL232 for the arduino.

Yes, I am using this little board from Sparkfun:

I had a look at the CLASSIC LITE 250 manual and could not find anything related to modbus RS232 only tcp which is way different.

I agree , their documentation of the MODBUS over RS232 functionality is very sparse. The Midnite solar folks have a cool forum filled with very helpful people. I have fully documented what I've done to communicate with the Classic via RS232 from my desktop using the modpoll simulator. Suffice to say it certainly does work:

http://midniteforum.com/index.php?topic=1431.0

So basically you want to use the arduino to act as a master (using the software serial library) to communicate with the CLASSIC LITE 250 which has only RS232 at 19600 baud. The arduino will than also act as a slave on your RS485 9600 baud bus system. All this communication works using modbus. Is all this correct so far?

If the train of though above is correct than the modbus master library needs to be changed to accommodate the software serial core library. This is easy.

Yes, this is almost correct. I do have the option to communicate with the Classic via MODBUS over TCP but that would mean having to get an ethernet shield which I would prefer not to do right now. Getting MODBUS over TCP going on the Arduino is however on my todo list.

Also, as I am building up a general purpose logger with the intention of being able to communicate with as many different devices as possible and with MODBUS being one of the most widespread industrial protocols in use, I would very much like to be able to do MODBUS over RS232 too, preferably with the same code base.

I hope this explains things a bit more clearly? I'm glad that you say it should be easy to do what I need to do. I will take a look at your code again and see whether I can figure it out myself.

Thanks in advance.

Roy

Hi Roy,

Is there no way to change the baud rate of the CLASSIC LITE 250 or the baud rate of the other slave devices on your bus?
To adhere the to the modbus protocol a few things must be implemented by the manufacturer like odd parity, choice between 9600 and 19200 baud rates and RTU mode. Most devices do implement this. If the controller can't then maybe the other devices can.

If you can change the baud rates on either side than you can just use an auto receive/transmit circuit like the one on the simplemodbus site and supply it with an auxiliary 5V supply from the controller (if it has).

I you can't change anything well... If you want I will help you out on changing the library for SoftwareSerial. Bear in mind that SoftwareSerial uses an interrupt to receive which is very cpu intensive. How SoftwareSerial and HardwareSerial interact is something of a prediction :relaxed:

I'd like to implement a sort of Modbus RTU over Bluetooth protocol.
I tried to use SimpleModbus library, Master Bluetooth send right command to Slave Bluetooth, but Slave didn't answer.

But If I send string command from the Master, Slave answer well and all works.

I understand that "problem" is in SimpleModbus library that is thinked and structured for RS485 channel.
But how and where could I change SimpleModbus library to work well both on RS485 anb Bluetooth channel?

P.S.: Sorry for my English :blush:

Hy schiavanoelettro1,

I cant help you with that, just want to say, if you make it works, please post here code. I might need this in future, who knows. Also what BT modules are you using?

Regards, Jakob

Hi schiavanoelettro1,

I need a bit more information to diagnose. What type of BT module are you using? Is there a spec sheet? How are you interfacing with the module? TTL232, RS232, SPI, bit bang? What size is the receive buffer of the module? What is the through put of the module?

Juan Bester

I'm using a pair of HC-05 Bluetooth Module with backplane without RS232 converter.
One is configured as Master and the other as slave.

http://www.exp-tech.de/service/datasheet/HC-Serial-Bluetooth-Products.pdf

To do my experiments, I use 2 Arduino Mega, so I can read data flow on serial monitor.

Serial parameters are same for all:
in modbus library I put : 9600,8,N,1
in every BT modules I put: 9600,8,N,1.

What is the matter, doctor?

The manual is a bit much to go through now but if you are sure that master and slave are correctly paired by verifying this on the led connections than it seems like the BT module is just a TTL232 to BT translator.

I couldn't find anything on response times from switching over from Tx to Rx but this shouldn't matter much as modbus is a master request slave response protocol. Although RS485 is mostly used RS232 or TTL232 is no problem as modbus is just the protocol, any topology could be used.

There is the inter-character and frame delays that must be taken into account though. I would try to change the T1_5 constant by changing "T1_5 = 16500000/baud" in line 412 in the SimpleModbusMaster.cpp file to some larger value, say "T1_5 = 5000". This will allow the BT modules buffer to fill while the arduino is processing the reply.

The same has to be done in SimpleModbusSlave.cpp in line 60. Change "T1_5 = 15000000/baud" to "T1_5 = 5000".

I would also increase the timeout value depending on the BT modules response and the distance between master and slave.

Could luck.

Sometimes slave reply, sometimes no.

But surely it is a forward step.

:slight_smile:

I tried to increase both T1_5 (inter-character delay) and T3_5 (frame delay) up to 20000, but nothing changed.

Only few times slave answer correctly.

So?