Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Convert String(received via EUSART) to Float

Status
Not open for further replies.

ahpan

New Member
Hi folks,

I have been trying to typecast the string to float so I can do the required maths on the received data from other device via EUSART of PIC18F45K22.

and one more thing how to detect the end of string when data receiving through other device connected to EUSART.

Your help is appreciated.

Cheers
 

Attachments

  • main.c
    6.1 KB · Views: 297
Does your math.h have an "atof" function? That should convert a string to float. If you don't have that in your library then you can google for "atof implementation".. Here is one result: https://crackprogramming.blogspot.fi/2012/10/implement-atof.html

What is the software/device sending the string? Is the value typed in by human? Is it a custom software written by you or just a terminal? If it is customs software/device, you really should send the data as raw binary, not as a text string.

To detect end of string, you have to send an ending character and detect that. Usually in C strings are terminated with null.. that is zero, or '\0' . Strings sent from terminal are considered terminated with a newline '\n'.
 
Last edited:
Thanks for your reply.

I am connecting a output of ADuC7061's RS232 output to PIC18F45K22's EUSART. The ADuC7061 sends a float value in string format. I am able to see that value on GLCD. However, the problem is that it is not showing the whole string which is 8 character long. e.g. 0002.34t . And this string is terminated by CR LF. I can see the whole string on PuTTY(serial emulator) via RS232 to USB converter.

Once I receive this string, I want to convert to Kilo Newton(kN) from tonne(t).

Cheers
 
What library and compiler are you using?
 
Ok, so you should have "atof" function available. You should read characters from the EUSART into a buffer until you receive the terminating CR LF. Then add '\0' at the end, just to be sure.. and pass the buffer string into the atof function.

Synopsis
#include <stdlib.h>
double atof (const char * s)

Description
The atof() function scans the character string passed to it, skipping leading blanks. It
then converts an ASCII representation of a number to a double. The number may be in
decimal, normal floating point or scientific notation.

Example
C:
#include <stdlib.h>
#include <stdio.h>
void
main (void)
{
    char buf[80];
    double i;
    gets(buf);
    i = atof(buf);
    printf(“Read %s: converted to %f\n”, buf, i);
}

Return Value
A double precision floating-point number. If no number is found in the string, 0.0 will be
returned.
 
Here is an example code:

C:
#include <stdlib.h>

volatile char buffer[20];
volatile char buf_index = 0;
volatile float recent_value;

void uart_interrupt()
{
    /* read character, and advance the index */
    buffer[buf_index] = get_received_character();
    buf_index++;
    
    /* Check if we received LF */
    if(buffer[buf_index-1] == '\n' ) {
        
        buffer[buf_index] = '\0'; /* Add terminating zero */
        recent_value = (float)atof(buffer); /* Convert ascii to float */
        buf_index = 0; /* reset the buffer index to zero */
    }
}
 
Code:
char* str;
char *p; 
double floatval,mod;

cUART_char = RCREG; //stores data to eusart receive buffer 
// Data received via EUSART
SetPos(30,20);
PutChar(cUART_char);

// Data Manipulation 
		
SetPos(30,30);
*p = cUART_char;
floatval = atof (p); //converting ascii to float
//printf("\n\r\t%i\r\n",floatval);  // float on EUSART
mod = floatval *9.80665; // multiply by 4.0
ftoa(mod,str); // converting float to ascii to display on GLCD
printf("\n\r\t%s\r\n",str); // string on EUSART
 		
while ( *str )  // Put whole String
{ 
	PutChar(*str);
	str++; 	
}

I have changed the code and I was able to see the effect of manipulation But suddenly stopped working. Can you tell me where's the problem ?

Cheers
 
You have not allocated any memory for your buffers:

char* str;
char *p;

Those are just pointers which point somewhere in the memory. You are just incrementing the "str" pointer therefore writing over all the memory until you run out. Define them this way so that actual memory is allocated.

char str[20]; /* Storage for 20 characters */
char p[20];


You also need to receive the full string before trying to convert it:

*p = cUART_char; /* This receives only one character */
floatval = atof (p); // You need to receive the full string. Take a look at my example. It shows how to receive the full string.



Use proper indexing in your loops. If you increment your pointer it is very difficult to come back to the start of the string again.

int i=0;
while ( str ) // Put whole String
{
PutChar(str);
i++;
}
 
Last edited:
Could you help me that how can I store the received 8 byte data and display whole string at once.

Code:
for (i=0; i < 8; i++)
	{
       while (PIR1bits.RCIF == 0)       //wait until there is a character ready
                ;       //do nothing
        buf[i] = RCREG; //copy received char into array at current index position
		//PutChar(buf[i]);
		delay();delay();
}		
		PutChar(buf); //display on GLCD

I am receiving only first 3 bytes.

Cheers
 
Now I can receive all the 8 bytes

Code:
for (i=0; i < 8; i++)
	{
        while (PIR1bits.RCIF == 0)       //wait until there is a character ready
                ;       //do nothing
        buf[i] = RCREG; //copy received char into array at current index position
		
}	
	SetPos(30,30); // display position
	for (i=0; i < 8; i++)
		{
		PutChar(buf[i]);  //print data on GLCD
		};

Have to convert this received data as @misterT suggested. Finger crossed.

Cheers
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top