Help to compare uint8_t with uint16_t hex value modus project

I am working on a project that reads data from various modbus sensors using the modbusmaster library.

In one sensor i read 3 16 bit registers to get the time from the sensor with this.

    void node5_rtu_time(){
      uint8_t j, result;
      node.setSlaveId(Node_5);
      // slave: read (8) 16-bit registers starting at register 0 to RX buffer
      result = node.readHoldingRegisters(52, 3);
      if (result == 224 || result == 226){
        delay(100);
        result = node.readHoldingRegisters(52, 3);
      }      
      #ifdef MQTT_NODEFUNC_DEBUG
        Serial.print("Node_5 Result: ");
        Serial.print(result);
        Serial.print(" : ");
      #endif
      if (result == node.ku8MBSuccess)
      {     
        for (j = 0; j < 3; j++)
        {
          node5_time[j] = node.getResponseBuffer(j);    
          #ifdef MQTT_NODEFUNC_DEBUG
            Serial.print(node5_time[j], HEX);
            if (j < 2){
              Serial.print(", ");
            }  
          #endif
        }
        #ifdef MQTT_NODEFUNC_DEBUG     
          Serial.print(" -- ");
          Serial.print("EC: ");
          Serial.print(errorcnt);
          Serial.print(" ");        
          Serial.print("CY: ");
          Serial.println(cycle);
          // Serial.println();
        #endif
        RainClockYear = highByte(node5_time[0]);
        RainClockMonth = lowByte(node5_time[0]);
        RainClockDay = highByte(node5_time[1]);
        RainClockHour = lowByte(node5_time[1]);
        RainClockMin = highByte(node5_time[2]);
        RainClockSecond = lowByte(node5_time[2]);
        Serial.print("Rain Sensor Time: ");
        Serial.print(RainClockYear, HEX);
        Serial.print("-");
        Serial.print(RainClockMonth, HEX);
        Serial.print("-");
        Serial.print(RainClockDay, HEX);
        Serial.print(" ");
        Serial.print(RainClockHour, HEX);
        Serial.print(":");
        Serial.print(RainClockMin, HEX);
        Serial.print(":");
        Serial.println(RainClockSecond, HEX);        
        cycle++;
        // send50=true;   
      }         
      else {
        errorcnt++;
        cycle++;
        Serial.print("Node_5 ER: ");
        Serial.print(result);
        Serial.print(" ");      
        Serial.print("EC: ");
        Serial.println(errorcnt);
      }

    }

    
  #endif

The output of the routine is this, and correct if i use the HEX option in the print commands.

Node_5 Result: 0 : 2408, 1915, 5610 -- EC: 0 CY: 0
Rain Sensor Time: 24-8-19 15:56:10

You can see that the time is actually BCD coded into the bytes received and if i use high and low byte i can load the individual numbers into the rain sensor time printout.

All is good but now i have an issue with how to compare the time variables from the rain sensor with the time variables from my rtc on the esp32. I created two conditionals in my setup to do this comparison as follows as a test

  RtcDateTime now = Rtc.GetDateTime();//rtc


  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
  }

  second = timeinfo.tm_sec;
  minute = timeinfo.tm_min;
  hour = timeinfo.tm_hour;
  day = timeinfo.tm_yday + 1;
  month = timeinfo.tm_mon + 1;
  year = timeinfo.tm_year + 1900;
  weekday = timeinfo.tm_wday +1;

  node5_rtu_time();

  if(hour != RainClockHour){
    Serial.println("Rtc and RainClockHour does not match");
    Serial.print(hour);
    Serial.print(" = ");
    Serial.println(RainClockHour);
    Serial.print(hour, HEX);
    Serial.print(" = ");
    Serial.println(RainClockHour, HEX);          
  }

  if(hour == RainClockHour){
    Serial.println("Rtc and RainClockHour matches");
    Serial.print(hour);
    Serial.print(" = ");
    Serial.println(RainClockHour);
    Serial.print(hour, HEX);
    Serial.print(" = ");
    Serial.println(RainClockHour, HEX);          
  }

The comparison fails since the rtc hour variable contains 15, but the RainClockHour contains 21. The RainClockHour does contain 15 if i print it as HEX. Here is the output from the code above

Node_5 Result: 0 : 2408, 1915, 5610 -- EC: 0 CY: 0
Rain Sensor Time: 24-8-19 15:56:10
Rtc and RainClockHour does not match
15 = 21
F = 15

Since this one particular sensor likes to embed many variables in this way i do need a way to compare things, i am lost at how to compare the hour variable with the HEX equivalent of the RainClockHour variable and not its decimal value. I am also going to need to load rtc variables back into the 3, 16 bit bytes to send a command to update the time on the sensor if they don't match.

I think you need to extract the BCD and convert it to decimal.

Maybe this helps:

uint8_t func(uint16_t value, uint8_t pos)
{
  return (value >> 4*pos) & 0x0f;
}

void setup() {
  Serial.begin(115200);

  uint16_t y = 0x2408;

  uint8_t year2 = func(y, 3) * 10 + func(y, 2);
  Serial.println(year2);
}

Choose one representation that allows for easy comparison and convert all values into that format before comparing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.