Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error compiling code: Adruino IDE 2.3.4. #377

Open
hacesoft opened this issue Dec 25, 2024 · 2 comments
Open

Error compiling code: Adruino IDE 2.3.4. #377

hacesoft opened this issue Dec 25, 2024 · 2 comments

Comments

@hacesoft
Copy link

Error compiling code: Adruino IDE 2.3.4. library version: ArduinoModbus 1.0.9. Run on NodeMCU. Relevant libraries in the project:

#include <stdio.h>
#include <string>
#ifdef DEBUG_Unit_Main
#include <iostream>
#include <cstdint>
#include <iomanip> // For std::setw and std::setfill
#endif
#include <Arduino.h>
#include <ModbusIP_ESP8266.h>
#include <LittleFS.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <ArduinoOTA.h>

I handle the administration via wifi myself, I only use the services of the basic library: #include <ESP8266WiFi.h> the first error is directly in the library code: #include <ModbusIP_ESP8266.h> specifically in the file: ModbusTCP.h where it is necessary to update the method on line 17, otherwise the compiler reports an obsolete method

class WiFiServerESPWrapper : public WiFiServer {
public: 
WiFiServerESPWrapper(uint16_t port) : WiFiServer(port) {}
 inline WiFiClient accept() {
 return WiFiServer::accept();
 }
}; 

the second code compilation error reports:

