Hola, tras probar con éxito la librería RS485 con vuestro módulo RS485/Modbus sobre dos Arduino uno mi objetivo es comunicar un Arduino maestro con otro esclavo. Estoy probando el ejemplo 4 y 8 de vuestra librería pero siempre obtengo el mensaje "communication error". El cableado es este:
https://www.dropbox.com/s/xkiw50bplq0yw ... 1.jpg?dl=0Y el código es prácticamente el mismo que vuestros ejemplos, sólo he cambiado el baudrate a 9600 en los dos arduino, la dirección del esclavo=1 y la dirección de lectura address=0. He borrado los registros en el esclavo dejando sólo uno. Y he cambiado el valor del registro uno para que sea el valor de un digitalread del pin4 que está conectado a gnd. El código es este.
Maestro:
Code:
#include <Wire.h>
// Cooking API libraries
#include <arduinoUtils.h>
// Include these libraries for using the RS-485 and Modbus functions
#include <arduinoRS485.h>
#include <ModbusMaster485.h>
#include <SPI.h>
// Instantiate ModbusMaster object as slave ID 1
ModbusMaster485 node(1);
// Define one addres for reading
#define address 0
// Define the number of bytes to read
#define bytesQty 2
void setup()
{
// Power on the USB for viewing data in the serial monitor
Serial.begin(115200);
delay(100);
// Initialize Modbus communication baud rate
// Only allowed in SOCKET1
node.begin(9600);
// Print hello message
Serial.println("Modbus communication over RS-485");
delay(100);
}
void loop()
{
// This variable will store the result of the communication
// result = 0 : no errors
// result = 1 : error occurred
int result = node.readHoldingRegisters(address, bytesQty);
if (result != 0) {
// If no response from the slave, print an error message
Serial.println("Communication error");
delay(1000);
}
else {
// If all OK
Serial.print("Read value : ");
// Print the read data from the slave
Serial.print(node.getResponseBuffer(0));
delay(1000);
}
Serial.print("\n");
delay(2000);
// Clear the response buffer
node.clearResponseBuffer();
}
Esclavo:
Code:
#include <Wire.h>
// Cooking API libraries
#include <arduinoUtils.h>
// Include these libraries for using the RS-485 and Modbus functions
#include <arduinoRS485.h>
#include <ModbusSlave485.h>
#include <SPI.h>
// Create new mbs instance
ModbusSlave485 mbs;
// Slave registers
enum {
MB_0, // Register 0
MB_REGS // Dummy register. using 0 offset to keep size of array
};
int regs[MB_REGS];
void setup() {
pinMode(4,INPUT);
// Power on the USB for viewing data in the serial monitor
Serial.begin(115200);
// Modbus slave configuration parameters
// SlaveId
const unsigned char SLAVE = 1;
// Baud rate
const long BAUD = 9600;
// Configure msb with config settings
// Only allowed in SOCKET1
mbs.configure(SLAVE, BAUD);
}
void loop()
{
// Pass current register values to mbs
mbs.update(regs, MB_REGS);
// Read all the analog Inputs, and store the values in
// the Modbus registers.
regs[MB_0] = digitalRead(4); // Read input dig pin4
delay(10);
}
Agradezco me den alguna pista para intentar depurar posibles errores porque ahora mismo no sé qué hacer...
Muchas gracias!