Salve a tutti, sto lavorando da svariati giorni a questo script con il mio tutor dello stage. Il problema che vi pongo é il seguente. Questo script mi fa stampare un solo dato per volta, e per cambiarlo devo per forza di cose cambiare l`adress della funzione.
Ora io vorrei impostare vari adress delal funzione (e relativi bit) in modo che sullo schermo mi vengano stampati in contemporanea i valori relativi a quella funzione. Ecco come posso fare?
#include <SimpleModbusMaster.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
//////////////////// Port information ///////////////////
#define baud 19200
#define timeout 1000
#define polling 100
#define retry_count 10
#define TxEnablePin 2
#define TOTAL_NO_OF_REGISTERS 4
float x;
int conta = 0;
//////////////////// Assembly of the Modbus packets ///////////////
enum
{
PACKET1, // Energy acumulator
// PACKET2,
// PACKET3,
TOTAL_NO_OF_PACKETS // Leave this last entry. It tells the library how many packets are to be sent out.
};
Packet packets[TOTAL_NO_OF_PACKETS]; //Leave this line in here.
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
Packet* statusWordVSD = &packets[PACKET1];
////////VFD READ VARIABLES///////////
unsigned int readStatusWordVSD = 0;
unsigned int readRegs[4];
void setup()
{
// Serial.begin(9600); // Start the Arduino IDE terminal in case we need it for debugging.
modbus_construct(statusWordVSD, 10, READ_HOLDING_REGISTERS, 3204, 4, readStatusWordVSD);
modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
}
void loop()
{
conta++;
modbus_update();
if (conta >= 1000) {
lcd.setCursor(0,0);
lcd.print("Value: ");
lcd.setCursor(8,0);
// Read 1 register (int16)
// lcd.print(regs[0]);
// Read Float (2 registers)
// lcd.print(decodeFloat(regs));
// Read Int64 (4 registers)
lcd.print(decodeInt64(regs), DEC);
conta = 0;
}
}
union Pun {float f; uint32_t u;};
void encodeFloat(uint16_t *regs, float x)
{
union Pun pun;
pun.f = x;
regs[0] = (pun.u >> 16) & 0xFFFFU;
regs[1] = pun.u & 0xFFFFU;
}
float decodeFloat(const uint16_t *regs)
{
union Pun pun;
pun.u = ((uint32_t)regs[0] << 16) | regs[1];
return pun.f;
}
unsigned long decodeInt64(const uint16_t *regs) {
unsigned long temp;
temp = ((unsigned long) regs[0] << 48) | (unsigned long) regs[1] << 32 | (unsigned long) regs[2] << 16 | (unsigned long) regs[3];
return temp;
}