How to extract bytes from byte array that received in rx ?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
ennng
Posts: 61
Joined: 25 Sep 2012 19:48

How to extract bytes from byte array that received in rx ?

#1 Post by ennng » 16 Jun 2020 15:08

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

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: How to extract bytes from byte array that received in rx ?

#2 Post by hexreader » 16 Jun 2020 15:53

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

:(
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) :)

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: How to extract bytes from byte array that received in rx ?

#3 Post by ennng » 16 Jun 2020 16:10

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

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: How to extract bytes from byte array that received in rx ?

#4 Post by hexreader » 16 Jun 2020 16:20

Sorry,

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) :)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: How to extract bytes from byte array that received in rx ?

#5 Post by Dany » 16 Jun 2020 17:43

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
You do not need a library function, but this:

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)

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: How to extract bytes from byte array that received in rx ?

#6 Post by ennng » 17 Jun 2020 06:35

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?

paulfjujo
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 ?

#7 Post by paulfjujo » 17 Jun 2020 08:56

hello,


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
Data contains the string "EF"
maybe need after to convert it in decimal via "atoi" function ?

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: How to extract bytes from byte array that received in rx ?

#8 Post by ennng » 17 Jun 2020 09:08

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,

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: How to extract bytes from byte array that received in rx ?

#9 Post by ennng » 17 Jun 2020 09:38

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'

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: How to extract bytes from byte array that received in rx ?

#10 Post by ennng » 17 Jun 2020 10:14

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

paulfjujo
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 ?

#11 Post by paulfjujo » 18 Jun 2020 08:41

ennng wrote:
17 Jun 2020 10:14
.....
Is the offset possible here on MikroC ? or it works with other compiler?
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
buffer+4 point on the data buffer[4]
Data{2]=0; is needed only if you want to treat Data as a string ...

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: How to extract bytes from byte array that received in rx ?

#12 Post by ennng » 18 Jun 2020 09:42

Thank you,
it is clear now :)

ennng
Posts: 61
Joined: 25 Sep 2012 19:48

Re: How to extract bytes from byte array that received in rx ?

#13 Post by ennng » 18 Jun 2020 13:03

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 ?

paulfjujo
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 ?

#14 Post by paulfjujo » 18 Jun 2020 16:14

ennng wrote:
18 Jun 2020 13:03
.. and we should count from 0 ?
Yes !

Post Reply

Return to “mikroC PRO for PIC General”