Go Down

Topic: Problem with ModBus 485 SimpleModbus Lib. V1.4 (SOLVED) (Read 1 time) previous topic - next topic

cueli

Dec 30, 2012, 01:22 pm Last Edit: Jan 05, 2013, 01:08 am by cueli Reason: 1
In summary, I can not get to work writing the slaves.

Reading, OK .. The Master, which received slaves shipped, (Function READ_HOLDING_REGISTERS) without problems... ; but in the other direction, no .. , Communication writing to slaves, always fails .. (function: 0x16 PRESET_MULTIPLE_REGISTERS)

IDE 1.0.2 y 1.0.3
Mega 2560
Modulo 485 de Sparfun
http://code.google.com/p/simple-modbus/
http://code.google.com/p/simple-modbus/downloads/list

MAESTRO
Code: [Select]
#include <SimpleModbusMaster1.h>

// led to indicate that a communication error is present
#define connection_error_led 13

//////////////////// Port information ///////////////////
#define baud 57600
#define timeout 1000
#define polling 200 // the scan rate


#define retry_count 3

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


enum
{
 PACKET1,
 PACKET2,
 // leave this last entry
 TOTAL_NO_OF_PACKETS
};

// Create an array of Packets for modbus_update()
Packet packets[TOTAL_NO_OF_PACKETS];

// The data from the PLC will be stored
// in the regs array
unsigned int regs[9];

void setup()
{
 Serial.begin (115200);
 
 // read 3 registers starting at address 0
 packets[PACKET1].id = 2;
 packets[PACKET1].function = READ_HOLDING_REGISTERS;
 packets[PACKET1].address = 0;
 packets[PACKET1].no_of_registers = 3;
 packets[PACKET1].register_array = regs;
 
 // write the 2 registers to the PLC starting at address 3
 packets[PACKET2].id = 2;
 packets[PACKET2].function = PRESET_MULTIPLE_REGISTERS;
 packets[PACKET2].address = 3;
 packets[PACKET2].no_of_registers = 6;
 packets[PACKET2].register_array = regs;
 
 // P.S. the register_array entries above can be different arrays
 
 // Initialize communication settings etc...
 modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
 
 pinMode(connection_error_led, OUTPUT);
}

void loop()
{
 unsigned int connection_status = modbus_update(packets);
 
 if (connection_status != TOTAL_NO_OF_PACKETS)
 {
   digitalWrite(connection_error_led, HIGH);
   // You could re-enable the connection by:
   packets[connection_status].connection = true;
   delay (50);
   
 }
 else
   digitalWrite(connection_error_led, LOW);
 
 
 // LO QUE LLEGA
 Serial.print (regs[0]);  Serial.print (" ");
 Serial.print (regs[1]);  Serial.print (" ");
 Serial.print (regs[2]);
 
 Serial.print (" - ");
 
 Serial.print (packets[PACKET1].requests);  Serial.print (" ");
 Serial.print (packets[PACKET1].successful_requests);  Serial.print (" ");
 Serial.print (packets[PACKET1].incorrect_id_returned);  Serial.print (" ");
 Serial.print (packets[PACKET1].incorrect_function_returned);  Serial.print (" ");
 Serial.print (packets[PACKET1].incorrect_bytes_returned);  Serial.print (" ");
 Serial.print (packets[PACKET1].checksum_failed);  Serial.print (" ");
 Serial.print (packets[PACKET1].buffer_errors);  Serial.print (" ");
 Serial.print (packets[PACKET1].illegal_function);  Serial.print (" ");
 Serial.print (packets[PACKET1].illegal_data_address);  Serial.print (" ");
 Serial.print (packets[PACKET1].illegal_data_value);  Serial.print (" ");
 Serial.print (packets[PACKET1].misc_exceptions);  Serial.print (" ");
 Serial.print (packets[PACKET1].total_errors);
 
 Serial.print (" // ");

 // LO QUE ENVIO AL EXCLAVO
 
 regs[3] = 30;
 regs[4] = 40;
 regs[5] = 50;
 regs[6] = 60;  
 regs[7] = 70;
 regs[8] = 80;  
 
 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.print (regs[7]);  Serial.print (" ");
 Serial.print (regs[8]);
 
 Serial.print (" - ");

 Serial.print (packets[PACKET2].requests);  Serial.print (" ");
 Serial.print (packets[PACKET2].successful_requests);  Serial.print (" ");
 Serial.print (packets[PACKET2].incorrect_id_returned);  Serial.print (" ");
 Serial.print (packets[PACKET2].incorrect_function_returned);  Serial.print (" ");
 Serial.print (packets[PACKET2].incorrect_bytes_returned);  Serial.print (" ");
 Serial.print (packets[PACKET2].checksum_failed);  Serial.print (" ");
 Serial.print (packets[PACKET2].buffer_errors);  Serial.print (" ");
 Serial.print (packets[PACKET2].illegal_function);  Serial.print (" ");
 Serial.print (packets[PACKET2].illegal_data_address);  Serial.print (" ");
 Serial.print (packets[PACKET2].illegal_data_value);  Serial.print (" ");
 Serial.print (packets[PACKET2].misc_exceptions);  Serial.print (" ");
 Serial.print (packets[PACKET2].total_errors);  Serial.println (" ");

 delay (100);
 
}


