0
\$\begingroup\$

I'm using the PIC24 microcontroller and I'm using a Thumb drive to save information from the microcontroller USB transmitter and receiver pins. The data is saved in the Thumbdrive using the '.CSV' file format. The following is the data collected:

3/19/2018    12:10  35      
3/19/2018    12:10  7   
3/19/2018    12:10  7   
3/19/2018    12:10  7       
3/19/2018    12:10  19      
3/19/2018    12:10  30      
3/19/2018    12:10  37      
3/19/2018    12:10  44  
3/19/2018    12:10  45      
3/19/2018    12:10  48      
3/19/2018    12:10  58      
3/19/2018    12:10  67      
3/19/2018    12:10  69      
3/19/2018    12:10  79      
3/19/2018    12:10  90      
3/19/2018    12:10  96      
3/19/2018    12:10  99  
3/19/2018    12:10  110     
3/19/2018    12:10  USBSÅV43/19/2018     12:10  126
3/19/2018    12:10  130     
3/19/2018    12:10  140     
3/19/2018    12:10  152     
3/19/2018    12:10  155     
3/19/2018    12:10  159     
3/19/2018    12:10  168     
3/19/2018    12:10  182     
3/19/2018    12:10  184     
3/19/2018    12:10  193 
3/19/2018    12:10  208     
3/19/2018    12:10  218     
3/19/2018    12:10  222     
3/19/2018    12:10  230     
3/19/2018    12:10  245     
3/19/2018    12:10  256     
3/19/2018    12:10  256     
253/19/2018  12:10  256     
3/19/2018    12:10  256     

As you can see, the first column is for the date(mm/dd/yyyy), the second column is for the time(hh:mm) and 3rd column is for the digital value. In one of the rows, I got a ADC value starting with 'USBS'. Then in the second last row, the month in the 1st column was noted as 253 instead of just 3. Why am I getting garbage values and how can I get rid of it?

The following is the line of code that allows me to write the data on the thumb drive:

charCount = sprintf(printBuffer, "%d/%d/20%d, %d:0%d,  %d, %d\r\n" , month, date, year, hour, minute, ADC);
\$\endgroup\$
7
  • 1
    \$\begingroup\$ No, that's the line that assembles the results for writing onto the drive. The actual code for writing is much larger. And it has a bug. \$\endgroup\$ Mar 20, 2018 at 23:12
  • 2
    \$\begingroup\$ the reason that you are getting the "USBS" is because your program has written it .... since you did not include the program code, it is impossible to guess which part of the program has caused the faulty data to be written \$\endgroup\$
    – jsotola
    Mar 20, 2018 at 23:17
  • \$\begingroup\$ Yes, I'm just trying to assemble the the data from the code, but I was just wondering why random data is added to the .csv file. The striking part is that it occurs irregularly. \$\endgroup\$
    – J2018
    Mar 20, 2018 at 23:17
  • 1
    \$\begingroup\$ don't be asking us until you provide the program listing .... -1 \$\endgroup\$
    – jsotola
    Mar 20, 2018 at 23:18
  • 1
    \$\begingroup\$ Aside from the fact that the “CSV” that you posted contains no Cs (and doesn’t match the code that you provided, as pointed out by others already), and ignoring the fact that your code contains more specifiers than arguments, the minutes field will not produce the results you’re after. (Phew, I almost got lost in that run-on sentence.) If minutes is 12, for example, your code will write “012”. You want “%02d” instead. \$\endgroup\$ Mar 21, 2018 at 3:22

1 Answer 1

1
\$\begingroup\$

One really obvious bug is that your format string specifies 7 data fields, but you're only supplying 6 additional arguments. sprintf() (and friends) will happily read whatever garbage is on the stack and put it into any unspecified fields.

Also, note that you aren't showing us the contents of the actual CSV file; you're showing us the result of passing the file through some other program, which is applying its own interpretation to what it's finding there.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.