Ciao a tutti!
Dopo aver utilizzato arduino come slave volevo provare ad utilizzarlo come master per interrogare un sensore modbus rtu ma non riesco a farlo funzionare
cercando su internet ho trovato questa libreria: Arduino Playground - ModbusMaster Library
Dall'esempio ho tratto questo codice:
#include <ModbusMaster.h>
// instantiate ModbusMaster object as slave ID 2
// defaults to serial port 0 since no port was specified
ModbusMaster node(5); //ho messo il 5 perchè il sensore ha questo indirizzo
void setup()
{
// initialize Modbus communication baud rate
node.begin(19200);
}
void loop()
{
static uint32_t i;
uint8_t j, result;
uint16_t data[6];
i++;
// set word 0 of TX buffer to least-significant word of counter (bits 15..0)
node.setTransmitBuffer(0, lowWord(i));
// set word 1 of TX buffer to most-significant word of counter (bits 31..16)
// node.setTransmitBuffer(1, highWord(i));
// slave: write TX buffer to (2) 16-bit registers starting at register 0 QUESTA PARTE L'HO ELIMINATA
//result = node.writeMultipleRegisters(0, 2); PERCHE' NON HO BISOGNO DI SCRIVERE
// slave: read (6) 16-bit registers starting at register 2 to RX buffer (2, 6);
result = node.readHoldingRegisters(1000, 1); (nell'indirizzo 1000 ho la temperatura rilevata dal sensore)
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 6; j++)
{
data[j] = node.getResponseBuffer(j);
}
}
}
Quello che non riesco a capire (oltre a dove sbaglio ) è
-
dove devo specificare il piedino di abilitazione per abilitare l'invio dati? (come per lo slave)
-
perchè dividiamo i primi 16bit?
// set word 0 of TX buffer to least-significant word of counter (bits 15..0)
node.setTransmitBuffer(0, lowWord(i));
// set word 1 of TX buffer to most-significant word of counter (bits 31..16)
// node.setTransmitBuffer(1, highWord(i));
- perchè abbiamo la variabile i che aumenta sempre senza essere mai azzerata?
Grazie mille!!