what is DATA? is it a character or or string?
if it is anything other than character type, your declaration won't work.
by knowing what type is DATA, a fix can be suggested.
maybe, you can post your full sketch here.
Thanks
aarg:
I think I understand. You probably want something like
char embeddedChar;
...
embeddedChar = Array[1];
since OP was trying to do "0x02%s0x21" I suspect DATA actually represents many bytes, may be even a cString with its trailing null char... hence my general answer #4
abdelhmimas:
if DATA is not a constant char type, the declaration: char Array[]={0x02,DATA,0x21} wont compile anyway
well I assumed it was conceptual DATA is just to represent stuff in the buffer, a number of bytes. It's not actual code. May be char Array[]={0x02, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', 0x00, 0x21} The the message would be "Hello World"
what made me think this way was the %s in the sscanf, looks like OP was assuming a string would be there
Making a few assumptions I think this will do the job
char Array[] = {0x02, 0x54, 0x55, 0x21};
const size_t MAX_DATA_LEN = 100;
char dataExtracted[MAX_DATA_LEN + 1]; // add one for possible terminating null if data is really chars
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
for (int i = 10; i > 0; i--) {
Serial.print(i);
Serial.print(' ');
delay(500);
}
Serial.println();
size_t dataSize = sizeof(Array) - 2;
if ( dataSize > MAX_DATA_LEN) {
while (1) {
Serial.print("Error!! need to make MAX_DATA_LEN larger, i.e. >="); Serial.println((sizeof(Array) - 2));
delay(5000);
}
}
memcpy(dataExtracted,Array+1,dataSize);
dataExtracted[dataSize] = '\0'; // terminate
Serial.println(dataExtracted);
}
void loop() {
// put your main code here, to run repeatedly:
}
If you need to duplicate the data then @drmpf code should do the job and as long as there are no null char in the payload in between your start byte and end byte and it’s ASCII then you can print it also in one go once you added the trailing null in the copy
If you don’t need to duplicate the buffer you can just replace the end marker 0x21 by 0x00 and start your printing or parsing at the index 1 instead or 0.
If you receive this payload on a stream then you could detect when to record after receiving the start marker and end the recording at the end marker. This way your buffer is ready to be used. I would suggest to study Serial Input Basics to handle this
If it is a binary protocol - probably not as 1 byte start and end marker could appear in the data - then you need to study in details the structure of the message to handle it correctly
@Ukhelibob,
not sure this declaration is right:
char * array[] = {0x02, "Hello World", 0x21};
this one will compile
char * array[] = {"blabla", "Hello World", "blabla"};
had no null char embedded (and was more painful to type in )
EDIT: actually on second thought
char array[] = {0x02, "Hello World", 0x21};
does not do what first look would hint at; you likely end up with 1 char in the second position which is the LSB of the constant cString memory address... does not work at all then