Hex serial data receiving problem

I am able to receive serial data in hex
All i want is to cut a part of the data with known start and end bytes

Hi, Could you specify your project better?

There is a Substring function that captures a snippet of a string

For example:


String datachar = ("IM LIVING ON BRAZIL AND IM CRAZY");

void setup() {

 Serial.begin (9600);

}

void loop() {

  Serial.println (datachar.substring (3,9));    /// will print the 3 char until 9.

}

This Code will write on Serial monitor only the word LIVING

The received data is in hex
23 32 12 12 34 66 64 04 03 56 12 02
I want to extract 3 bytes 12 56 03

Is the hex data a string, a String or something else ?
Will the data that you are interested in always be in the same position within the data ?

Look for a "12" and adjust the next look for a "56". If a "56" arrives look for a "03". If not, reset to look for "12".

Yes the received data is a string

How?

Is it a string (a zero terminated array of chars) or a String (an object created by the String library) ?

Please post an example complete sketch that receives the data then it will be clearer

What how?
Starting the sequence You look for a certain character. At the start let the "search character" be "12". When "12" is found change the search character to the next, "56".

Could you show me a code example!

Sorry, no. If You know that little about coding You need to step back and pick up knowledge.
Start with reading the first topics telling how to use this forum as well as how to get the best from this forum.
You provide no useful information at all.....

Post your sketch that does that.

What is the source of the data?

i am using this this sketch but it does not work
i was sking for another way
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11);
const byte numBytes = 32;
byte receivedBytes[numBytes];
byte numReceived = 0;

boolean newData = false;

void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {
recvBytesWithStartEndMarkers();
showNewData();
}

void recvBytesWithStartEndMarkers() {
boolean recvInProgress = false;
byte ndx = 0;
byte startMarker =0x12;
byte endMarker = 0x03;
byte rb;

while (mySerial.available() > 0 && newData == false) {
    
    
    rb = mySerial.read();
    if (recvInProgress == true) {
        
        if (rb != endMarker) {
            receivedBytes[ndx] = rb;
            ndx++;
            if (ndx >= numBytes) {
                ndx = numBytes - 1;
            }
        }
        else {
            receivedBytes[ndx] = '\0'; // terminate the string
            recvInProgress = false;
            numReceived = ndx;  // save the number for use when printing
            ndx = 0;
            newData = true;
            Serial.write(receivedBytes[1]);
        }
    }

    else if (rb == startMarker) {
        recvInProgress = true;
    }
}

}

void showNewData() {
if (newData == true) {
Serial.print("This just in (HEX values)... ");
for (byte n = 0; n < numReceived; n++) {
Serial.print(receivedBytes[n], HEX);
Serial.print(' ');
}
Serial.println();
newData = false;
}
}

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

What do you see on the Serial monitor ?


#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11);
const byte numBytes = 32;
byte receivedBytes[numBytes];
byte numReceived = 0;

boolean newData = false;

void setup() {
    Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {
    recvBytesWithStartEndMarkers();
    showNewData();
}

void recvBytesWithStartEndMarkers() {
     boolean recvInProgress = false;
     byte ndx = 0;
    byte startMarker =0x12;
    byte endMarker = 0x03;
    byte rb;
   

    while (mySerial.available() > 0 && newData == false) {
        
        
        rb = mySerial.read();
        if (recvInProgress == true) {
            
            if (rb != endMarker) {
                receivedBytes[ndx] = rb;
                ndx++;
                if (ndx >= numBytes) {
                    ndx = numBytes - 1;
                }
            }
            else {
                receivedBytes[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                numReceived = ndx;  // save the number for use when printing
                ndx = 0;
                newData = true;
                Serial.write(receivedBytes[1]);
            }
        }

        else if (rb == startMarker) {
            recvInProgress = true;
            Serial.write(rb);
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in (HEX values)... ");
        for (byte n = 0; n < numReceived; n++) {
            Serial.print(receivedBytes[n], HEX);
            Serial.print(' ');
        }
        Serial.println();
        newData = false;
    }
}

nothing

1. So, you have a frame of 12-byte which begins with 0x23 (START mark) and terminates with 0x02 (STOP mark). (The spaces that you have shown, I assume, are for clarity.) The rest are ASCII coded digits of hex numbers. Correct?

2. You want to extract three bytes which are lying just behind the terminating code (02). Correct?

3. You want to save the three bytes data of Step-2 into three seperate variables. Correct?

4. If the answers to all the above questiopns are "Yes", then you may execute the following sketch to capture the string for the digits and then retrive your desired data bytes from the received string:

#include<SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

bool flag = false;
char myArray[50];

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  byte n = mySerial.available();
  if (n != 0)
  {
    if (flag == false)
    {
      byte x = mySerial.read();
      if (x == 0x23)
      {
        flag = true;
      }
    }
    else
    {
      byte m = mySerial.readBytesUntil(0x02, myArray, 50);
      myArray[m] = '\0';
      Serial.println(myArray);  //shows the recived array except 0x23 and 0x02
      //---- process the array charcaters to retrive the desired bytes----------
      memset(myArray, 0x00, 50);
      flag = false;
    }
  }
}
1 Like