0

how are you?. I'm having some trouble analizing streamed data. Recently I made a program that analizes streamed data and works for the first message of the stream as you can see in the following post

Problem with structire packing

when I noticed that program will work only for the first message for the way the buffer is being filler I changed the focus of the processing to move the compare variable to the position of the last byte arrived and then compare backwards since I need to check first the suffix of the message to determine where the prefix is, after that then decide what to do (this last part is not done yet).

The problem that I have here is that as I'm inside the analize_stream function I want to use the pointer to the reception buffer I declared to compare instead the original reception buffer name, so when I try to use the pointer then the first element appears perfectly but the final three appears as zero as you can see in the following image

Resultado con incomming

and the code is this

uint8_t analize_string(uint32_t * incomming)
{
    static uint8_t incCounter = 0;
    static uint8_t arreglo[Long_Rec] = {0};
    arreglo[incCounter] = incomming[incCounter];
    if((incomming[incCounter] == 0xff)
        && (incomming[incCounter  - 1] == 0xff)
        && (incomming[incCounter - 2] == 0xff))
    {
        incCounter = 0;
    }
    if(interface.aHMI)
    {
//////      screen_handler();
        interface.aHMI = false;
    }
    receive_data((char *) &U2rxBuffer[incCounter], (uint16_t) sizeof(uint8_t));
    return incCounter;
}

as you can see in the image the incomming pointer is correctly directed since it has the correct data to analize (0xffffff86) but when I did the comparison with the "if" then it didn't worked so I added the operation to mirror the incomming pointer (U2rxBuffer actually) in the array "arreglo", then arreglo is showing the second, third and fourth element equal to zero but when I do the same operation but mirroring directly U2rxBuffer into arreglo it shows the content of arreglo as should be as you can see in the following image:

Resultado con U2rxBuffer

the only thing that I changed to the code was "incomming" for "U2rxBuffer", so, why if incomming is correctly directed then it doesn't assign the data for addresses beyond initial address?.

This program righ now what does is to call analize_string every time a new byte arrives and exits until a new one arrives.

NOTE: Seeing again my previous post I noted that yesterday I was this problem too but I didn't noted.

Please help.

vram
  • 158
  • 12
  • The main issue, as explained at your previous question, is that this is a severely unwise architecture. As for specific mistakes, you're mixing up uint32_t vs uint8_t arrays. But don't try to fix it; the best advice would be to study more usual situations to this need and then start over. – Chris Stratton Dec 04 '20 at 22:20
  • Thanks @ChrisStratton for your soon answer, is the problem problem related to as I' incrementing a 32 bit address by incrementing the index of incomming then it moves four bytes ahead?, then bypassing the preciselly the suffix?. I'm sorry that you told me but I did'nt understood that part since I was trying to understand the packing of the structure. Thanks to your advice the problem is solved, I've changed the definition of the pointer to uint8_t and now the index of the pointer is incrementing in 8 bits steps. Now, about the architecture could you be more clear? – vram Dec 04 '20 at 22:33
  • I need to know everything I can. Lastly put your answer as an answer to give you the punctuation. – vram Dec 04 '20 at 22:34

1 Answers1

0
uint8_t analize_string(uint32_t * incomming)
{
    static uint8_t incCounter = 0;
    static uint8_t arreglo[Long_Rec] = {0};
    arreglo[incCounter] = incomming[incCounter];

arreglo is a uint8_t array while incomming is a pointer to uint32_t that is presumably the first element of an array.

One must always remember that pointer arithmetic and array indicies operate on the declare width of the data item, eg, the increment is only bytes if the width of the element is 1 byte. If it's four bytes, then the increment is four bytes.

As such each execution of

arreglo[incCounter] = incomming[incCounter];

Reads four consecutive bytes of the source (starting at the inCounter * 4th byte), but can only write the lowest order byte to the destination, almost certainly not what you want.

Just trying to fix that is unwise though, the whole architecture needs to be rethought from the start.

Chris Stratton
  • 33,282
  • 3
  • 43
  • 89
  • yes, preciselly thanks to your comment to the original post I changed the uint32_t * incomming for uint8_t * incomming and then it started to work well. Thank you very much for your help. – vram Dec 04 '20 at 22:41