Coder Social home page Coder Social logo

Frage about arduino-tools40 HOT 5 CLOSED

industrialshields avatar industrialshields commented on June 30, 2024
Frage

from arduino-tools40.

Comments (5)

jordicasals avatar jordicasals commented on June 30, 2024

Dear @Martin1683
could you please write it in english?
Thank you,

from arduino-tools40.

grphine avatar grphine commented on June 30, 2024

I believe the issue Martin is having is the same as my own:
There doesn't seem to be a clear way to request multiple disconnected registers.
In martin's example, from two different modbus slaves on the same physical device.
For myself, I'm after multiple disconnected ranges of input and holding registers.

The issue is that, as the read function is non-blocking, there's no defined way to hold it until it's finished reading before commencing the next read.
To illustrate, I may need the following 10 registers.

master.readInputRegisters(slave,1,100,5)
master.readHoldingRegisters(slave,1,100,5)

The second and third will fail as the process is held by the first.

I've tried putting the response processing after the read returns, however this causes the read to fail.

if (master.readInputRegisters(slave, 1, 99, 5)) {
  ModbusResponse response = master.available();
  ...
}
if(master.readHoldingRegisters...

I've also tried holding until processing finishes

ModbusResponse response = master.available();
while(!response);

but this, unsurprisingly, locks up the process so the program freezes.

Perhaps I'm missing something obvious, so please enlighten me if I am.

from arduino-tools40.

Martin1683 avatar Martin1683 commented on June 30, 2024

Hello everyone,

unfortunately I don't have time to write a great novel. I was able to solve the problems for my above question. Just look at my sketch:`

#include <Ethernet.h>
#include <ModbusTCPMaster.h>

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// Ethernet configuration values
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
uint8_t ip[] = { 192, 168, 178, 65 };
uint8_t slaveIp[] = { 192, 168, 178, 54 };
uint16_t slavePort = 502;

// Define the ModbusTCPMaster object
ModbusTCPMaster master1;
ModbusTCPMaster master4;
ModbusTCPMaster master5;

// Ethernet client object used to connect to the slave
EthernetClient slave;

uint32_t lastSentTime1 = 0UL;
uint32_t lastSentTime2 = 0UL;
uint32_t lastSentTime4 = 0UL;
uint32_t lastSentTime5 = 0UL;

int L1;
int L2;
int L3;
int ACgesamt;
int SOC;
int PVpower;
int diffACPV;

//Pins
int Pumpe = 7;
int Pumpe_led = 30;
int Heizstab1 = 5;
int Heizstab1_led = 31;
int Heizstab2 = 6;
int Heizstab2_led = 32;
int S1 = 2;
int S1_led = 33;
int S2 = 3;
int S2_led = 34;
int PWM = 9;
int Ventil = 8;

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {

lcd1.begin(20,4);

pinMode(Pumpe, OUTPUT);
pinMode(Heizstab1, OUTPUT);
pinMode(Heizstab1_led, OUTPUT);
pinMode(Heizstab2, OUTPUT);
pinMode(Heizstab2_led, OUTPUT);
pinMode(S1, INPUT_PULLUP);
pinMode(S1_led, OUTPUT);
pinMode(S2, INPUT_PULLUP);
pinMode(S2_led, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(Ventil, OUTPUT);

Serial.begin(9600UL);

// Begin Ethernet
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());

}

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

if (!slave.connected()) {
slave.stop();
slave.connect(slaveIp, slavePort);
}

//----------------------------AC Verbrauch L1 L2 L3--------------------------------------------------------------------------------------------------
if (slave.connected()) {

if (millis() - lastSentTime1 > 1700) {



  if (!master1.readInputRegisters(slave, 100, 817, 3)) {
    Serial.print("ReadFehler 1");
   }

        lastSentTime1 = millis();}
  
if (master1.isWaitingResponse()) {
  ModbusResponse response1 = master1.available();
  if (response1) {
    if (response1.hasError()) {
      Serial.print("Empfangsfehler1");          
    } else {

      
        L1 = response1.getRegister(0);
        L2 = response1.getRegister(1);
        L3 = response1.getRegister(2);
      }

    }
  }
}

//----------------------------State of Charge SOC--------------------------------------------------------------------------------------------------
if (slave.connected()) {
if (millis() - lastSentTime4 > 1500) {
lastSentTime4 = millis();

  if (!master4.readInputRegisters(slave, 225, 266, 1)) {
    Serial.print("ReadFehler 2");
      }

  lastSentTime4 = millis();}
  
if (master4.isWaitingResponse()) {
  ModbusResponse response4 = master4.available();
  if (response4) {
    if (response4.hasError()) {
      Serial.print("Empfangsfehler2");
    } else {
  
      SOC = response4.getRegister(0)/10;
      

    }
  }
}

}

//----------------------------PV Leistung--------------------------------------------------------------------------------------------------
if (slave.connected()) {
if (millis() - lastSentTime5 > 1200) {

  if (!master5.readInputRegisters(slave, 100, 850, 1)) {
    Serial.print("ReadFehler 3");
      }

  lastSentTime5 = millis();}
  
if (master5.isWaitingResponse()) {
  ModbusResponse response5 = master5.available();
  if (response5) {
    if (response5.hasError()) {
      Serial.print("Empfangsfehler3");          
    } else {

// Serial.print("PV Power: ");
// Serial.print(response5.getRegister(0));

      PVpower = response5.getRegister(0);

// Serial.println();
}
}
}
}

//---------------------------------Auswertungscode-----------------------------------------------------------------------------------------

if (millis() - lastSentTime2 > 5000) {

ACgesamt = L1 + L2 + L3;
diffACPV = PVpower - ACgesamt;
SOC = SOC;

//--------------------------------Bedingungen SOC und POW-Power nicht erfüllt--------------------------------------------
if ((diffACPV <= 1200) || (SOC < 96)){

digitalWrite(Pumpe, HIGH);
digitalWrite(Pumpe_led, LOW);
analogWrite(PWM, 0);
digitalWrite(Heizstab1, HIGH);
digitalWrite(Heizstab1_led, LOW);
digitalWrite(Heizstab2, HIGH);
digitalWrite(Heizstab2_led, LOW);
digitalWrite(S1_led, LOW);
digitalWrite(S2_led, LOW); 
}

//--------------------------------SOC und POW-Power für 1000W Heizstab erfüllt--------------------------------------------

if ((diffACPV > 1200) && (diffACPV < 2400)){

if (digitalRead(S1) == LOW){//Ein Heizstab an und Ventil auf oben

digitalWrite(Pumpe, LOW);
digitalWrite(Pumpe_led, HIGH);
analogWrite(PWM, 220);
digitalWrite(Heizstab1, LOW);
digitalWrite(Heizstab1_led, HIGH);
digitalWrite(Heizstab2, HIGH);
digitalWrite(Heizstab2_led, LOW);
digitalWrite(Ventil, LOW);
Serial.print("XXXXXX");
}

if ((digitalRead(S1) == HIGH) && (digitalRead(S2) == LOW)){//Ein Heizstab an und Ventil auf unten
digitalWrite(Pumpe, LOW);
digitalWrite(Pumpe_led, HIGH);
analogWrite(PWM, 220);
digitalWrite(Heizstab1, LOW);
digitalWrite(Heizstab1_led, HIGH);
digitalWrite(Heizstab2, HIGH);
digitalWrite(Heizstab2_led, LOW);
digitalWrite(Ventil, HIGH);
Serial.print("YYYYYY");
}

if ((digitalRead(S1) == HIGH) && (digitalRead(S2) == HIGH)){// Heizstab aus
digitalWrite(Pumpe, HIGH);
digitalWrite(Pumpe_led, LOW);
analogWrite(PWM, 0);
digitalWrite(Heizstab1, HIGH);
digitalWrite(Heizstab1_led, LOW);
digitalWrite(Heizstab2, HIGH);
digitalWrite(Heizstab2_led, LOW);
Serial.print("ZZZZZZ");
}

}

//--------------------------------SOC und POW-Power für 2000W Heizstab erfüllt--------------------------------------------
/*
if ((diffACPV >= 2400) && (SOC >= 96)){

if (S1 = LOW){//Beide Heizstäbe an und Ventil auf oben

digitalWrite(Pumpe, LOW);
digitalWrite(Pumpe_led, HIGH);
analogWrite(PWM, 220);
digitalWrite(Heizstab1, LOW);
digitalWrite(Heizstab1_led, HIGH);
digitalWrite(Heizstab2, LOW);
digitalWrite(Heizstab2_led, HIGH);
digitalWrite(Ventil, LOW);}

if ((S1 = HIGH) && (S2 = LOW)){//Beide Heizstäbe an und Ventil auf unten
digitalWrite(Pumpe, LOW);
digitalWrite(Pumpe_led, HIGH);
analogWrite(PWM, 220);
digitalWrite(Heizstab1, LOW);
digitalWrite(Heizstab1_led, HIGH);
digitalWrite(Heizstab2, LOW);
digitalWrite(Heizstab2_led, HIGH);
digitalWrite(Ventil, HIGH);}
if ((S1 = HIGH) && (S2 = HIGH)){//Beide Heizstäbe aus
digitalWrite(Pumpe, HIGH);
digitalWrite(Pumpe_led, LOW);
analogWrite(PWM, 0);
digitalWrite(Heizstab1, HIGH);
digitalWrite(Heizstab1_led, LOW);
digitalWrite(Heizstab2, HIGH);
digitalWrite(Heizstab2_led, LOW);}
}
*/
//------------------------Serial Print zur Überwachung---------------------------------------------------------------------

Serial.print("AC Gesamt: ");
Serial.print(ACgesamt);
Serial.println();
Serial.print("diff AC - PV: ");
Serial.print(diffACPV);
Serial.println();
Serial.print("PV Power: ");
Serial.print(PVpower);
Serial.println();
Serial.print("SOC: ");
Serial.print(SOC);
Serial.println();
Serial.print("L1: ");
Serial.print(L1);
Serial.println();
Serial.print("L2: ");
Serial.print(L2);
Serial.println();
Serial.print("L3: ");
Serial.print(L3);
Serial.println();
Serial.println();

//----------------------------------------LCD-------------------------------------------------------
lcd1.setCursor(0,0);
lcd1.print("AC Gesamt: W ");
if (ACgesamt <1000) lcd1.print(" ");
if (ACgesamt <100) lcd1.print(" ");
if (ACgesamt <10) lcd1.print(" ");
lcd1.print(ACgesamt);

if (diffACPV >0){
lcd1.setCursor(0,1);
lcd1.print("Ueberschuss: W ");
if (diffACPV < 1000) lcd1.print(" ");
if (diffACPV <100) lcd1.print(" ");
if (diffACPV <10) lcd1.print(" ");
lcd1.print(diffACPV);}
if (diffACPV < 0){
lcd1.setCursor(0,1);
lcd1.print("Ueberschuss: W 0"); }

lcd1.setCursor(0,2);
lcd1.print("SOC: % ");
if (SOC <100) lcd1.print(" ");
if (SOC <10) lcd1.print(" ");
lcd1.print(SOC);

lcd1.setCursor(0,3);
lcd1.print("PV Power: W ");
if (PVpower <1000) lcd1.print(" ");
if (PVpower <100) lcd1.print(" ");
if (PVpower <10) lcd1.print(" ");
lcd1.print(PVpower);
//-----------------------------------------End LCD----------------------------------------------------

lastSentTime2 = millis();

}

}`

from arduino-tools40.

grphine avatar grphine commented on June 30, 2024

Thanks for shoowing your solution that works.
I had the idea of multiple concurrent master objects, but I didn't think it was so good and hoped there may be some recommended way to sequentially run things.

Thanks again, though

from arduino-tools40.

jordicasals avatar jordicasals commented on June 30, 2024

Hi,

if you want a 'blocking version' of the Modbus library you can implement something like this:

modbusTcpMaster.readInputRegisters(slaveAddress, inputRegisterAddr, numInputRegisters);
while (modbusTcpMaster.isWaitingResponse()) {
  // wait for a response
}
// detect timeout
// process response

from arduino-tools40.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.