Well, i was trying to use the library <ModbusRtu.h>
#include <ModbusRtu.h>
uint16_t au16data[16]; //!< data array for modbus network sharing
uint8_t u8state; //!< machine state
uint8_t u8query; //!< pointer to message query
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0,0,2);
modbus_t telegram[2];
unsigned long u32wait;
void setup() {
// telegram 0: read registers
telegram[0].u8id = 1; // slave address
telegram[0].u8fct = 3; // function code (this one is registers read)
telegram[0].u16RegAdd = 1020; // start address in slave
telegram[0].u16CoilsNo = 1; // number of elements (coils or registers) to read
telegram[0].au16reg = au16data; // pointer to a memory array in the Arduino
master.begin( 9600 ); // baud-rate at 19200
master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
u32wait = millis() + 1000;
u8state = u8query = 0;
}
void loop() {
switch( u8state ) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
master.query( telegram[u8query] ); // send query (only once)
u8state++;
u8query++;
if (u8query > 2) u8query = 0;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 1000;
}
break;
}
Serial1.println(au16data[16],DEC);
}
but i was only getting some strange symbols in the serial monitor with or withouth the slave device connected, then i decide to use the same library than you, because this is the biggest post with a lot of information that i have found, but I'm getting this errors:
Arduino: 1.6.6 Hourly Build 2015/11/03 04:46 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
elmasbueno:16: error: 'packetPointer' does not name a type
packetPointer packet1 = &packets[PACKET1];
^
C:\Users\Casa\Documents\Arduino\elmasbueno\elmasbueno.ino: In function 'void setup()':
elmasbueno:22: error: 'packet1' was not declared in this scope
modbus_construct(packet1, 1, READ_HOLDING_REGISTERS, 1008, 1, readRegs);
^
elmasbueno:24: error: too few arguments to function 'void modbus_configure(HardwareSerial*, long int, unsigned char, long int, long int, unsigned char, unsigned char, Packet*, unsigned int, unsigned int*)'
modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
^
In file included from C:\Users\Casa\Documents\Arduino\elmasbueno\elmasbueno.ino:1:0:
C:\Users\Casa\Documents\Arduino\libraries\SimpleModbusMasterV2rev2/SimpleModbusMaster.h:134:6: note: declared here
void modbus_configure(HardwareSerial* SerialPort,
^
C:\Users\Casa\Documents\Arduino\elmasbueno\elmasbueno.ino: In function 'void loop()':
elmasbueno:33: error: 'writeRegs' was not declared in this scope
writeRegs[0] = analogRead(A0); // update data to be written to arduino slave
^
exit status 1
'packetPointer' does not name a type
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.