Writing a float value over UART

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

cross mob
Anonymous
Not applicable

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

0 Likes
5 Replies
Anonymous
Not applicable

  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.

0 Likes
Anonymous
Not applicable

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Anonymous
Not applicable

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.

0 Likes
Anonymous
Not applicable

Hi,

i had the same problem.

The "Problem" is, UART commuincates byte for byte over ASCII.

So i did it this way. (Not very nice)

My Float value is not a float, it is an array with single char signs.

Float Value = 1.55;          // Float-Value

char arrayValue[4];          // Array for single Digits

arrayValue[0] = 1;

arrayValue[1] = .;

arrayValue[2] = 5;

arrayValue[3] = 5;

// Plot over UART

UART_PutString("My Value ' ' ");

for( i=0; i>=arraySize ; i++)

{

     UART_PutChar(arrayValue);

}

UART_PutString("' ' was a Float-Value.");

However you get your Float to an array, in my way this works well.

Greets

0 Likes