ESCLAVO
Code: [Select]
#include <SimpleModbusSlave1.h>


//////////////// registers of your slave ///////////////////
enum
{    
 // just add or remove registers and your good to go...
 // The first register starts at address 0
 OUT1,    
 OUT2,        
 OUT3,
 

 REC1,
 REC2,
 REC3,
 REC4,
 REC5,
 REC6,
 
 TOTAL_ERRORS,
 // leave this one
 TOTAL_REGS_SIZE
 // total number of registers for function 3 and 16 share the same register array
};

unsigned int holdingRegs[TOTAL_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////

void setup()
{
 Serial.begin (115200);
 
 
 /* parameters(long baudrate,
               unsigned char ID,
               unsigned char transmit enable pin,
               unsigned int holding registers size)
               
    The transmit enable pin is used in half duplex communication to activate a MAX485 or similar
    to deactivate this mode use any value < 2 because 0 & 1 is reserved for Rx & Tx
 */
 
 modbus_configure(57600, 2, 23, TOTAL_REGS_SIZE);

}

void loop()
{
 
 

 holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs);
 for (byte i = 0; i < 3; i++)
 {
   holdingRegs[i] = analogRead(i+8);
   delayMicroseconds(500);    
 }


 // LO QUE ENVIA EL EXCLAVO
 Serial.print (holdingRegs[OUT1]);  Serial.print (" ");
 Serial.print (holdingRegs[OUT2]);  Serial.print (" ");
 Serial.print (holdingRegs[OUT3]);
 
 Serial.print ("  //  ");

 // RECIBE EL EXCLAVO
 
 Serial.print (holdingRegs[REC1]);  Serial.print (" ");
 Serial.print (holdingRegs[REC2]);  Serial.print (" ");
 Serial.print (holdingRegs[REC3]);  Serial.print (" ");
 Serial.print (holdingRegs[REC4]);  Serial.print (" ");
 Serial.print (holdingRegs[REC5]);  Serial.print (" ");
 Serial.print (holdingRegs[REC6]);
 
 Serial.print (" - ");

 Serial.println (holdingRegs[TOTAL_ERRORS]);
 
 delay (100);
}



The lib in the sketch, are edited to work with the serial 1

Any suggestions of what could be the mistake I'm making?

thank you very much
Greetings


cueli

I answer myself ...

When the bus is free, at times it does not transmit anyone can produce erroneous readings due to noise in the pair
To avoid this, it puts a pull-up resistance in the A and a pull-down, in the B, thus, a potential is fixed to the case that all release the bus.

Interestingly, only affected the transmission, that appeared a "1" ghost, which falsified the total data received ..

http://en.wikipedia.org/wiki/File:Rs485-bias-termination.svg
http://en.wikipedia.org/wiki/RS-485
http://www.maximintegrated.com/app-notes/index.mvp/id/763

regards

Go Up