Difficulty using the substring function

Hello everybody

I'm having trouble using the substring function.

Does anyone have an idea that could help me please.

The Arduino receives a message that is available in the buffTans variable, for example: RX=7B FF 2C FF 2C FF FF 7D. I need to get the three values sent, which in this case are FF, FF, FF FF. As I said, I used the substring function, but the result was not what I expected.

void getDLMsg(){                                   // Sigfox Downlink Payload ************************************
  recvMsg *RecvMsg;
  int result;

  RecvMsg = (recvMsg *)malloc(sizeof(recvMsg));
  result = Isigfox->getdownlinkMsg(RecvMsg);
  for (int i=0; i<RecvMsg->len; i++){
                                                   // Downlink string example RX=7B FF 2C FF 2C FF FF 7D
    buffTrans = (RecvMsg->inData[i]);              //                            {  FF ,  FF  , FF FF  }
    Serial.print (buffTrans);                      //                          {HazardZone, Alarm ack, Time to TX}
   }
     parseData();
    Serial.println ("");
    free(RecvMsg);
}
//------------------------------------------------------------------------------------------------------------------
void parseData() {                                // split the data into its parts
//
    BlinkLED();
    pinMode(redLED, OUTPUT);
//
    hazardZone  = buffTrans.substring (6,8);
    alAck       = buffTrans.substring (12,14);
    tTTx        = buffTrans.substring (18,23);

                Serial.print ("Hazard Zone: ");  // Only for test. The data will be used in another part of the code
                Serial.println (hazardZone);
                Serial.print ("Alarm Ack: ");
                Serial.println (alAck);
                Serial.print ("Time to TX: ");
                Serial.println (tTTx);
}

Please post the full sketch so that the problem can be seen in context. For instance, where is buffTrans declared and what data type is it ?

The UART has no knowledge about HEX, ASCII, BIN, .... he only send bytes between 0 and 255. It is the software that make it the correct character.

if you read the serial data into a char array you can parse it so

void setup() {
  Serial.begin(115200);
  char RX[]="7B FF 2C FF 2C FF FF 7D";
  int d[8]={0};
  sscanf(RX,"%2x%2x%2x%2x%2x%2x%2x%2x",&d[0],&d[1],&d[2],&d[3],&d[4],&d[5],&d[6],&d[7]);
  for(int i=0;i<8;i++) {
    Serial.print(d[i],HEX);
    Serial.print(' ');
  }
}

void loop() {}

when run serial monitor displays

7B FF 2C FF 2C FF FF 7D 

Are you sure that the message has string format "7B FF 2C FF 2C FF FF 7D" and not the bytes with this binary values?
From what program do you receive it?

Hello b707,
Thanks for the answer.
Actually the data is not marked ". It might work if I include the marks ", but I still don't know how I would do that.
This data comes from the Sigfox Backend and I capture the data by the payload sent through the Sigfox IoT module

Hi Horace,
Thanks.
I will try your suggestion for sure.

Hi, your answer is not entirely clear. Are you receiving binary data or text? For example FF is one byte 255 or two characters "FF" ?

Hi UKHeliBob
Thanks for your response.
Yes, of course it would be better.
I'll organize the code better and post it here.

That's the problem. I'm not sure what kind of data is it. See below:

buffTrans = (RecvMsg->inData[i]);

The message comes by expression (RecvMsg->inData[i])
I only load the content in a String created by me buffTrans, but I don't do any processing on this String.

buckfast bee keeper, I understand. I believe that if I can create a real String with this data it will work.
When I say true String, I mean using the " marks between the data.

His example begins with RX=. Looks like ASCII text to me.

But still, the buffer name has the address and each byte after is an at an offset.

exactly GoForSmoke... that's my difficulty.
the Payload looks like a String, but when I treat it as a String, it doesn't behave like one.
I believe that if I take the data RX=7B FF 2C FF 2C FF FF 7D and manage to transform it into {"RX=7B FF 2C FF 2C FF FF 7D"} I solve my problem

Where from ?

The data is a payload received by the Sigfox module from Wisol WSFM11R2DAT which receives information from the sigfox backend

Please provide the output of this part of the code.

Allocating memory without checking the result of it is dangerous.

I'm not familiar with SigFox. What does result tell you? You should check it.

when I was using the MKRFOX the SIGFOX downlink messages were 8 bytes of the form

Response from server:
 0xDE 0xAD 0xBE 0xEF 0xCA 0xFE 0xBA 0xBE

code was of the form

  if (SigFox.parsePacket()) {
    Serial.println("Response from server:");
    while (SigFox.available()) {
      Serial.print(" 0x");
      Serial.print(SigFox.read(), HEX);
    }

looks like SigFox.read() returns a byte which I printed in HEX format, e.g. function prototype

int read();

return next byte as an int

Excuse me, but maybe it's me who doesn't understand, but that's what I do

char* recvMsg;
recvMsg = (char*)malloc(length * sizeof(char));
if(recvMsg != nullPrt)
{
no memory......
}

Result checks if the received message is ok. If it is correct, the "serial monitor" prints OK on the screen. So the whole message on the screen will be:
OK
RX=7B FF 2C FF 2C FF FF 7D

Actually the Sigfox backend sends the 8 byte downlink only. In HEX.
However, we have two libraries involved in the process:
#include <WISOL.h>
and also
Isigfox *Isigfox = new WISOL();
I'm trying to simplify the process by using the substring function to pull out the data I really need.

Do you think it would be possible for me to take the line RX= 7B FF 2C FF 2C FF FF 7D and transform it into {"RX= 7B FF 2C FF 2C FF FF 7D"}?