c:\data\modbus-esp8266\src\ModbusRTU.cpp: In member function 'uint16_t ModbusRTUTemplate::send(uint8_t, TAddress, cbTransaction, uint8_t, uint8_t*, bool)':
c:\data\modbus-esp8266\src\ModbusRTU.cpp:183:96: warning: unused parameter 'unit' [-Wunused-parameter]
 183 | uint16_t ModbusRTUTemplate::send(uint8_t slaveId, TAddress startreg, cbTransaction cb, uint8_t unit, uint8_t* data, bool waitResponse) {
 | ~~~~~~~~^~~~
c:\data\modbus-esp8266\src\Modbus.cpp: In member function 'void Modbus::slavePDU(uint8_t*)':
c:\data\modbus-esp8266\src\Modbus.cpp:318:25: warning: comparison is always false due to limited range of data type [-Wtype-limits]
 318 | if (bufSize > MODBUS_MAX_FRAME) { // Frame to return too large
c:\data\modbus-esp8266\src\Modbus.cpp: In static member function 'static Modbus::ResultCode Modbus::_onRequestDefault(Modbus::FunctionCode, Modbus::RequestData)':
c:\data\modbus-esp8266\src\Modbus.cpp:905:67: warning: unused parameter 'fc' [-Wunused-parameter]
 905 | Modbus::ResultCode Modbus::_onRequestDefault(Modbus::FunctionCode fc, const RequestData data) {
| ~~~~~~~~~~~~~~~~~~~~~~~^~
c:\data\modbus-esp8266\src\Modbus.cpp:905:89: warning: unused parameter 'data' [-Wunused-parameter]
905 | Modbus::ResultCode Modbus::_onRequestDefault(Modbus::FunctionCode fc, const RequestData data) {
| ~~~~^~~~

there, in the appropriate functions, it would be enough to add something like this under the function: (void)_params; // Suppress the warning about an unused parameter.

@hacesoft
Copy link
Author

I use the code like this:

//************************* Global BUFFER for parsing **************************
#define nChunkSize 70 // Size of the send page
char sBuffer_RAM[nChunkSize]; // general output string, size define is in chunkSize
char sBuffer_RAM_1[nChunkSize]; // general output string, size define is in chunkSize

#define SDA_PIN D3 // Software I2C on pin D3
#define SCL_PIN D1 // Software I2C on pin D1

SensorManager sensorManager(SDA_PIN, SCL_PIN, 400000L);

void setup() {
.
.
.
setup_WiFi();
sensorManager.Init_reg_MODBUS();
sensorManager.begin();
sensorManager.Init_reg_MODBUS_MAC(false);
.
.
.
}

void loop() {
.
.
.
sensorManager.update();
 yield();
 handleTelnetClients();
.
.
.
}

class SensorManager {
public:
  //Konstructor
  SensorManager(int sdaPin, int sclPin, long clockSpeed)
        : lightSensor(BH1750FVI::k_DevModeContHighRes, sdaPin, sclPin, clockSpeed),
          bme(sdaPin, sclPin, clockSpeed) {

        // Initialize intervals
        previousMillis_100ms = INTERVAL_100MS;
        previousMillis_500ms = INTERVAL_500MS;
        previousMillis_1000ms = INTERVAL_1000MS;
        previousINTERVAL_10S = INTERVAL_10S;
    }
    //************************************************************************************
    void Init_reg_MODBUS_MAC(bool mode, WiFiClient* telnet = nullptr) {
      //mode znamena 0, vypisuji se hodnoty do konsloty, 1 vypisuji se data do telnetu
      // Definice MODBUD MAC
      WiFi.macAddress().toCharArray(sBuffer_RAM_1, nChunkSize); // Získání MAC adresy bez dvojteček a přímé uložení do sBuffer_RAM_1
      removeColonsAndStore (sBuffer_RAM_1, sBuffer_RAM); //Vysledek je ulozen v sBuffer_RAM
      {// Rozdělení MAC adresy na tři části a uložení do holding registru
        //char part1[5], part2[5], part3[5];

        // Kopírování částí MAC adresy
        strncpy(sBuffer_RAM_1, sBuffer_RAM, 4);
        sBuffer_RAM_1[4] = '\0';
        uint16_t upart1 = strtoul(sBuffer_RAM_1, nullptr, 16);

        strncpy(sBuffer_RAM_1, sBuffer_RAM + 4, 4);
        sBuffer_RAM_1[4] = '\0';
        uint16_t upart2 = strtoul(sBuffer_RAM_1, nullptr, 16);

        strncpy(sBuffer_RAM_1, sBuffer_RAM + 8, 4);
        sBuffer_RAM_1[4] = '\0';
        uint16_t upart3 = strtoul(sBuffer_RAM_1, nullptr, 16);

        // Uložení do holding registru
        modbusTCPServer.Hreg(REG_MAC2, upart1);
        modbusTCPServer.Hreg(REG_MAC1, upart2);
        modbusTCPServer.Hreg(REG_MAC0, upart3);

        if (mode == false){
          #ifdef DEBUG_Sensor_manager
            std::cout << "MAC ADRESA: " << sBuffer_RAM << std::endl;
            std::cout << "part1 (REG_MAC2): " << std::hex << upart1 << std::endl;
            std::cout << "part2 (REG_MAC1): " << std::hex << upart2 << std::endl;
            std::cout << "part3 (REG_MAC0): " << std::hex << upart3 << std::endl;
          #endif
        }
        else{//mode == 1 -> vypis hodnot do telnetu
          // Sestavení zprávy
          if (telnet != nullptr) {
            String message = "\r\n MAC ADRESA: " + String(sBuffer_RAM) + "\r\n";
            message += " part1 (REG_MAC2): " + String(upart1, HEX) + "\r\n";
            message += " part2 (REG_MAC1): " + String(upart2, HEX) + "\r\n";
            message += " part3 (REG_MAC0): " + String(upart3, HEX) + "\r\n";

            // Odeslání zprávy na Telnet server
            telnet->print(message);
            telnet->print(PROMPT); // Zobrazení promptu
          }
        }
      }
    }

 //******************************** MODBUS ********************************************
    void Init_reg_MODBUS() {
      
      // Nastavení ModbusIP jako server
      modbusTCPServer.server(Port_ModbusIP);
    
      modbusTCPServer.addHreg(REG_RND_NUMBER, 0);              //Nahodné číslo 0x0 žz 0xFFFF
      modbusTCPServer.addHreg(REG_BH1750FVI_nLux, 0);          //Hodnota osvetleni BH1750FVI

      modbusTCPServer.addHreg(REG_BME280_nTempBME, 0);         //Hodnota teploty BME280
      modbusTCPServer.addHreg(REG_BME280_nHumBME, 0);          //Hodnota humidity BME280
      modbusTCPServer.addHreg(REG_BME280_nPresBME_LO, 0);      //Hodnota pressure BME280 (tlak)
      modbusTCPServer.addHreg(REG_BME280_nPresBME_HI, 0);      //Hodnota pressure BME280 (tlak)
      modbusTCPServer.addHreg(REG_BME280_oTempUnit, 0);        //Hodnotoa teploty u BME280 C nebo F

      modbusTCPServer.addHreg(REG_Version_FW, 0);              //Firmware verze      //String(ESP.getCoreVersion()).c_str());
      modbusTCPServer.addHreg(REG_WiFi_dBm, 0);                //Signál Wi-Fi v dBm //(WiFi.SSID().c_str());
      
      modbusTCPServer.addHreg(REG_nPocetTeplomeru, 0);         //Aktualní počšt teploměrů DS18B20

      modbusTCPServer.addHreg(REG_Interni_DS18B20, 0);         //Interní čidlo na DPS
      modbusTCPServer.addHreg(REG_Interni_DS18B20_type, 0);    //Interní čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_Interni_DS18B20_version, 0); //Interní čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_1_DS18B20, 0);              //První externí čidlona DPS
      modbusTCPServer.addHreg(REG_1_DS18B20_type, 0);         //První externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_2_DS18B20_version, 0);      //První externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_2_DS18B20, 0);              //Druhé externí čidlona DPS
      modbusTCPServer.addHreg(REG_2_DS18B20_type, 0);         //Druhé externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_1_DS18B20_version, 0);      //Druhé externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_3_DS18B20, 0);              //Třetí externí čidlona DPS
      modbusTCPServer.addHreg(REG_3_DS18B20_type, 0);         //Třetí externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_3_DS18B20_version, 0);      //Třetí externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_4_DS18B20, 0);              //Čtvrté externí čidlona DPS
      modbusTCPServer.addHreg(REG_4_DS18B20_type, 0);         //Čtvrté externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_4_DS18B20_version, 0);      //Čtvrté externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_5_DS18B20, 0);              //Páté externí čidlona DPS
      modbusTCPServer.addHreg(REG_5_DS18B20_type, 0);         //Páté externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_5_DS18B20_version, 0);      //Páté externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_6_DS18B20, 0);              //Šesté externí čidlona DPS
      modbusTCPServer.addHreg(REG_6_DS18B20_type, 0);         //Šesté externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_6_DS18B20_version, 0);      //Šesté externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_7_DS18B20, 0);              //Sedmé externí čidlona DPS
      modbusTCPServer.addHreg(REG_7_DS18B20_type, 0);         //Sedmé externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_7_DS18B20_version, 0);      //Sedmé externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_8_DS18B20, 0);              //Osmé externí čidlona DPS
      modbusTCPServer.addHreg(REG_8_DS18B20_type, 0);         //Osmé externí čidlo na DPS sThis_teplomer->type
      modbusTCPServer.addHreg(REG_8_DS18B20_version, 0);      //Osmé externí čidlo na DPS sThis_teplomer->version

      modbusTCPServer.addHreg(REG_Analog0, 0);                //Analog vstup 0
      modbusTCPServer.addHreg(REG_Analog1, 0);                //Analog vstup 1

      modbusTCPServer.addHreg(REG_Digital0, 0);               //Digitalni vstup 0
      modbusTCPServer.addHreg(REG_Digital1, 0);               //Digitalni vstup 0

      modbusTCPServer.addHreg(REG_MAC0, 0);                   //MAC0 adresa zařízení
      modbusTCPServer.addHreg(REG_MAC1, 0);                   //MAC1 adresa zařízení
      modbusTCPServer.addHreg(REG_MAC2, 0);                   //MAC2 adresa zařízení

      modbusTCPServer.addHreg(IP0, 0);                        //IP0 - HI - adresa zařízení
      modbusTCPServer.addHreg(IP1, 0);                        //IP1 - LO - adresa zařízení


    }

No more code is needed to get the idea.

@hacesoft
Copy link
Author

Further code analysis:

Errors in ModbusRTU.cpp
Warning about unused parameter unit

Line: 183
Description: The parameter unit is not used in the send function.
Fix: If the parameter is not needed, you can remove it from the function declaration.
//***********************************

Errors in Modbus.cpp
Error in comparison

Line: 318
Description: The comparison bufSize > MODBUS_MAX_FRAME always fails due to the limited range of the data type.
Fix: Check whether bufSize can be larger than MODBUS_MAX_FRAME. If so, check whether MODBUS_MAX_FRAME is defined correctly.

if (bufSize > MODBUS_MAX_FRAME) {
exceptionResponse(fcode, EX_ILLEGAL_ADDRESS);
return;
}

//****************************************

Warning about unused parameter fc and data

Line: 905
Description: The parameters fc and data are not used in the _onRequestDefault function.
Fix: If parameters are not needed, you can remove them from the function declaration.

Modbus::ResultCode Modbus::_onRequestDefault(Modbus::FunctionCode fc, const RequestData data) {
return EX_SUCCESS;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant