Arduino Nano Every ModBus communication

Hello everybody,

I have made a Modbus connection to a Siemens CPU1212C DC / DC / DC with an Arduino Pro Mini and the following UART-TTL to RS485 converter and I can send and receive the desired registers with the following program.

Because the Arduino Pro Mini is a phase-out product, I would therefore like to use an Arduino Nano Every instead of the Arduino Pro mini.
I've been looking for a solution for weeks, but I can't connect to the Arduino Nano Every. Does anyone have an idea what could be the cause or does someone know a functional Modbus library that I can use?

I would be really grateful for help.
Thank you in advance.

#include <ModbusRtu.h>
#include <Wire.h>
//#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C  lcd(0x27, 16,4);


//Ein- und Ausgänge festlegen
#define  iLuftMinus  5
#define  iLuftPlus  6
#define  iMaterialMinus 7 
#define  iMaterialPlus  8



// data array for modbus network sharing
uint16_t Register[20] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
};


//Datenbereiche festlegen
uint16_t intSollLuft = 30;        //Sollwert Luftstrom
uint16_t intSollMaterial = 30;    //Sollwert Materialstrom


bool bFLK_Luft = false;           //Flankenmerker Luftstrom Plus / Minus
bool bFLK_Material = false;       //Flankenmerker Materialstrom Plus / Minus



/*
    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 slave(1, 0, 0); // this is slave @1 and RS-485



// Einmaliger Programmablauf
void setup() {

  //Bautrate festlegen
  slave.begin( 19200 );


  //Deklaration der Ein- und Ausgänge
  pinMode(iLuftPlus, INPUT);
  pinMode(iLuftMinus, INPUT);
  pinMode(iMaterialPlus, INPUT);
  pinMode(iMaterialMinus, INPUT);



  //Aktiviere interne Pull UP Widerstände
  digitalWrite(iLuftPlus, HIGH);
  digitalWrite(iLuftMinus, HIGH);
  digitalWrite(iMaterialPlus, HIGH);
  digitalWrite(iMaterialMinus, HIGH);

  // Aktiviere LCD
   lcd.init(); 
   lcd.backlight();
}

//////////////////////////////////////////
// Ablaufprogramm
//////////////////////////////////////////
void loop() {

  //Modbus kommunikation
  slave.poll( Register, 30 );

  /////////////////////////////////////
  //Luftstromeinstellung
  /////////////////////////////////////
  //Luft Plus
  if (digitalRead(iLuftPlus) == LOW and bFLK_Luft)
  { intSollLuft = intSollLuft + 5;
  }


  //Luft Minus
  if (digitalRead(iLuftMinus) == LOW and bFLK_Luft)
  { intSollLuft = intSollLuft - 5;
  }

  //Luft Flankenmerker
  if (digitalRead(iLuftPlus) and digitalRead(iLuftMinus))
  { bFLK_Luft = 1;
  }
  else
  { bFLK_Luft = 0;
  }


  //Maximalwert Luftstrom festlegen
   
  if (not Register[7] and intSollLuft > 100)
  { intSollLuft = 100;
  }
else if 
(Register[7] and intSollLuft > 60)
  { intSollLuft = 60;
  }

  //Minimalwert Luftstrom festlegen
  //darf nicht bei "0" sein da dei anzeige im Display falsch angezeigt wird
  if (intSollLuft < 20)
  { intSollLuft = 20;
  }



  /////////////////////////////////////
  //Materialstromeinstellung
  /////////////////////////////////////
  //Material Plus
  if (digitalRead(iMaterialPlus) == LOW and bFLK_Material)
  { intSollMaterial = intSollMaterial + 5;
  }


  //Material Minus
  if (digitalRead(iMaterialMinus) == LOW and bFLK_Material)
  { intSollMaterial = intSollMaterial - 5;
  }

  //Material Flankenmerker
  if (digitalRead(iMaterialPlus) and digitalRead(iMaterialMinus))
  { bFLK_Material = 1;
  }
  else
  { bFLK_Material = 0;
  }


  //Maximalwert Materialstrom festlegen
  if (intSollMaterial > 100)
  { intSollMaterial = 100;
  }

  //Minimalwert Materialstrom festlegen
  //darf nicht bei "0" sein da dei anzeige im Display falsch angezeigt wird
  if (intSollMaterial < 20)
  { intSollMaterial = 20;
  }
  

 ///////////////////////////////////////////////////
 // Ausgaben an Display
 ///////////////////////////////////////////////////
    // LCD ausgabe
    lcd.setCursor(0, 0);
    lcd.print("Luft    :");
    lcd.setCursor(12, 0);
    lcd.print("%");

    lcd.setCursor(0, 1);
    lcd.print("Material:");
    lcd.setCursor(12, 1);
    lcd.print("%");



    //Ausgabe Sollwert Luftstrom
    if (intSollLuft == 100) {
      lcd.setCursor(9, 0);
      lcd.print(intSollLuft);
    }

    if (intSollLuft < 100) {
      lcd.setCursor(10, 0);
      lcd.print(intSollLuft);
      lcd.setCursor(9, 0);
      lcd.print(" ");
    }

    //Ausgabe Sollwert Materialstrom
    if (intSollMaterial == 100) {
      lcd.setCursor(9, 1);
      lcd.print(intSollMaterial);
    }

    if (intSollMaterial < 100) {
      lcd.setCursor(10, 1);
      lcd.print(intSollMaterial);
      lcd.setCursor(9, 1);
      lcd.print(" ");
    }


  // Slave Registerdaten zuweisen
  Register[18] = 0;
  Register[19] = intSollLuft;
  Register[20] = intSollMaterial;

}

use hardware Serial1 on Rx/TX instead of SoftwareSerial

Thank you for your reply
I tried to program this through the Hardware Serial1. However, I didn't get it to work. Can you explain to me how I have to program this?

I know that there is no SoftwareSerial library for Nano Every. I don't see how your sketch uses the SoftwareSerial because you don't know what modbus library you use.

I use the following library.

and what is
Modbus slave(1, 0, 0);
then?

it doesn't match constructor
Modbus(uint8_t u8id, Stream& port, uint8_t u8txenpin =0);

second parameter should be Serial1
and don't forget Serial1.begin

1 Like

Thank you for your help

I have now the second parameter on Serial1 and have Serial1.begin instead of slave.begin. Now the communication works.

Thank you very much

1 Like