this is my code:
#include <SimpleModbusMaster.h>
#include <LiquidCrystal.h>
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate
// If the packets internal retry register matches
// the set retry count then communication is stopped
// on that packet. To re-enable the packet you must
// set the "connection" variable to true.
#define retry_count 10
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2
//#define LED 9
#define LED 3
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,
// PACKET2,
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// Create a packetPointer to access each packet
// individually. This is not required you can access
// the array explicitly. E.g. packets[PACKET1].id = 2;
// This does become tedious though...
packetPointer packet1 = &packets[PACKET1];
//packetPointer packet2 = &packets[PACKET2];
// Data read from the arduino slave will be stored in this array
// if the array is initialized to the packet.
unsigned int readRegs[1];
// Data to be written to the arduino slave
//unsigned int writeRegs[1];
LiquidCrystal lcd(12, 11, 9, 8, 7, 6); // LCD configuration
void setup()
{
// read 1 register starting at address 0
// 0x13 is the address of data, 2 is the word, the word i 16 bit, this value i read from manual of Ducati Energia
modbus_construct(packet1, 1, READ_HOLDING_REGISTERS, 0x13, 2, readRegs);
// configuration
modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
lcd.print("requests:");
lcd.print(packet1->requests);
lcd.print("successful_requests: ");
lcd.print(packet1->successful_requests);
lcd.print("failed_requests: ");
lcd.print(packet1->failed_requests);
lcd.print("exception_errors: ");
lcd.print(packet1->exception_errors);
lcd.print("Active energy: ");
lcd.print(readRegs[0]);
delay(2000);
}