Go Down

Topic: communication modbus rs485, HELP!! (Read 1 time) previous topic - next topic


Jun 10, 2013, 06:57 pm Last Edit: Jun 11, 2013, 06:12 pm by danielvillarrealg Reason: 1
hi, i need communicate a device with my arduino using the modbus rs485 protocol, i don´t understand the few examples in the web, maybe some one can give me a example more easy.
My communication is half-duplex i´m using the the RS-485 Breakout of sparkfun (https://www.sparkfun.com/products/10124).
I was read about of the SimpleModbus-Library(http://playground.arduino.cc/Code/ModbusMaster), but i don´t find any example about it.

i hope your comments, thanks! :)


i follow investigating, and i am using the simplemodbus-library,  this is my code, but i don´t know how to test if mi communication to discard this posible problem, and too i don´t know how read the receiver value, i tryin read the value like the comment line:
Code: [Select]

#include <SimpleModbusMaster.h>
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate

#define retry_count 5

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2

   // leave this last entry

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];

// The data from the PLC will be stored in the regs array
unsigned int regs[10];
long val = 0;

void setup()
    // modbus_construct(packet, id, function, address, number of registers, register array)
     modbus_construct(packet1, 1, READ_HOLDING_REGISTERS, 3001, 20, regs);

     modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);

void loop()
////////////tryin to read////////////////////////////////////////
  while(val=Serial.available() > 0){
    for(int i=0; i<val; i++){
      regs[i] = Serial.read();

  // update the array with the counter data
  regs[3] = packet1->requests;
  regs[4] = packet1->successful_requests;
  regs[5] = packet1->failed_requests;
  regs[6] = packet1->exception_errors;


if have a suggestion can help.


Well you have most of the code commented out, that won't help.

Surely this modbus library has a call to receive a "packet" or whatever they are called, you shouldn't have to read each byte yourself.


Rob Gray aka the GRAYnomad www.robgray.com


I use SimpleModbus[Master/Slave] library too, and it works great.

Take a look at this walkthrough on modbus through rs485, it contains examples that can help you



Thanks Graynomad, you´re right, that code don´t help, i need find a way to read the packet, but before, i need confirm my communication, i now understand a little bit more the library and noticed that this lines:

  regs[3] = packet1->requests;
  regs[4] = packet1->successful_requests;
  regs[5] = packet1->failed_requests;
  regs[6] = packet1->exception_errors;

are a counters, and i printed the regs[], and the successful_requests (regs[4] = packet1->successful_requests;) always are 0, my communication it´s bad, i need first confirm the communication to continue.

And ellepiu thanks, your links helped me for understand a little bit more this library.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131