Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length



Pages: [1]
  Print  
Author Topic: problem after GHI_WriteFile() command  (Read 928 times)
Mikhail617
Newbie
*
Posts: 11


View Profile
« on: January 06, 2010, 10:43:54 AM »

I am working on a gps data logger. Started with a combination of the code from SerialTest and MS_test examples. The program stops reading data after I send the GHI_WriteFile() command and returns "cannot read data" when I use the following code:

int8 SerialTest(void)
{
   char gps_data_buffer[128];
   int8 file_name[12]="log.txt\0";
   int32 var32, bigsize;
   int8 device, c, size;
   int32 baudrate;

   device = '0';
   baudrate = 4800;
   c = 0;

   GHI_RegisterSerialDevice(device, SERIAL_PROLIFIC1_DEVICE, baudrate);
   
   while(1)
   {
      if(c = GHI_SerialRead(device, &size))
      {
         printf(ROM_TYPE"Cannot read data\r");
         return c;
      }
      while(size)      
      {
         while(BusyUSART());
         gps_data_buffer[0-size+128]=GHI_GetC();
         size--;
      }
      if(size==0)
      {
         bigsize=strlen((char*)gps_data_buffer);
         var32=strlen((char*)gps_data_buffer);
      
         printf(ROM_TYPE"%s\r", gps_data_buffer);   //let's see what's in the buffer (for debug)

         GHI_MountFATFileSystem(SD_DEVICE);
         if (c = GHI_OpenFile(FILE_HANDLE_0, file_name, FILE_WRITE_MODE))
         {
            printf(ROM_TYPE"Cannot open %s\r", file_name);
            return c;
         }
         if (c = GHI_WriteFile(FILE_HANDLE_0, gps_data_buffer, bigsize, &var32))
         {
            printf(ROM_TYPE"Could not write to the log\r");
            return c;
         }
         if(c = GHI_CloseFile(FILE_HANDLE_0))
         {
            printf(ROM_TYPE"Cannot close %s\r", file_name);
            return c;
         }
      }
      if(c = GHI_GetResult())
      {
         printf(ROM_TYPE"Error reading data\r");
         return c;
      }
   }
}

#endif   //  _INCLUDE_SERIAL_DEVICES_SUPPORT_
 
I can see that I am getting data from the gps mouse when i print the contens of the gps_data_buffer. If I comment out the writeFile command, it continues to retrieve data from the gps and printing the contennts so I know that something goes wrong durring the WriteFile although if I simply do 'GHI_WriteFile(...)' without checking for errors, it will log one instance of the data to the the SD card and then go into the "cannot read data" routine.  Any hint/clues/advice/suggestions would be greatly appreciated.

Thanks in advance,

~Mike
Logged
SupportAdmin
Administrator
Hero Member
*****
Posts: 5394



View Profile WWW
« Reply #1 on: January 06, 2010, 12:53:31 PM »

try something simpler..write one byte to a file...if that works then move on to your code
Logged
Mikhail617
Newbie
*
Posts: 11


View Profile
« Reply #2 on: January 06, 2010, 03:15:12 PM »

I am able to write a byte or a string to the SD card once but after that the program gets stuck. When I step through it with ICD2, it looks like it is stuck in the GHI_DataIsReady(void)...
Logged
SupportAdmin
Administrator
Hero Member
*****
Posts: 5394



View Profile WWW
« Reply #3 on: January 06, 2010, 03:41:00 PM »

This happens when send a command to write 2 bytes but then only one is transmitted
Logged
Mikhail617
Newbie
*
Posts: 11


View Profile
« Reply #4 on: January 06, 2010, 04:40:45 PM »

just by some quick commenting out to isolate the problem, as of right now, it looks like the 'serial read' command will not coexist with any kind of write command in an infinite loop. not sure why it happens yet or how to work around this...
Logged
Mikhail617
Newbie
*
Posts: 11


View Profile
« Reply #5 on: January 07, 2010, 05:30:16 PM »

okay I really can't get much simpler than this:

