trouble with sscanf and sprintf

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 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. 

0 Likes
9 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 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.

0 Likes
WaMa_286156
Level 5
Level 5
First comment on blog 100 replies posted 50 replies posted

  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.

0 Likes
Anonymous
Not applicable

 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.

0 Likes
JiGi_284761
Level 4
Level 4
Welcome!

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

0 Likes
JiGi_284761
Level 4
Level 4
Welcome!

Sorry. I Cant get the formatting right.

   

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

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
JiGi_284761
Level 4
Level 4
Welcome!

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.

0 Likes
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

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.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes