Estoy tratando de transmitir 4 valores analogicos de analogRead de un arduino a otro, por modbusRTU.
La comunicacion se realiza pero siempre me llega el valor 1023-
No se que hago mal, esta claro que es un error de sofware mio.
Alguien que sepa sobre modbus me puede ayudar?, realmente solo quiero pasar un total de 6 potenciometros y 4 entradas digitales, de un arduino a otro por modbus. Solo hay un master y un slave.
Os paso el codigo a ver en que me estoy equivocando
#include <ModbusRtu.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// array de datos que se comparten en la red modbus
uint16_t au16data[16];
uint8_t u8state;
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0,Serial,0); // this is master and RS-232 or USB-FTDI
/**
* This is an structe which contains a query to an slave device
*/
modbus_t telegram;
unsigned long u32wait;
void setup() {
Serial.begin( 19200 ); // baud-rate at 19200
// Inicializar el LCD con el número de columnas y filas del LCD
lcd.begin(40, 4);
master.start();
master.setTimeOut( 1000 ); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 500;
u8state = 0;
}
void loop() {
switch( u8state ) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
telegram.u8id = 1; // slave address
telegram.u8fct = 3; // function code (this one is registers read)
telegram.u16RegAdd = 0; // start address in slave
telegram.u16CoilsNo = 16; // number of elements (coils or registers) to read
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
master.query( telegram ); // send query (only once)
u8state++;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 100;
}
// recibe un mensaje
lcd.setCursor(0,0);
lcd.print("Pot1 ");
lcd.setCursor(5,0);
lcd.print(au16data[0]);
lcd.setCursor(10,0);
lcd.print("Pot2 ");
lcd.setCursor(15,0);
lcd.print(au16data[1]);
lcd.setCursor(0,1);
lcd.print("Pot3 ");
lcd.setCursor(5,1);
lcd.print(au16data[2]);
lcd.setCursor(10,1);
lcd.print("Pot4 ");
lcd.setCursor(15,1);
lcd.print(au16data[3]);
break;
}
}
EL codigo del otro arduino es este
#include <ModbusRtu.h>
// data array for modbus network sharing
uint16_t au16data[16];
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI
void setup() {
Serial.begin( 19200 ); // baud-rate at 19200
slave.start();
}
void loop() {
au16data[0] = analogRead(A0);
au16data[1]= analogRead(A1);
au16data[2]= analogRead(A2);
au16data[3]= analogRead(A3);
delay(50);
slave.poll( au16data, 16 );
}