library for Modbus

Hello,
I'm trying to understand the transmission protocols transmitted over RS485
I have many difficulties
about master the candidate is:
http://www.arduino.cc/playground/Code/ModbusMaster
for the slave I found:
http://code.google.com/p/arduino-modbus-slave/
for this after changing
#include "Wprogram.h" changed #include "Arduino.h."

I can compile the examples.

I can understand the using of two separate libraries, but these are compatible?
or they speak two different dialects?

can I find some more information or some examples

Thanks very much

Luca

I gather those two pieces of code come from different sources?

If so I would not expect them to work together out of the box although you may be lucky, after all they are supposed to be written to the same spec but then IIRC there are different versions of Modbus..

Modbus is pretty complicated IMO, you may be able to do something much simpler yourself.

What are you trying to do?


Rob

Try just getting the slave working first with the PC as the master.
There are many SCADA apps out there for free.

Also, I think you should be able to use RS232-RS485 converters, so you can stay away from the 485.

Graynomad:
Modbus is pretty complicated IMO, you may be able to do something much simpler yourself.

Thank you for the answer,
I would like to better understand how to communicate 3/4 Arduino each other
In my personal opinion some libraries do a portion of job for me simplifying it :blush:

@ OZ_Willy great help
I found some, which you suggest ?
a converters from serial port to RS485 is very simple: a max232 some electrolytic capacitor and a max481

I have an excellent setup with Mango Automation. I have Mango getting data from two Arduino's over Xbee via a
Digi Connectport X4. The X4 converts the Modbus RTU from the Arduino to Modbus TCP.

You probably will want to start with Modbus RTU over RS232.
The following SHOULD work (Not personally tested):

  1. Install Mango, with its Modbus module and com port drivers.
  2. Install the Modbus slave software (the one you are working with is the same one I have used)
    on your arduino, just use a few digital input registers at this stage.
  3. Hook up some switches to your arduino on the corresponding pins you allocated in your slave code.
  4. Connect the arduino to your Mango PC with a USB cable and take note of the com port number.
  5. In mango, go to data sources, add the arduino com port, save.
  6. Use the point locator test with the slave id and register, less 1!(Teh modbus offset or register address is 0 based in Mango,
    so 1 in the slave code will be 0 in Mango), input register, binary data.
    If all is good, you should get back the status of your switch at that address.
  7. Add this data point and then play around with mango.
  8. Then get your analog inputs, outputs and digital outputs into the system.

Keep in mind that although the Addresses in the slave code are regBank.add(10001), regBank.add(8), regBank.add(30001), regBank.add(40001),
you ignore the address type(ie 30001) when inputting data points in Mango. The system knows it is an analog input when you select the "Register range" in Mango.
(I have obviously condensed the above steps and you will need to put your thinking cap on!)

It is worth the effort getting this working. You then SHOULD be able to implement a 485 bus with multiple
Arduino Modbus RTU slaves on it, just using a commercial RS232 to RS485 converter on the Mango PC.
If you want an Arduino to act as a Master, well ummmm, yeaaaaaa, next question, my brain hurts!

I am thinking about producing a tutorial on this subject if I get the time.

Here's the basic code I have working.

#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>

/*
Add more registers if needed
Digital input pins 2,3,4,5,6,7
Digital output pins 8,9,12,13
Analog output pins 10,11 (PWM)
Analog input pins 0,1,2,3,4,5
*/


modbusDevice regBank;
modbusSlave slave;

int AI0,AI1,AI2,AI3,AI4,AI5;


void setup()
{   
  regBank.setId(2);

//Add Digital Input registers
  regBank.add(10002);
  regBank.add(10003);
  regBank.add(10004);
  regBank.add(10005);
  regBank.add(10006);
  regBank.add(10007);
// Add Digital Output registers
  regBank.add(8);
  regBank.add(9);
  regBank.add(12);
  regBank.add(13);
//Analog input registers
  regBank.add(30001);
  regBank.add(30002);
  regBank.add(30003);
  regBank.add(30004);
  regBank.add(30005);
  regBank.add(30006);
//Analog Output registers
  regBank.add(40010);  
  regBank.add(40011);  

  slave._device = &regBank;  
  slave.setBaud(9600);   
  
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
   
}
void loop(){

  while(1){   
  //Digital Input
    byte DI2 = digitalRead(2);
    if (DI2 >= 1)regBank.set(10002,1);
    if (DI2 <= 0)regBank.set(10002,0);
    byte DI3 = digitalRead(3);
    if (DI3 >= 1)regBank.set(10003,1);
    if (DI3 <= 0)regBank.set(10003,0);
    byte DI4 = digitalRead(4);
    if (DI4 >= 1)regBank.set(10004,1);
    if (DI4 <= 0)regBank.set(10004,0);
    byte DI5 = digitalRead(5);
    if (DI5 >= 1)regBank.set(10005,1);
    if (DI5 <= 0)regBank.set(10005,0);
    byte DI6 = digitalRead(6);
    if (DI6 >= 1)regBank.set(10006,1);
    if (DI6 <= 0)regBank.set(10006,0);
    byte DI7 = digitalRead(7);
    if (DI7 >= 1)regBank.set(10007,1);
    if (DI7 <= 0)regBank.set(10007,0);
                                
  //Digital output
    int DO8 = regBank.get(8);
      if (DO8 <= 0 && digitalRead(8) == HIGH)digitalWrite(8,LOW);
      if (DO8 >= 1 && digitalRead(8) == LOW)digitalWrite(8,HIGH);
    int DO9 = regBank.get(9);
      if (DO9 <= 0 && digitalRead(9) == HIGH)digitalWrite(9,LOW);
      if (DO9 >= 1 && digitalRead(9) == LOW)digitalWrite(9,HIGH);
    int DO12 = regBank.get(12);
      if (DO12 <= 0 && digitalRead(12) == HIGH)digitalWrite(12,LOW);
      if (DO12 >= 1 && digitalRead(12) == LOW)digitalWrite(12,HIGH);
    int DO13 = regBank.get(13);
      if (DO13 <= 0 && digitalRead(13) == HIGH)digitalWrite(13,LOW);
      if (DO13 >= 1 && digitalRead(13) == LOW)digitalWrite(13,HIGH);
            
  //Analog input
    AI0 = analogRead(0);
    delay(10);
    AI0 = analogRead(0);
    regBank.set(30001, (word) AI0);
    delay(10);
    
    AI1 = analogRead(1);
    delay(10);
    AI1 = analogRead(1);
    regBank.set(30002, (word) AI1);
    delay(10);
    
    AI2 = analogRead(2);
    delay(10);
    AI2 = analogRead(2);
    regBank.set(30003, (word) AI2);
    delay(10);
    
    AI3 = analogRead(3);
    delay(10);
    AI3 = analogRead(3);
    regBank.set(30004, (word) AI3);
    delay(10);
    
    AI4 = analogRead(4);
    delay(10);
    AI4 = analogRead(4);
    regBank.set(30005, (word) AI4);
    delay(10);
    
    AI5 = analogRead(5);
    delay(10);
    AI5 = analogRead(5);
    regBank.set(30006, (word) AI5);
    delay(10);
        
  //Analog output 
    word AO10 = regBank.get(40010);
    analogWrite(10,AO10);
    delay(10);
    word AO11 = regBank.get(40011);
    analogWrite(11,AO11);
    delay(10);
        
  slave.run();  
  }
}

Many thanks for the suggestions
I'm working on it!

OZ_Willy:
If you want an Arduino to act as a Master, well ummmm, yeaaaaaa, next question, my brain hurts!

I am thinking about producing a tutorial on this subject if I get the time.

About master may I look at linux, and a smart solution could be Raspberry pi....
what do you think about this?

about a tutorial it is a great idea!
If I can help you somehow...

I have a pi. I am going to try to run Mango on it as a SCADA server.

My main interest with all this is cheap wireless sensor networks.

By the way, in my code example, the slave is set to 2. (regBank.setId(2):wink:
It was the last node I programmed with it.

I'll be updating this as I go
MAX 485 & ModbusMaster - Basic Guide
http://arduino.cc/forum/index.php/topic,132177.0.html

Thank you so much
but can I see simple example, very basic.

regards

Luca

How does download Modbus slave.h library

We'll use Nkhil's dedicated thread to help them with their question:
https://forum.arduino.cc/index.php?topic=618258