9 Replies Latest reply on Feb 15, 2015 8:23 AM by jim.gilmartin

    trouble with sscanf and sprintf

    dennis.nguyen

       Hi there, 

         

      I got a string from TeraTerm: numbuf and want to convert it to set_voltage (define as float) , so I use sscanf function:

         

      sscanf(numBuf, "%f", &set_voltage)

         

      Then I want to print that set_voltage to the LCD so I use sprintf to convert it to ASCII string resultbuf (define as char resultbuf[6]) :

         

      sprintf (resultbuf, "%3.1f", set_voltage);

         

      then use

         

      LCD_PrintString(resultbuf); to the LCD.

         

      but I got some wierd characters on the LCD.

         

      Can anyone explain to my why?

         

      I just want to print a float number to LCD. 

        • 1. Re: trouble with sscanf and sprintf
          user_14586677

          This might help =

             

           

             

              

             

                    

             

          http://airsupplylab.com/index.php/learn/14-psoc5/9-psoc-floating-point-problem-in-psoc-creator-3 nano library  enable

             

          http://www.cypress.com/?id=4&rID=87354     nano library enable

             

           

             

           

             

          Regards, Dana.

          • 2. Re: trouble with sscanf and sprintf
            dennis.nguyen

             Thanks Dana.

               

            I were actually aware of that setting but it seems doesnt work for me. I think I have a problem with sscanf and sprintf syntax. I post my project here, hope someone can help me find out the problem.

               

            I got the warning " passing argument 1 of "Display_PrintString" makes pointers from interger without a cast" at PrintString function.

               

            Then a " cast a pointer to interger of different size" at PrintResult  function.

            • 3. Re: trouble with sscanf and sprintf
              user_463759797

                I looked at your source code, and have a few comments.

                 

                In printResult( (uint8)resultBuf);   you are changing the char * result buffer to an 8 bit integer.

                 

                Change the definition of printResult( uint8 resultBuf ) to be printResult( uint8 *resultBuf) and all the other calls use (uint8*) rather than (uint8) to cast the buffer.

                 

                 You are forcing the C compiler to point to lower memory for the data, rather than to the actual resultBuffer by leaving the * (pointer) off of your cast.  You are probably printing either variables or vectors as a string to your display since an 8 bit integer points either really low (starting at 0 to 256) or really high (if sign extended, from the top 256 bytes).

                 

                Also, I always like larger buffers than I'll ever use, especially if on the stack, as an overflow can lead to trouble.  Maybe make numBuf, resultBuf, and usbBuffer static?  that takes them off the stack and helps in overflow conditions.  However, that is not your problem here, from what I can tell.  If you do make them static, you'll need to increase your heap size, most likely.

              • 4. Re: trouble with sscanf and sprintf
                dennis.nguyen

                 hi wsm,

                   

                I basically just want to convert the numBuf string to a float (set_voltage) by sscanf, and use that set_voltage number to process some data and will result a nother float number(resultBuf), then print it to the LCD. 

                   

                to simplify the process, I didnt do any prossessing on that set_voltage and just print it straight out to the LCD. But somehow cannot get anything. So I suspect either the sscanf function is not working or the sprintf is not working. 

                   

                should I declare another temp variable to get the value out of the set_voltage address. and use that temp variable as the input for the sprintf?

                   

                Cheers.

                • 5. Re: trouble with sscanf and sprintf
                  jim.gilmartin

                  How do you plan to convert chars to ints? If using ints. first convert the 8 to 16. Then cast to float32. Then use sprint to convert to char.

                     

                   

                     

                   See attached screenshot

                  • 6. Re: trouble with sscanf and sprintf
                    jim.gilmartin

                    Sorry. I Cant get the formatting right.

                       

                    Anyone have an idea on how to post code so that it looks like code??

                    • 7. Re: trouble with sscanf and sprintf
                      user_14586677

                      The forum software has "issues", a lot of things that do

                         

                      not post correct, etc. Best way is to post a project archive.

                         

                       

                         

                          

                         

                                

                         

                      “File”                                                             Creator

                         

                      “Create Workspace Bundle”

                         

                       

                         

                       

                         

                      Attached, formatting string help for printf/sprintf

                         

                       

                         

                      Regards, Dana.

                      • 8. Re: trouble with sscanf and sprintf
                        jim.gilmartin

                        I think you are trying to make characters float...

                           

                        First convert the chars to uint8. remember a 0 int is 0x00h but a 0char is 0x30h so if you try to print a o int as a char you will get a funny looking square (Null).

                           

                        Then convert the uint8's into uint16's

                           

                        Then cast the uint16's into a float32.

                           

                        Then use sprint to break it up into chars that the lcd can display properly.

                           

                        Functions are located in the main.c in the attached bundle.

                        • 9. Re: trouble with sscanf and sprintf
                          user_119654

                          The %f descriptor in sscanf is not working in the newlib-nano version of lib.c.  Turn off newlib-nano and use the standard lib.c.  It works.  Sad part is the the standard lib.c is much larger than the nano version.