Noob needs help with Modbus communication

no.

HardwareSerial &mySerial = Serial3;

means you can either use mySerial or Serial3 in the code.
It has nothing to do with SoftSerial because the lines for SoftSerial are commented! (!!!)

it doesn't matter that HardwareSerial isn't red ... press on compile and you will see that the code will be accepted.

it doesn't matter that HardwareSerial isn't red ... press on compile and you will see that the code will be accepted.

Ok.

means you can either use mySerial or Serial3 in the code.

That's what I meant to ask XD sorry for misunderstanding :sweat_smile:

Will compile now then :+1:

-Einar

Noiasca,

So the compilation worked and I believe my wiring is all correct, however I am not getting the result I was "expecting".
What I thought would happen was this: that the temp data would be stored in register DI2 and then my LCD would print the data from that register.
Here is my modification of your original example:

#include <LiquidCrystal.h>
#include <DHT.h>

int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);  

#define Type DHT11
int sensePin=2;
DHT HT(sensePin,Type);
float tempC;

// On a Mega you can simply use 
// a Reference to an existing HW Serial:
// HardwareSerial &mySerial = Serial3;

// If you don't need debugging output, you can use
// a reference to Serial
// HardwareSerial &mySerial = Serial;

HardwareSerial &mySerial = Serial1;

constexpr byte modbus_format = SERIAL_8N1;   // leave it on SERIAL_8N1 for SoftSerial
constexpr uint32_t modbus_baud = 19200;      // use only slow speeds with SoftSerial
constexpr byte modbus_enable_pin = 4;       // The GPIO used to control the MAX485 TX pin. Set to 255 if not needed (i.e. a selfsensing module or RS232).

/* **************************************************** *            
 * Modbus 
 * **************************************************** */    

#include <NoiascaModbusServerSimple.h>            // Modbus Slave library (low level RS484 communication)
#include <NoiascaModbusCommon.h>                 // predefined Modbus registers for the library examples
constexpr byte modbus_slave_id = 2;              // Modbus address of this slave

// FC3/FC16 holding registers - not used in this example
enum
{
  // just add or remove registers and your good to go...
  // The first register starts at address 0 which is for Function 3 Register 40001
  DI2,               // a digitlRead of a GPIO
  DO13,              // a digitalWrite to GPIO
  ADC0,              // analogRead
  ADC1,              // analogRead
  ADC2,              // analogRead
  ADC3,              // analogRead
  HOLDING_REGS_SIZE  // leave this one
};
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // FC3/FC16 register array

ModbusServer modbusServer(modbus_slave_id, mySerial);

/*
   update the holding register (read the pins) 
   and update the pins according to 
   this function should be called over and over again in loop  
*/
void updateRegisters()
{
  // write new values to GPIO
  digitalWrite(13, holdingRegs[DO13]);
  // update data to be read by the master
  holdingRegs[DI2]  = HT.readTemperature();
  holdingRegs[ADC0] = analogRead(A0);
  holdingRegs[ADC1] = analogRead(A1);
  holdingRegs[ADC2] = analogRead(A2);
  holdingRegs[ADC3] = analogRead(A3);
}

/*
   if you need to control the transmit pin manually
   this callback will be called before the transmission starts  
*/
void cbPreTransmission()
{
  digitalWrite(modbus_enable_pin, HIGH);
}

/*
   if you need to control the transmit pin manually
   this callback will be called after the transmission has ended  
*/
void cbPostTransmission()
{
  digitalWrite(modbus_enable_pin, LOW);
}

/*
   this functions can be called,
   when a Modbus Client writes a value with FC6 or several values with FC16
   This example only shows a debug messages to indicate Master activity.   
*/
void cbRegisterWritten(const uint16_t reg, const uint16_t value)
{
  Serial.print(F("write register:")); Serial.print(reg); Serial.print(F(" value:")); Serial.println(value);
}

/*
   this functions can be called,
   when a Modbus Client read values from this Modbus Server (FC3).
   If you don't need the debug messages don't set the callback.
   This example only shows a debug messages to indicate Master activity.
*/
void cbRegisterRead(const uint16_t reg, const uint16_t count)
{
  Serial.print(F("read from start register:")); Serial.print(reg); Serial.print(F(" count:")); Serial.println(count);
}

/* **************************************************** *            
 * Arduino default setup and loop
 * **************************************************** */   
void setup()
{
  lcd.begin(16,2);
  HT.begin();
  delay(100);
  
  Serial.begin(115200);                       // only used for debugging
  Serial.println(F("Modbus LCD Slave OOP"));  // comment this lines when you use the hardware serial for Modbus
  Serial.flush();
  
  mySerial.begin(modbus_baud);              // start the Serial for Modbus
  
  modbusServer.begin(&mySerial, modbus_baud, modbus_format, modbus_slave_id, HOLDING_REGS_SIZE, holdingRegs);
  pinMode(modbus_enable_pin, OUTPUT);        // if you need a TX enable pin, set it to Output
  modbusServer.setPreTransmission(cbPreTransmission);
  modbusServer.setPostTransmission(cbPostTransmission);
  modbusServer.setRegisterWritten(cbRegisterWritten);
  modbusServer.setRegisterRead(cbRegisterRead);
  // init your local hardware
  pinMode(13, OUTPUT);
}

