Search code examples
cfilemicrocontrollerpicpic32

Read from a certain part of a file PIC32MX


I am using PIC32MX in a design made by myself and everything is working perfectly.

Now I am trying to implement a feature which is basically read from a file sequentially until I found a certain frame of characters, so I am doing:

    while( (readedBytes = FSfread((void *)&c,sizeof(char),1,ephpubData->filetouart) != 0) && G_RUNNING && ephpubData->readedBytes < 2520){
    privData->txBuffer[privData->txBufferPos++] = c;
    ephpubData->readedBytes = ephpubData->readedBytes + readedBytes;
    if (privData->txBufferPos == TX_BUFFER_SIZE){
        if (verifyDate (task) == 1){
        *gpsState = GPS_STATE_VERIFY;
        ephpubData->count++;
        break;
        }
        FSfseek(ephpubData->filetouart , ephpubData->readedBytes , SEEK_SET);
        privData->txBufferPos = 0;
   }
}

For the first time when it finds the frame (using the verifyDate function) everything is ok and it goes to break sentence. When it comes to read the second time in the while loop (after close/reopen the file and doing other things in the code) it goes to the first position again. So I want to save the the latest position found until the break sentence. I already tried to use the seek function for every while iteration

    while( (readedBytes = FSfread((void *)&c,sizeof(char),1,ephpubData->filetouart->seek) != 0) && G_RUNNING && ephpubData->readedBytes < 2520)

but it gave me an error.


Solution

  • Sorry folks but I found the bug.

    Somewhere in the code I was doing:

        FSfseek( ephpubData->filetouart , 0 , SEEK_SET );
    

    Which sets the reading position to 0.