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

    Writing a float value over UART

    phillip.tatti

      char *k="This is a test\r\n";
      UART_1_Start();
      UART_1_PutString(k);

         

       

         

      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
          phillip.tatti

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

          if(ch == (unsigned int) 'a'){
          setPin();
          UART_1_PutString(k);

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

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

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

          }
          }

             

           

             

          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
            eilrem.fernandez

            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
              user_1377889

              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

                 

              Bob

              • 4. Re: Writing a float value over UART
                eilrem.fernandez

                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.