void loop()
{
  modbusServer.update();         // call modbus in loop
  updateRegisters();             // handle GPIOs (outputs like relais or LEDs and Inputs)
  // do other things non blocking in loop

  lcd.setCursor(0,0);
  lcd.print(DI2);
  delay(1000);
  lcd.clear();
}

I have followed the wiring with the LCD and the DHT sensor like this:
https://toptechboy.com/arduino-tutorial-50-how-to-connect-and-use-the-dht11-temperature-and-humidity-sensor/
https://toptechboy.com/arduino-tutorial-48-connecting-and-using-an-lcd-display/

currently the LCD is displaying nothing (it is ON and working though!)

-Einar

first, please tell:
Did the HelloWorld Example from Modbus Slave Library work like expected?
Did the HelloWorld Example react on all provided registers / addresses?
Is your Modbus working?

And then ask yourself:
is the DHT working?
does the example sketch of DHT Sensor work?
is the LCD working?
does the example sketch of LCD work?

put a lcd.print("end setup"); at the end in your setup

does it get printed?

If you can answer all questions with a clear yes, I can give one hint,
analyze this line

what should this line print to the lcd?
What is the content of DI2?
What do you expect there instead?

Hi Noiasca,

I am certain of the following:

  • LCD works.

  • DHT works.

  • Modbus RS485 connection is established.

I have NOT tested the HelloWorld example from the library. In my mind I was adding my own LCD and temp reader in order to test it.

How do I test it as it is (without editing it)?

what should this line print to the lcd?
What is the content of DI2?
What do you expect there instead?

My expectation of the line lcd.print(DI2); was this:
DHT reads temperature --> DHT sends temperature data to the DI2 "digital in" register --> LCD prints the data from the DI2 register.

Is this understanding not correct?

-Einar

I thought the content of DI2 was the incoming temperature data in a 16 bit format.
That the temp number would be stored "directly" in the register.

well, 1 in the enumeration.
But I thought the enumeration was created as reference chart and that DI2 was coded to refer to a holding register.

My bad, DI2 has a value of 0.

How I understood the code was this:

  1. The enum creates a reference table for the future registers where DI2 = 0, DO13 = 1, ... , ADC3 = 5.

  2. This code

void updateRegisters()
{
  // write new values to GPIO
  digitalWrite(13, holdingRegs[DO13]);
  // update data to be read by the master
  holdingRegs[DI2]  = HT.readTemperature();
  holdingRegs[ADC0] = analogRead(A0);
  holdingRegs[ADC1] = analogRead(A1);
  holdingRegs[ADC2] = analogRead(A2);
  holdingRegs[ADC3] = analogRead(A3);
}

establishes what measurment each register is going to make.

  1. This line
modbusServer.begin(&mySerial, modbus_baud, modbus_format, modbus_slave_id, HOLDING_REGS_SIZE, holdingRegs);

Actually begins the slave and activates/starts up the registers.

Please correct me if this understanding in not correct and also please explain how I am to correctly understand the creation of registers and register data in this code (?).

-Einar

Hi Noiasca,

I also know now that the basic/standard HelloWorld example is working.
I can send the following through QModBus
image
and Serial monitor in IDE says this:
image

-Einar

OK, you got it. DI2 contains just the 0.

now back to this:

this will print a 0 on the lcd.
but I assume you want to print the content of the first holdingRegs --> DI2.
therefore you would need something like

lcd.print(holdingRegs[DI2]);

Noiasca,
It is working!
Now the LCD displays 17. Serial also prints 17 when I do

Serial.println(holdingRegs[DI2]);
delay(1000);

How would I write to this holding register with QModBus to see if I can change is succesfully?

-Einar

adopt

```
void cbRegisterWritten(const uint16_t reg, const uint16_t value)
{
  Serial.print(F("write register:")); Serial.print(reg); Serial.print(F(" value:")); Serial.println(value);
}
```

to write the value into holdingReg[DI12] if reg == DI12.

remove

holdingRegs[DI2]  = HT.readTemperature();

send a write holding register to register 0 with your software.

Hi Noiasca,
I spoke with a person at a company I know who is familiar with Modbus. He recommended I use the library ModbusRtu since it can make both a slave and master and is quite simple to use. So I have switched to that library and learned the functions I need.
However the help you have given me has been very valuable and made me understand more about coding in general and Modbus, and has also made it much easier for me to understand the other library.
Thank you for your help & instructions :slight_smile:

-Einar