GHI_SerialRead(device, &size);
      while(size)     
      {
         while(BusyUSART());
         gps_data_buffer[0-size+128]=GHI_GetC();
         size--;
      }
         bigsize=strlen((char*)gps_data_buffer);
         var32=strlen((char*)gps_data_buffer);
GHI_MountFATFileSystem(SD_DEVICE);
GHI_OpenFile(FILE_HANDLE_0, file_name, FILE_WRITE_MODE);
GHI_WriteFile(FILE_HANDLE_0, gps_data_buffer, bigsize, &var32);
GHI_CloseFile(FILE_HANDLE_0);

It successfully logs the data in the buffer in the first cycle and then stops reading data, WHY?!?!?!?

When I step through the code, it looks normal to me but it won't read any more data after the first cycle. It reads the data continuoulsly with no problem if I comment out the writecommand. What is the conflict between this serial read routine and the write file cycle?Huh?

Please help...
Logged
SupportAdmin
Administrator
Hero Member
*****
Posts: 5394



View Profile WWW
« Reply #6 on: January 07, 2010, 08:30:51 PM »

Yes sure it can be much simpler and you should have it simpler to make sure the routines are good to go!

Start with a fixed simple array with one or 2 bytes and write them...read them...etc. why strlen? go simpler...much simpler!

int size=0;
byte b[2];
main()
{
OpenFile(.........)
b[0]='G';
b[1]='S';
WriteFile(0,b,2,&size);///all fixed constant number here...do not use variables for now
}
...now this is simple Smiley
Logged
GHI_Support
Administrator
Hero Member
*****
Posts: 1252


View Profile
« Reply #7 on: January 08, 2010, 09:25:35 AM »

It very unlikely that there is a conflict. Something is getting messed up at some step.
You should always check error codes back from each function.
One thing is after GHI_SerialRead() and reading data, you should call GHI_GetResult, see library.
Logged
Mikhail617
Newbie
*
Posts: 11


View Profile
« Reply #8 on: January 12, 2010, 09:34:52 AM »

I still haven't figured this out. Simple programs work fine but when I try to log buffered GPS data, I'm getting the same result. There is some pressure on me to get this feature working ASAP. We have been using the USBwiz for a while and are looking to purchase another 100+ units in the near future. I was hoping that someone could take a closer look at this with me. I am sure that there is a simple solution that should be a no-brainer for someone that knows the system intimately.
Logged
GHI_Support
Administrator
Hero Member
*****
Posts: 1252


View Profile
« Reply #9 on: January 12, 2010, 10:13:33 AM »

Are you checking error codes back?
Did you add GHI_GetResult()?
The way to investigate this is to start as simple as possible and one thing at a time until it breaks. At that point we can know what is going on.
It should be things missing like "GHI_GetResult()" or telling USBwiz that you will write 10 bytes, byte you write only 8...
Logged
Mikhail617
Newbie
*
Posts: 11


View Profile
« Reply #10 on: January 12, 2010, 11:06:34 AM »

I added the GHI_GetResult() command and it no longer stops reading data! It now reads data continuously which I can tell by printing the contents of the gps_data_buffer. However, it is writing over the previously written data on each cycle. When I stop the program and check the contents of the SD card, the log only shows the last instance of data. How can I fix it to keep logging continuously like the example does?
Thanks.
« Last Edit: January 12, 2010, 11:23:00 AM by Mikhail617 » Logged
SupportAdmin
Administrator
Hero Member
*****
Posts: 5394



View Profile WWW
« Reply #11 on: January 12, 2010, 11:47:34 AM »

You can't just a function randomly, instead step in your code and see what data you receive and what you send to USBwiz...we can guess all day but the only to fix this is by you stepping an code and analyzing the commands/responses...etc.  Smiley
Logged
Mikhail617
Newbie
*
Posts: 11


View Profile
« Reply #12 on: January 12, 2010, 11:51:48 AM »

I just got it to work  Grin

I did not realize that opening a file in WRITE mode will erase the previous contents! Now, I open in APPEND mode and everything works fine!!!

Thanks.
Logged
Pages: [1]
  Print  
 
Jump to: