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

Hi,JuanB,
i am new for arduino and MODBUS RTU communication, i use arduino 2560 with a TOUCH SCREEN communication use MODBUS RTU protocol, i use your simple MODBUS libary V12 for the master, the voltage between A line and B line is 320MV, i found the indicate light of MAX485 TX flshing fast but Rx light flashing to slow, the data i got from the TOUCH SCREEN and write to it both not right, i use serial monitor to see it, the data changed wihtin another scan, i do not know why?
can you help me out ?

thank you very much.

zhangjianjun327

SimpleModbusMasterArduino1.ino (7.81 KB)

QQ图片20151217173651.png

Hi everybody,

I currently tried to use simplemodbusmaster library to communicate with PROCON module PM8TCISO and PM16DO (PROCON modules).
First I tried successfully with modules using direct communication with Labview, then I would like to use my M-DUINO as a master (Arduino mega with different shields) http://www.industrialshields.com/m-duino-plc-arduino-34R-i-os-relay_analog-digital
I tried to use the A and B pin (MAX-485 connect to serial 3 of arduino mega).

First, I would like to read holding register in PM8TCISO. ModBus address 30002 to 30009.
The communication of module is 9600,8 bits, no parity, 1 stop bits.
Here below the code that I tried to read the first register 30002. I defined in module slave ID as 32.

#include <SimpleModbusMaster.h>

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 300 // the scan rate
#define retry_count 50

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

#define LED 9

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 1

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

