User`s manual

152 digi.com File Systems
The next block of code appends the latest entry into the file that was opened at the start of the application.
Again, waitfor is used to voluntarily relinquish control, this time while waiting for the write function to
complete. If an error occurs during the write operation the device is unmounted and the application exits.
Otherwise the loop counter and the buffer position pointer are advanced by the number of bytes actually
written. Since this can be less than the requested number of bytes, it is best to check in a loop such as the
while loop shown in putdata.
The last action taken by putdata is to reset filestate, indicating that the open file is available.
10.2.3.2.2 Costatement that Reads and Displays a File
The costatement named showdata waits for ten seconds. Then it waits for the open file to be available,
and when it is, immediately marks it as unavailable.
The next statement modifies the internal file position pointer. The first time this costate runs, readto is
zero, meaning the position pointer is at the first byte of the file. The variable readto is incremented
every time a record is read from the file, allowing showdata to always know where to seek to next.
The rest of showdata is a while loop inside of a while loop. The inner while loop is where each
record is read from the file into the buffer and then displayed in the Stdio window with the printf()
call. Since fat_Read() may return less than the requested number of bytes, the while loop is needed
to make sure that the function will be called repeatedly until all bytes have been read. When the full record
has been read, it will then be displayed to the Stdio window.
waitfor (filestate == 0); // Wait until file is available
filestate = 1; // Show file is being updated
while (ocount < REC_LEN){ // Loop until entire record is written
waitfor((rc = fat_Write(&file, optr, REC_LEN - ocount))!= -EBUSY);
if (rc < 0){
printf("fat_Write: rc = %d\n",rc);
while ((rc = fat_UnmountDevice(first_part->dev)) == -EBUSY);
return rc;
}
optr += rc; // Move output pointer
ocount += rc; // Add number of characters written
}
filestate = 0; // Show file is idle
}
costate showdata always_on{
waitfor (DelaySec(10));
waitfor (filestate == 0);
filestate = 2;
waitfor (fat_Seek(&file, readto, SEEK_SET) != -EBUSY);