4 Replies Latest reply on Apr 21, 2012 5:45 AM by eilrem.fernandez

    Writing a float value over UART


      char *k="This is a test\r\n";




      I am able to send over strings from the psoc over UART but  I cannot figure out how to send float values over the uart.


      I also tried to send integers by importing stdlib.h using the itoa function, but I get an error stating undefined reference to `itoa'.


      Any snippets of code or a link to the right information would be appreciated. Thanks

        • 1. Re: Writing a float value over UART

          range = RangeRead();
          /* Check the UART status */
          ch = UART_1_GetChar();

          if(ch == (unsigned int) 'a'){

          }else if(ch == (unsigned int) 'b'){

          }else if(ch == (unsigned int) 'c'){

          }else if(ch == (unsigned int) 's'){





          Also, how can I read a string or a word at a time, instead of receiving a character at a time? Sorry if this is too much for one post.

          • 2. Re: Writing a float value over UART

            You can use sprinf() to convert float to char string. Sample code can be found here: http://www.cypress.com/?id=4&rID=39791

            • 3. Re: Writing a float value over UART

              UARTs are often thought of to handle ASCII-characters only and so are used in a man-machine-conversation. But there is no constraint to stick to that rule, any uint8 may be sent (and received) via UART as long as you know what you are expecting.


              A 4-byte float can be thougt of as 4 individual bytes which can be transmitted and on the receiver side put together to form a float again. Commonly used for that is the unit-declaration in C which makes it possible to access the same memory-area as a float or as an array of bytes:


              union {


                   float f;


                  unsigned char ch[]4];


                  } C_Float;


              The benefit of do-it-yourself is SPEED. The number of characters sent via UART decreases and the CPU-Load for converting from one format to the other (and back) tends against zero. printf and scanf can use a lot of code due to their universality.


              The drwawback? Well, sprintf is written easier than the call to the home-brewed conversion.




              Happy coding



              • 4. Re: Writing a float value over UART

                Transmitting the binary data of a float is certainly faster. However, you have to go directly to the low level implementation since using a high level software interface might cause it to misinterpret certain binary data. A lot of characters with ASCII values below 32 have special meanings when transmitted via serial protocol.