Hello Guest it is March 29, 2024, 10:05:23 AM

Author Topic: Help Arduino Modbus RTU  (Read 12395 times)

0 Members and 1 Guest are viewing this topic.

Help Arduino Modbus RTU
« on: January 19, 2013, 08:03:56 AM »
Hi guys
I'm trying to use my arduino mega as Modbus but I can not activate inputs or outputs to put buttons and LEDs.
The feed and speed potentiometers for misalignment with the brain
http://www.machsupport.com/forum/index.php/topic,22980.0.html

For example like putting a running Cycle Start button

And LED lights to run Cycle Start

I hope you can help me

thanks
Re: Help Arduino Modbus RTU
« Reply #1 on: January 27, 2013, 05:48:18 AM »
Hello dotn:

I have a short tutorial to help you get started. It would be helpful to print it out as a reference as
you start writing the code into the framework of the modbus program from Zafar. It ends up getting
fairly long. You might just start with making a "Cycle Start" button, and then add more stuff after you
get that part working.

If you have a working knowledge of Arduino I/O codeing, this is the standard Arduino
sketch code sections of: declaration, setup, and "void" loop sections of the modbus program.

Example:

This part is the declaration of pins and variables. After the beginning where the code
has: int t1,t2,t3; (this is the same as A0, A1, A2 for the analog pins.

plus the declaration for input pins;

int inPin2 = 2; //This is digital pin D2
int inPin3 = 3; // this is digital pin D3  etc.
int inPin4 = 4; // ........

The Mega has lots of pins. You can do this up to inPin 25 or 30 if you want.
--------------------------------------------------------------------------------------------------------------------
Save some pins for Outputs in the declaration;

int outPin1 = 13; // D13, this is the built in led pin. You can make it blink on as an output.
int outPin2 = 25; // D25, this is Mega digital pin 25
int outPin3 = 26; // D26
int outPin4 = 27; .....etc.

int buttonState1 = 0;  // you will need the declaration of "buttonState" for reading the input state of pins in the "void" section.
int buttonState2 = 0;
int buttonState3 = 0;


---------------------------------------------------------------------------------------------------------------------------

Then in the next area : (this is copied directly from the program to show location)

/* Modbus RTU common parameters, the Master MUST use the same parameters */
enum {
  MB_SLAVE = 1,   /* modbus slave id */
};
/* slave registers example */
enum {   
     
  MB_REG0,  // analog pin 0
  MB_REG1,  // analog pin 1
  MB_REG2,  // analog pin 2
  //
  MB_REG3,  // digital pin 2
  MB_REG4,  // digital pin 3
  MB_REG5,  // digital pin 4 

These are the modbus registers listed by number. The program allows up to 40 registers.
You have to label the inputs (as above shown), and the outputs in the same list.
It is not necessary to  go all the way to 40 registers, but you can if needs be.
Every comma, underline, semi-colon is absolutely necessary. The program will not
compile is so much as one comma is out of place.

The double slash, // is to precede any comments you want to add for keeping track of what
the program lists are all about.

------------------------------------------------------------------------------------------------------------------------
Now is the "setup" section of the program: The following lines are copied from the program to show
location, and some of my own code, i.e., Cycle Start.......


int regs[MB_REGS];    /* this is the slave's modbus data map */

void setup()
{

  pinMode(inPin2, INPUT);  // D2, Cycle Start  (connect your pushbutton to pin D2.)
  pinMode(inPin3, INPUT);  // D3, Feed Hold
  pinMode(inPin4, INPUT);  // D4, Stop
  pinMode(inPin5, INPUT);  // D5, Reset
  pinMode(inPin6, INPUT);  // D6, Spindle on/off

This is the standard Arduino sketch pinMode listing for INPUT and OUTPUT

  pinMode(outPin1, OUTPUT); // D13, Led pin 13 output
  pinMode(outPin2, OUTPUT); // D36,
  pinMode(outPin3, OUTPUT); // D37,
  pinMode(outPin4, OUTPUT); // D38,

This listing will include all the inputs and outputs you need.

---------------------------------------------------------------------------------------------------------

Now comes the real deal. This is the "void" loop. All your program tasks happen here.



void loop()
{
  /* This is all for the Modbus slave */
  update_mb_slave(MB_SLAVE, regs, MB_REGS) ;

  // analog input pins ----------
  regs[0]=analogRead(A0);  // this is the area reading the analog inputs.
  regs[1]=analogRead(A1);
  regs[2]=analogRead(A2);
  // ----------------------------
  regs[3]=digitalRead(inPin2);  // this is the area reading the digital input pins.
  regs[4]=digitalRead(inPin3);
  regs[5]=digitalRead(inPin4);
  regs[6]=digitalRead(inPin5);
  regs[7]=digitalRead(inPin6);

 
Every digital input and output, and every analog input must have it's own register;  regs[16] etc.


Here is a short code to turn on a green LED in combination with the Cycle Start button, and turn off the yellow, and red
led's that match the Feedhold and Stop buttons.

This is at the end section of the "void" loop, and before the loop closing bracket.

// Read "Cycle Start" button; if activated, turn on green LED
  buttonState1 = digitalRead(inPin2);
  if(buttonState1 == HIGH) {
    digitalWrite(outPin2, HIGH);
    digitalWrite(outPin3, LOW);
    digitalWrite(outPin4, LOW);
  }

As you can see, I connect a green led to "outPin2"
a yellow led to "outPin3"
and a red led to "outPin4"

You can write similar code yourself, to turn on/off various led's to match the
buttons pushed. 

All the stuff in the "void" loop must be inside of loop starting bracket....

void setup()



and the ending bracket in the program....

}

---------------------------------------------------

Now you need to write the brains to implement the input buttons. Zafar made sample brains for that already.

Do just a section at a time when you start writing it all in the program. Do a "compile" to find any mistakes before you have
too much code written down.

------------------------------------------------------


Have fun.

John


Re: Help Arduino Modbus RTU
« Reply #2 on: January 27, 2013, 05:42:02 PM »
Much of the modbus code writing is not "intuitive". It takes some time to get it all correct. For instance, in this modbus program, the entries into the Serial Modbus screen, under the "Function Cfg's" , the type of input in the right side column labeled, Direction, must be "Input-Holding."

I tried using the "Input-reg", which works with my CubLoc PLC, and it was a no-go.

I will try to put together a step by step on the strange way (strange to me anyhow) the brains have to be written. You should use the test box in the Serial Modbus screen to make sure your pushbuttons, and/or analog potentiometers are working before trying to get a brain to communicate with Mach3. For pushbuttons, you will see a list of zeros, one for each input, change from a zero to a (1) as you click on the "read" button, as you push the buttons on your panel, or proto board. With the analog inputs, you will see the numbers change from 0 to 1023 as you turn the pot, and click on the read button.

If it works correctly, you know the register# under the "address" column heading is correct, and the communication is working.

The modbus program for Arduino does work well, and with the Arduino Mega, you have a really large number of I/O possible, including using the serial I/O for an LCD, and the PWM for spindle control. It is a very powerful program, however it takes time to get all the bits correct.

The parts of my coding I posted all work correctly. There should be enough of the code to get a picture of how to write your own variety of this modbus program. The potential for 40 individual I/O's, analog, digital, PWM, Serial,  is certainly not a trivial program to add to the Mach3 control.

I am not an Arduino guru programmer, and do not claim to fully understand how this modbus program works; however, it is a masterpiece of software engineering, and does allow the Arduino standard code, which is fairly straight forward, and simple to understand to work within the main program.

This code, generously placed in the public domain can do more than many commercial PLC's costing hundreds of dollars.


John






Re: Help Arduino Modbus RTU
« Reply #3 on: January 31, 2013, 05:43:10 PM »
Hi guys
I'm trying to use my arduino mega as Modbus but I can not activate inputs or outputs to put buttons and LEDs.
The feed and speed potentiometers for misalignment with the brain
http://www.machsupport.com/forum/index.php/topic,22980.0.html

For example like putting a running Cycle Start button

And LED lights to run Cycle Start

I hope you can help me

thanks

Hi Have a look at the article I wrote. I should help.

http://www.homanndesigns.com/pdfs/Using_Modbus_with_Mach3.pdf

Cheers,

Peter.
----------------------------------------------------
Homann Designs
http://www.homanndesigns.com
email: peter at homanndesigns.com
Re: Help Arduino Modbus RTU
« Reply #4 on: February 06, 2013, 01:04:41 PM »
Hello,
 if you want to see this article in this blog
http://www.biemmeitalia.net/blog
Is very good