void setup()
{
  // Initialize each packet
  modbus_construct(&packets[PACKET1], 32, READ_HOLDING_REGISTERS, 0, 1, 0);
  
  // Initialize the Modbus Finite State Machine
  modbus_configure(&Serial3, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
  
  Serial.begin(9600);
  
}

void loop()
{
  modbus_update();
  
  Serial.println(regs[0]);
  Serial.println(packets[PACKET1].requests);

  delay(500);
       
  
}

The Rx Led in my proton don't blink which I can attribute to bad modes message sent. I connect A and B pins to my RS485 - UBS converter and I could see Rx led always on.

Could you say me if you find any mistakes in my code or advises?

I use simplemodbusmasterV2.rev2

thank you a lot

benoit

Benchem,
Address of holding register starts from 40001, but you need to read 30002, means you don't want to read holding registers but you have to read for input registers.

modbus_construct(&packets[PACKET1], 32, READ_HOLDING_REGISTERS, 0, 1, 0);

so here you have to make the change as replacing READ_HOLDING_REGISTER by READ_INPUT_REGISTER also you need to change that last 0 to 2 as you want to start from 30002 not from 30000.
Hope that will work!
Yatin

Thanks for your answer.
According to your answer I modified my code but nothing change.

#include <SimpleModbusMaster.h>

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 300 // the scan rate
#define retry_count 50

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

#define LED 9

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 1

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

void setup()
{
  // Initialize each packet
  modbus_construct(&packets[PACKET1], 32, READ_INPUT_REGISTERS, 2, 1, 0);
  
  // Initialize the Modbus Finite State Machine
  modbus_configure(&Serial3, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
  
  Serial.begin(9600);
  
}

void loop()
{
  modbus_update();
  
  Serial.println(regs[0]);
  Serial.println(packets[PACKET1].requests);

  delay(500);
       
  
}

When I used Labview to send a message on MobDus to request the previous I send (decimal visualisation) :

32 <-- slave ID
03 <-- fonction code
00 01 <-- register adress
00 01 <-- number of register to request
CRC code

When I read the message send from arduino it something very different :
in hex

00BB FFBB FFFD D322

Thank you

benoit

Benchem,
you need to check whether it is transmitting proper data and how it response to received so make a proteus circuit with lcd and check what data u getting yoc can check the tryal program that i used on my post #452!
20 03 00 01 00 01 D3 7B this is what master should send for slave id 32 and starting address 30002, function 3 no of reg 1 and D37B is crc! it show some thing different on lcd due to ascii but yes you can try with proteus and if it runs there you can check lcd data over there and in actual at your hardware.
YATIN

Good day to all,

I am using the simple modbus library and have implemented 1 master and 2 slaves with the arduino Mega boards. I am able to read slave registers successfully but when it comes to writing to slave registers I am having problems. My goal is basically for the master to send a packet to slave 2 setting the Pump register to a value for example say 150 and printing it to the screen to prove that it has in fact been changed by the master. My setup code for the master and slave 2 can be seen below. Any help would be greatly appreciated. Thanks.

Master setup code:

enum
{
PACKET1,
PACKET2,
PACKET3,
PACKET4,
PACKET5,
PACKET6,
PACKET7,
PACKET8,
PACKET9,
PACKET10,
PACKET11,
PACKET12,
PACKET13,
PACKET14,
PACKET15,
PACKET16,
PACKET17,
TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];
packetPointer packet3 = &packets[PACKET3];
packetPointer packet4 = &packets[PACKET4];
packetPointer packet5 = &packets[PACKET5];
packetPointer packet6 = &packets[PACKET6];
packetPointer packet7 = &packets[PACKET7];
packetPointer packet8 = &packets[PACKET8];
packetPointer packet9 = &packets[PACKET9];
packetPointer packet10 = &packets[PACKET10];
packetPointer packet11 = &packets[PACKET11];
packetPointer packet12 = &packets[PACKET12];
packetPointer packet13 = &packets[PACKET13];
packetPointer packet14 = &packets[PACKET14];
packetPointer packet15 = &packets[PACKET15];
packetPointer packet16 = &packets[PACKET16];
packetPointer packet17 = &packets[PACKET17];
unsigned int slave1Regs[12];
unsigned int slave2Regs[2];
unsigned int phRegs[5];

void setup()
{
modbus_construct(packet1, 1, READ_HOLDING_REGISTERS, 0, 1, slave1Regs);
modbus_construct(packet2, 1, READ_HOLDING_REGISTERS, 0, 2, slave1Regs);
modbus_construct(packet3, 1, READ_HOLDING_REGISTERS, 0, 3, slave1Regs);
modbus_construct(packet4, 1, READ_HOLDING_REGISTERS, 0, 4, slave1Regs);
modbus_construct(packet5, 1, READ_HOLDING_REGISTERS, 0, 5, slave1Regs);
modbus_construct(packet6, 1, READ_HOLDING_REGISTERS, 0, 6, slave1Regs);
modbus_construct(packet7, 1, READ_HOLDING_REGISTERS, 0, 7, slave1Regs);
modbus_construct(packet8, 1, READ_HOLDING_REGISTERS, 0, 8, slave1Regs);
modbus_construct(packet9, 1, READ_HOLDING_REGISTERS, 0, 9, slave1Regs);
modbus_construct(packet10, 1, READ_HOLDING_REGISTERS, 0, 10, slave1Regs);
modbus_construct(packet11, 1, READ_HOLDING_REGISTERS, 0, 11, slave1Regs);
modbus_construct(packet12, 1, READ_HOLDING_REGISTERS, 0, 12, slave1Regs);
modbus_construct(packet13, 2, READ_HOLDING_REGISTERS, 0, 1, slave2Regs);
modbus_construct(packet14, 2, READ_HOLDING_REGISTERS, 0, 2, slave2Regs);
modbus_construct(packet15, 2, PRESET_MULTIPLE_REGISTERS, 1, 1, phRegs);
Serial.begin(9600);
modbus_configure(&Serial1,baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
pinMode(8, OUTPUT);

}

Slave 2 setup code:

#include <SimpleModbusSlave.h>
#include<stdlib.h>

enum
{
T1,
T7,
Pump,
SIZE
};

unsigned int SlaveTwo;

void setup()
{
Serial.begin(9600);
modbus_configure(&Serial1,9600, SERIAL_8N2, 2, 2, SIZE, SlaveTwo);

}

Welcome to the forum ChrisR13.
A couple of things.
First, please present any code in code tags as is so often asked on this forum.

Secondly, insert the slave code in its own code tags, as I first thought it was the same code and wondered how on Earth you could get such code to compile with two functions called setup().

It is always a good idea to provide all your code, not just a part or a part where you may think the problem is.

You say

but when it comes to writing to slave registers I am having problems

, but it may not be with the writing, which is at the master end, but rather, at the slave end, of which we can not see all the code.


Paul

Hi Paul,
Thanks alot for replying and I apologize this is the first time I am posting on any forum. I hope this post is better. Now that I have posted the entire code, what I want to do is take the value stored in phRegs[0] on the master and write it to the slave 2 register called Pump. I hope you understand and thanks in advance.

Master code

#include <SimpleModbusMaster.h>
#include<stdlib.h>
#define baud 9600
#define timeout 1000
#define polling 100 
#define retry_count 5
#define TxEnablePin 2 
//#define LED 13

enum
{
  PACKET1,
  PACKET2,
  PACKET3,
  PACKET4,
  PACKET5,
  PACKET6,
  PACKET7,
  PACKET8,
  PACKET9,
  PACKET10,
  PACKET11,
  PACKET12,
  PACKET13,
  PACKET14,
  PACKET15,
  PACKET16,
  PACKET17,
  TOTAL_NO_OF_PACKETS 
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];
packetPointer packet3 = &packets[PACKET3];
packetPointer packet4 = &packets[PACKET4];
packetPointer packet5 = &packets[PACKET5];
packetPointer packet6 = &packets[PACKET6];
packetPointer packet7 = &packets[PACKET7];
packetPointer packet8 = &packets[PACKET8];
packetPointer packet9 = &packets[PACKET9];
packetPointer packet10 = &packets[PACKET10];
packetPointer packet11 = &packets[PACKET11];
packetPointer packet12 = &packets[PACKET12];
packetPointer packet13 = &packets[PACKET13];
packetPointer packet14 = &packets[PACKET14];
packetPointer packet15 = &packets[PACKET15];
packetPointer packet16 = &packets[PACKET16];
packetPointer packet17 = &packets[PACKET17];
unsigned int slave1Regs[12];
unsigned int slave2Regs[2];
unsigned int phRegs[5];

void setup()
{
  modbus_construct(packet1, 1, READ_HOLDING_REGISTERS, 0, 1, slave1Regs);
  modbus_construct(packet2, 1, READ_HOLDING_REGISTERS, 0, 2, slave1Regs);
  modbus_construct(packet3, 1, READ_HOLDING_REGISTERS, 0, 3, slave1Regs);
  modbus_construct(packet4, 1, READ_HOLDING_REGISTERS, 0, 4, slave1Regs);
  modbus_construct(packet5, 1, READ_HOLDING_REGISTERS, 0, 5, slave1Regs);
  modbus_construct(packet6, 1, READ_HOLDING_REGISTERS, 0, 6, slave1Regs);
  modbus_construct(packet7, 1, READ_HOLDING_REGISTERS, 0, 7, slave1Regs);
  modbus_construct(packet8, 1, READ_HOLDING_REGISTERS, 0, 8, slave1Regs);
  modbus_construct(packet9, 1, READ_HOLDING_REGISTERS, 0, 9, slave1Regs);
  modbus_construct(packet10, 1, READ_HOLDING_REGISTERS, 0, 10, slave1Regs);
  modbus_construct(packet11, 1, READ_HOLDING_REGISTERS, 0, 11, slave1Regs);
  modbus_construct(packet12, 1, READ_HOLDING_REGISTERS, 0, 12, slave1Regs);
  modbus_construct(packet13, 2, READ_HOLDING_REGISTERS, 0, 1, slave2Regs);
  modbus_construct(packet14, 2, READ_HOLDING_REGISTERS, 0, 2, slave2Regs);
  modbus_construct(packet15, 2, PRESET_MULTIPLE_REGISTERS, 1, 1, phRegs);
  Serial.begin(9600);
  modbus_configure(&Serial1,baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
pinMode(8, OUTPUT);

}

void loop()
{//analogWrite(8,127);
float pumpData = 0; 
float heaterData=0;
float t7SP=0;
float t1SP=0;
float pump = 0;
float heater =0;

if (Serial.available() > 0) {
          if (Serial.peek()=='p'){
            Serial.read();
          
                // read the incoming byte:
                pumpData = Serial.parseFloat();
                phRegs[0] = pumpData;
                pump = ((pumpData/300)*255);
                analogWrite(8, pump);
                // say what you got:
                //Serial.print("I received: ");
                //Serial.println(pumpData); 
                }

                if (Serial.peek()=='h'){
            Serial.read();
          
                // read the incoming byte:
                heaterData = Serial.parseFloat();
                phRegs[1]= heaterData;
                heater = ((heaterData/2000)*255);
                analogWrite(9, heater);
                // say what you got:
                //Serial.print("I received: ");
                //Serial.println(heaterData); 
                }

                 if (Serial.peek()=='a'){
            Serial.read();
          
                // read the incoming byte:
                t7SP = Serial.parseFloat();
                phRegs[2] = t7SP;
                
                // say what you got:
                //Serial.print("I received: ");
                //Serial.println(t7SP); 
                }

                 if (Serial.peek()=='b'){
            Serial.read();
          
                // read the incoming byte:
                t1SP = Serial.parseFloat();
                phRegs[3] = t1SP;
                
                // say what you got:
                //Serial.print("I received: ");
                //Serial.println(t1SP); 
                }
                
          while (Serial.available() > 0) {
                Serial.read();
          }
       }

  
  modbus_update();
  char str[10];
  float num = slave2Regs[0];
 float T1;
 T1 = ((num/1024)*150);
  
   modbus_update();
float num1 = slave1Regs[0];
 float T2;
 T2 = ((num1/1024)*150);
  
modbus_update();
    float num2 = slave1Regs[1];
 float T3;
 T3 = ((num2/1024)*150);
 
modbus_update();
    float num3 = slave1Regs[2];
 float T4;
 T4 = ((num3/1024)*150);
  
modbus_update();
    float num4 = slave1Regs[3];
 float T5;
 T5 = ((num4/1024)*150);
  
modbus_update();
    float num5 = slave1Regs[4];
 float T6;
 T6 = ((num5/1024)*150);
  
modbus_update();
    float num6 = slave2Regs[1];
 float T7;
 T7 = ((num6/1024)*150);
  
modbus_update();
    float num7 = slave1Regs[5];
 float T8;
 T8 = ((num7/1024)*150);
  
modbus_update();
    float num8 = slave1Regs[6];
 float T9;
 T9 = ((num8/1024)*150);
  
modbus_update();
    float num9 = slave1Regs[7];
 float T10;
 T10 = ((num9/1024)*150);
  
modbus_update();
    float num10 = slave1Regs[8];
 float T11;
 T11 = ((num10/1024)*150);
  
modbus_update();
    float num11 = slave1Regs[9];
 float T12;
 T12 = ((num11/1024)*150);
  
modbus_update();
float num12 = slave1Regs[10];
 float T13;
 T13 = ((num12/1024)*150);
  
  modbus_update();
    float num13 = slave1Regs[11];
 float T14;
 T14 = ((num13/1024)*150);
 
   modbus_update();
  
  
   
    const unsigned long fiveMinutes =  3 * 1000UL;
 static unsigned long lastSampleTime = 0 - fiveMinutes;  // initialize such that a reading is due the first time through loop()

 unsigned long now = millis();
 if (now - lastSampleTime >= fiveMinutes)
 {
    lastSampleTime += fiveMinutes;
    // add code to take temperature reading here
    //Serial.print ("T1 ");
    Serial.println(dtostrf(T1,4,1,str));
    //Serial.print ("T2 ");
    Serial.println(dtostrf(T2,4,1,str));
    //Serial.print ("T3 ");
    Serial.println(dtostrf(T3,4,1,str));
    //Serial.print ("T4 ");
    Serial.println(dtostrf(T4,4,1,str));
    //Serial.print ("T5 ");
    Serial.println(dtostrf(T5,4,1,str));
    //Serial.print ("T6 ");
    Serial.println(dtostrf(T6,4,1,str));
    //Serial.print ("T7 ");
    Serial.println(dtostrf(T7,4,1,str));
    //Serial.print ("T8 ");
    Serial.println(dtostrf(T8,4,1,str));
    //Serial.print ("T9 ");
    Serial.println(dtostrf(T9,4,1,str));
    //Serial.print ("T10 ");
    Serial.println(dtostrf(T10,4,1,str));
    //Serial.print ("T11 ");
    Serial.println(dtostrf(T11,4,1,str));
    //Serial.print ("T12 ");
    Serial.println(dtostrf(T12,4,1,str));
    //Serial.print ("T13 ");
    Serial.println(dtostrf(T13,4,1,str));
    //Serial.print ("T14 ");
    Serial.println(dtostrf(T14,4,1,str));

    
   
 }
 // add code to do other stuff here
 
}

Slave 2 code:

#include <SimpleModbusSlave.h>
#include<stdlib.h>

enum 
{     
  T1,
  T7,   
  Pump,         
  SIZE 
};

unsigned int SlaveTwo[SIZE];

void setup()
{
  Serial.begin(9600);
  modbus_configure(&Serial1,9600, SERIAL_8N2, 2, 2, SIZE, SlaveTwo);    
  
}

void loop()
{
  
  modbus_update();
  
  SlaveTwo[T1] = analogRead(A0);
  SlaveTwo[T7] = 600; 
  

   modbus_update();
   Serial.println(SlaveTwo[T1]);
   Serial.println(SlaveTwo[T7]);
  Serial.println(SlaveTwo[Pump]);
modbus_update();
}
}

Hi everybody,

I solved my issue next to a feedback from industrialshields. I had to use a modified simplemodbus library and carefully define my RS485 pins and defined it in modbusconstruct.
Here you can find now if it can help someone my code to read input registers and write single register to my 5 Promux slaves.

#include <SimpleModbusMasterSoftwareSerial.h>
#include <SoftwareSerial.h>

#define baud 9600
#define timeout 1000
#define polling 10 // the scan rate 
#define retry_count 10


int SSerialRX =11 ;// RO  //RS485 Receive pin
int SSerialTX=10; // DI  //RS485 Transmit pin
int  SSerialTxControl=14;  // DE  //RS485 Direction pin
int GroundRE=15 ; // RE
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);
enum
{
PACKET1,
//PACKET2,
//PACKET3,
//PACKET4,
//PACKET5,
//PACKET6,
//PACKET7,
//PACKET8,
PACKET9,
//PACKET10,
//PACKET11,
//PACKET12,
//PACKET13,
//PACKET14,
//PACKET15,
//PACKET16,
PACKET17,
//PACKET18,
//PACKET19,
//PACKET20,
//PACKET21,
//PACKET22,
//PACKET23,
//PACKET24,
PACKET25,
//PACKET26,
//PACKET27,
//PACKET28,
//PACKET29,
//PACKET30,
//PACKET31,
//PACKET32,
PACKET33,

TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
#define TOTAL_NO_OF_REGISTERS 33
unsigned int regs[TOTAL_NO_OF_REGISTERS];





void setup()
{
  Serial.begin(38400);
  pinMode(SSerialTxControl, OUTPUT);  
  pinMode(GroundRE, OUTPUT);
  digitalWrite(GroundRE,LOW);
  /////module promux TC slave 32
  modbus_construct(&packets[PACKET1], 32, READ_INPUT_REGISTERS, 1, 8, 0);
//  modbus_construct(&packets[PACKET2], 32, READ_INPUT_REGISTERS, 2, 1, 1);
//  modbus_construct(&packets[PACKET3], 32, READ_INPUT_REGISTERS, 3, 1, 2);
//  modbus_construct(&packets[PACKET4], 32, READ_INPUT_REGISTERS, 4, 1, 3);
//  modbus_construct(&packets[PACKET5], 32, READ_INPUT_REGISTERS, 5, 1, 4);
//  modbus_construct(&packets[PACKET6], 32, READ_INPUT_REGISTERS, 6, 1, 5);
//  modbus_construct(&packets[PACKET7], 32, READ_INPUT_REGISTERS, 7, 1, 6);
//  modbus_construct(&packets[PACKET8], 32, READ_INPUT_REGISTERS, 8, 1, 7);
  /////module promux TC slave 33
  modbus_construct(&packets[PACKET9], 33, READ_INPUT_REGISTERS, 1, 8, 8);
//  modbus_construct(&packets[PACKET10], 33, READ_INPUT_REGISTERS, 2, 1, 9);
//  modbus_construct(&packets[PACKET11], 33, READ_INPUT_REGISTERS, 3, 1, 10);
//  modbus_construct(&packets[PACKET12], 33, READ_INPUT_REGISTERS, 4, 1, 11);
//  modbus_construct(&packets[PACKET13], 33, READ_INPUT_REGISTERS, 5, 1, 12);
//  modbus_construct(&packets[PACKET14], 33, READ_INPUT_REGISTERS, 6, 1, 13);
//  modbus_construct(&packets[PACKET15], 33, READ_INPUT_REGISTERS, 7, 1, 14);
//  modbus_construct(&packets[PACKET16], 33, READ_INPUT_REGISTERS, 8, 1, 15);
  /////module promux TC slave 34
  modbus_construct(&packets[PACKET17], 34, READ_INPUT_REGISTERS, 1, 8, 16);
//  modbus_construct(&packets[PACKET18], 34, READ_INPUT_REGISTERS, 2, 1, 17);
//  modbus_construct(&packets[PACKET19], 34, READ_INPUT_REGISTERS, 3, 1, 18);
//  modbus_construct(&packets[PACKET20], 34, READ_INPUT_REGISTERS, 4, 1, 19);
//  modbus_construct(&packets[PACKET21], 34, READ_INPUT_REGISTERS, 5, 1, 20);
//  modbus_construct(&packets[PACKET22], 34, READ_INPUT_REGISTERS, 6, 1, 21);
//  modbus_construct(&packets[PACKET23], 34, READ_INPUT_REGISTERS, 7, 1, 22);
//  modbus_construct(&packets[PACKET24], 34, READ_INPUT_REGISTERS, 8, 1, 23); 
  /////module promux TC slave 35
  modbus_construct(&packets[PACKET25], 35, READ_INPUT_REGISTERS, 1, 8, 24);
//  modbus_construct(&packets[PACKET26], 35, READ_INPUT_REGISTERS, 2, 1, 25);
//  modbus_construct(&packets[PACKET27], 35, READ_INPUT_REGISTERS, 3, 1, 26);
//  modbus_construct(&packets[PACKET28], 35, READ_INPUT_REGISTERS, 4, 1, 27);
//  modbus_construct(&packets[PACKET29], 35, READ_INPUT_REGISTERS, 5, 1, 28);
//  modbus_construct(&packets[PACKET30], 35, READ_INPUT_REGISTERS, 6, 1, 29);
//  modbus_construct(&packets[PACKET31], 35, READ_INPUT_REGISTERS, 7, 1, 30);
//  modbus_construct(&packets[PACKET32], 35, READ_INPUT_REGISTERS, 8, 1, 31);
  /////module promux DO slave 36
  modbus_construct(&packets[PACKET33], 36, PRESET_SINGLE_REGISTER, 2, 1, 32);  

  
  modbus_configure(&RS485Serial, baud, SERIAL_8N1, timeout, polling, retry_count, SSerialTxControl, packets, TOTAL_NO_OF_PACKETS, regs);
}

void loop()
{
  modbus_update();

Serial.print(regs[0]);
Serial.print("//");
Serial.print(regs[1]);
Serial.print("//");
Serial.print(regs[2]);
Serial.print("//");
Serial.print(regs[3]);
Serial.print("//");
Serial.print(regs[4]);
Serial.print("//");
Serial.print(regs[5]);
Serial.print("//");
Serial.print(regs[6]);
Serial.print("//");
Serial.println(regs[7]);
Serial.print("//");
Serial.print(regs[8]);
Serial.print("//");
Serial.print(regs[9]);
Serial.print("//");
Serial.print(regs[10]);
Serial.print("//");
Serial.print(regs[11]);
Serial.print("//");
Serial.print(regs[12]);
Serial.print("//");
Serial.print(regs[13]);
Serial.print("//");
Serial.print(regs[14]);
Serial.print("//");
Serial.print(regs[15]);
Serial.print("//");
Serial.print(regs[16]);
Serial.print("//");
Serial.print(regs[17]);
Serial.print("//");
Serial.print(regs[18]);
Serial.print("//");
Serial.print(regs[19]);
Serial.print("//");
Serial.print(regs[20]);
Serial.print("//");
Serial.print(regs[21]);
Serial.print("//");
Serial.print(regs[22]);
Serial.print("//");
Serial.print(regs[23]);
Serial.print("//");
Serial.print(regs[24]);
Serial.print("//");
Serial.print(regs[25]);
Serial.print("//");
Serial.print(regs[26]);
Serial.print("//");
Serial.print(regs[27]);
Serial.print("//");
Serial.print(regs[28]);
Serial.print("//");
Serial.print(regs[29]);
Serial.print("//");
Serial.print(regs[30]);
Serial.print("//");
Serial.println(regs[31]);
}

You can also download a free modbus slave simulator if you have a RS485 converter to PC.

benoit

gpl-3.0.txt (34.3 KB)

keywords.txt (407 Bytes)

SimpleModbusMasterSoftwareSerial.cpp (15.3 KB)

SimpleModbusMasterSoftwareSerial.h (5.99 KB)

Hi. I'm newbie. Sorry for my english.

I'm using Arduino Uno with 485 modules as master and slave.

I managed to talk to each other with the example of #350 ( Help with ModBus RTU Master-Slave: SimpleModbus [SOLVED] - #351 by jackrussel - Networking, Protocols, and Devices - Arduino Forum).
For simplicity, only use a package such that the master reads the status of the slave input 8. I had many problems to achieve communication, because the master did not get good answers, but I solved with large pull-up and pull-down on lines A and B bus.
It works when I connect pin 8 of the slave master LED lights. But when I disconnect pin 8 on the slave, the master initially off the led, but then back on 2 or 3 times, erratically, and finally leaves off, as it should be. What can be caused this?

From already thank you very much.

Cheers.

mig

Migue,
If you post your schematic and code here, then we can tell you exactly where is problem, but we cant tell you any improvement just from your problem, because it can be due to hardware or even because of software!
So please provide more details about it!
Yatin

Hello all,

I am Nike, working with an energy meter (Schneider Conzerv EM 6436), and trying to read the register values using modbus. I saw the post http://forum.arduino.cc/index.php?topic=176142.15 and went through the entire thread (took nearly 2 days).

I followed the schematic at https://aglasspool.files.wordpress.com/2010/03/diagram1rev3.jpeg. I modified the connections with necessary resistors and Arduino Yun is master and conzerv meter is slave.

I tried with the simpleModbusMaster example with necessary baud rate, slave ID and couldn't get any response. Please help me out. I am here by attaching the manual of energy meter.

Manual : http://www.powertechinternational.in/catalouges/SCHNEIDER-CONZERV-EM-6400.pdf

Thanks in advance
Nike

Hi Nikeroy,
Please post your exact code (with your changes) And also your schematic so we can check where the exact fault is and can help you further!
Thanks!

Hi Yatin,

I have attached both my code and the configuration schematic. Here Uno/Yun is master and Conzerv energy meter is slave. (Manual of meter Manual : http://www.powertechinternational.in/catalouges/SCHNEIDER-CONZERV-EM-6400.pdf)

I have changed the register addresses from 3900 to 43900 or 0x0F3C etc. But, couldn't succeed. I am using SimpleModbusMasterV12 (their *.cpp and *.h files are attached).

I am using both the schematics with and without capacitors, and also changing the resistors from 680 ohm to 510 ohm (according to JuanB's manual)

Let me know which part am I missing.

BR
Nike

Arduino_Yun_or_Uno.pdf (637 KB)

Arduino_Yun_or_Uno.txt (1.29 KB)

SimpleModbusMaster.cpp (14 KB)

SimpleModbusMaster.h (5.43 KB)

Hello all,

I am trying different techniques, like with and without resistors, with and without capacitors but no success.

Any inputs !!

--
Nike

Hi all,

Any inputs on my previous requests ?

--
Nike

Hi again,

new question: My slave sends a 32BitFloat Value. I've got Integer Values in 2 Registers. How to convert the 2 Integer Values to a 32BItFloat ?? Thanks for your Help!

Manu

Nike,
Looking at your code I notice something that I am surprised you haven't picked up if you read the whole thread, where it is mentioned on more than a few occasions that with a small Arduino, such as an UNO, there is only one hardware serial port to use.

From looking at your code, you are trying to use that one serial port for both Modbus and to do your serial prints.
Additionally, when you use a serial port for sending of data, you first need to initialise the port with a serial.begin(...), which you don't have in case you were wanting to use the serial port for serial.print(...).

But remember, if you are using this on an UNO, you can only connect the hardware serial port to only one device, either your computer, via USB or to the MAX-485 chip that then talks Modbus to the Modbus slave.

The other thing to test, once you get that sorted out, is to swap the IEA-485 lines, that is swap the A and B lines, as I recall there was mention that some MAX-485 chips had the pins swapped. You need to confirm and clarify this claim, though swapping those lines should cause no ill effects anyhow.


Paul

soundmanu, please look and read the documentation for SimpleModbus, I believe it is detailed clearly enough in there to do these sorts of things.


Paul