How to extract bytes from byte array that received in rx ?
How to extract bytes from byte array that received in rx ?
Hi All,
I hope you are all well,
I used interrupt rx to receive data and stored on rx_buffer, this was successfully done, so I want to extract a specific byte location on the stream data.
For example:
byte: 0 1 2 3 4 5 6 7 8
start com D1 D2 X X D3 D4 X
0xAA 0x1B 0x11 0x12 0x00 0x00 0x13 0x14 0xFF
So, I have now in rx_buffer[] = { 0xAA, 0x1B, 0x11, 0x12, 0x00, 0x00, 0x13, 0x14, 0xFF }
and I only want D1=0x11, D2=0x12, D3=0x13, D4=0x14 (the actual data), is there any library function can do the job or please can you write a simple function with for loop or other that can extract these data.
Thank you
I hope you are all well,
I used interrupt rx to receive data and stored on rx_buffer, this was successfully done, so I want to extract a specific byte location on the stream data.
For example:
byte: 0 1 2 3 4 5 6 7 8
start com D1 D2 X X D3 D4 X
0xAA 0x1B 0x11 0x12 0x00 0x00 0x13 0x14 0xFF
So, I have now in rx_buffer[] = { 0xAA, 0x1B, 0x11, 0x12, 0x00, 0x00, 0x13, 0x14, 0xFF }
and I only want D1=0x11, D2=0x12, D3=0x13, D4=0x14 (the actual data), is there any library function can do the job or please can you write a simple function with for loop or other that can extract these data.
Thank you
Re: How to extract bytes from byte array that received in rx ?
Post deleted due to me being a complete idiot when I wrote it.
Apologies to original Poster - will try to be more helpful in future posts
Apologies to original Poster - will try to be more helpful in future posts
Last edited by hexreader on 17 Jun 2020 05:08, edited 1 time in total.
Start every day with a smile...... (get it over with)
Re: How to extract bytes from byte array that received in rx ?
Hi,
Thank you for your replay,
There isn't any hidden information if this is a general question? it does not matter which pic or any other details, but let me explain a bit if that make more sense to you,
I have a sensor that connected to UART and if I send a request to the sensor, it will response with serial data and that's all. so, I can handle the serial response that contain actual wanted data and other unwanted byte (like start byte, command byte, X byte) and store them in buffer (array variable) so what I want now is that how I can extract these actual data from the buffer = { string of bytes} ?
Regards
Thank you for your replay,
There isn't any hidden information if this is a general question? it does not matter which pic or any other details, but let me explain a bit if that make more sense to you,
I have a sensor that connected to UART and if I send a request to the sensor, it will response with serial data and that's all. so, I can handle the serial response that contain actual wanted data and other unwanted byte (like start byte, command byte, X byte) and store them in buffer (array variable) so what I want now is that how I can extract these actual data from the buffer = { string of bytes} ?
Regards
Re: How to extract bytes from byte array that received in rx ?
Sorry,
Seem to be grumpy today - unkind remarks removed.
Dany seems to have the solution.
Seem to be grumpy today - unkind remarks removed.
Dany seems to have the solution.
Last edited by hexreader on 16 Jun 2020 21:48, edited 1 time in total.
Start every day with a smile...... (get it over with)
Re: How to extract bytes from byte array that received in rx ?
You do not need a library function, but this:So, I have now in rx_buffer[] = { 0xAA, 0x1B, 0x11, 0x12, 0x00, 0x00, 0x13, 0x14, 0xFF }
and I only want D1=0x11, D2=0x12, D3=0x13, D4=0x14
Code: Select all
D1 = rx_buffer[2];
D2 = rx_buffer[3];
D3 = rx_buffer[6];
D4 = rx_buffer[7];
Last edited by Dany on 05 Aug 2021 20:49, edited 1 time in total.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)
Re: How to extract bytes from byte array that received in rx ?
Thank you for you both, it is great suggestion
What about string values? So the buffer now received a serious of string for example rx_buffer=ABCDEFGHI... but we knew that the first two char (AB) is start byte and we don't need that, and the second two char CD is command byte and also we don't need that, and EF is the actual data.. and we want to extract these bytes from the entire string? Is it the same method as you suggested or it will be different? because the buffer will received a group of char string without any coma in between bytes, just like a bunch of serial string data?
What about string values? So the buffer now received a serious of string for example rx_buffer=ABCDEFGHI... but we knew that the first two char (AB) is start byte and we don't need that, and the second two char CD is command byte and also we don't need that, and EF is the actual data.. and we want to extract these bytes from the entire string? Is it the same method as you suggested or it will be different? because the buffer will received a group of char string without any coma in between bytes, just like a bunch of serial string data?
-
- Posts: 1558
- Joined: 24 Jun 2007 19:27
- Location: 01800 St Maurice de Gourdans France
- Contact:
Re: How to extract bytes from byte array that received in rx ?
hello,
if rx_buffer contains "ABCDEFGHI."
and EF represents the data
Data contains the string "EF"
maybe need after to convert it in decimal via "atoi" function ?
if rx_buffer contains "ABCDEFGHI."
and EF represents the data
Code: Select all
char Data[3];
strncpy(Data,Rx_buffer,4,2); // 4 is the start position of Data into the buffer
Data{2]=0; // more safety to put the zero as end of string
maybe need after to convert it in decimal via "atoi" function ?
Re: How to extract bytes from byte array that received in rx ?
Thank you very much for your replay,
That exactly what I am looking for, as I ask in the first post if there any function in the library can do the job,
what does it mean Data{2]=0; is mean make the second as 0
so, Data[0]=E;
Data[1]=F;
Data[2]=0;
thank you again,
That exactly what I am looking for, as I ask in the first post if there any function in the library can do the job,
what does it mean Data{2]=0; is mean make the second as 0
so, Data[0]=E;
Data[1]=F;
Data[2]=0;
thank you again,
Re: How to extract bytes from byte array that received in rx ?
I check the function that you suggested but I found that it only accept two parameters, and size ! and return something different from that you explain? could you please elaborate this!
This what I found in help library:
char txt[] = "mikroElektronika";
char txt_sub = "mikro_test";
int res;
res = strncpy(txt,txt_sub,4); // copies first 4 characters form the string 'txt_sub' to 'txt'
This what I found in help library:
char txt[] = "mikroElektronika";
char txt_sub = "mikro_test";
int res;
res = strncpy(txt,txt_sub,4); // copies first 4 characters form the string 'txt_sub' to 'txt'
Re: How to extract bytes from byte array that received in rx ?
I found in other forum, one said strncpy function copies a string from the first character to a given length. But it cannot copy a portion of a string in the middle. The other one suggest to use some offset with proper arguments,
so if we have string as rx_buffer=[ABCDEFHI...] and we only looking for specific portion in the middle i.e. EF as a data,
char Data[3];
strncpy(Data,rx_buffer+4,2); // so we will do offset of +4 on rx_buffer and copy 2 char after this offset,
// which is the actual data that we are looking for EF.
Data{2]=0;
Is the offset possible here on MikroC ? or it works with other compiler?
Regards
so if we have string as rx_buffer=[ABCDEFHI...] and we only looking for specific portion in the middle i.e. EF as a data,
char Data[3];
strncpy(Data,rx_buffer+4,2); // so we will do offset of +4 on rx_buffer and copy 2 char after this offset,
// which is the actual data that we are looking for EF.
Data{2]=0;
Is the offset possible here on MikroC ? or it works with other compiler?
Regards
-
- Posts: 1558
- Joined: 24 Jun 2007 19:27
- Location: 01800 St Maurice de Gourdans France
- Contact:
Re: How to extract bytes from byte array that received in rx ?
yes, this is OK !
i did a mistake in my previous response ...
Code: Select all
strncpy(Data,Rx_buffer+4,2); // 4 is the start position of Data into the buffer 2 =nb of data to transfer
Data{2]=0; is needed only if you want to treat Data as a string ...
Re: How to extract bytes from byte array that received in rx ?
Thank you,
it is clear now
it is clear now
Re: How to extract bytes from byte array that received in rx ?
sorry about confusion again
strncpy(Data,Rx_buffer+4,2); is the shift here mean that count from 0 to 3 (which is 4 steps) or from 1 to 4 and then take the byte 4 or byte 5?
as our previous example Rx_buffer=[ABCDEFHI...]
so in this case strncpy(Data,Rx_buffer+4,2); will return EF or FH !
the +4 mean at it will take the byte at four or jump these all four bytes and then get the next one? and we should count from 0 ?
strncpy(Data,Rx_buffer+4,2); is the shift here mean that count from 0 to 3 (which is 4 steps) or from 1 to 4 and then take the byte 4 or byte 5?
as our previous example Rx_buffer=[ABCDEFHI...]
so in this case strncpy(Data,Rx_buffer+4,2); will return EF or FH !
the +4 mean at it will take the byte at four or jump these all four bytes and then get the next one? and we should count from 0 ?