|
|
View previous topic :: View next topic |
Author |
Message |
Channing
Joined: 23 Jun 2006 Posts: 2 Location: Canton, MS
|
Need to combine multiple chars to string then float |
Posted: Fri Jun 23, 2006 3:00 pm |
|
|
I have a program that reads in data from a digital protractor via serial. I already have the receive functions setup and working properly. The protractor sends the angle one 'number' at a time, i.e. 123.45 degrees is sent as a 1, then a 2, then a 3, then a ".", then 4, then 5, and I have each number assigned to a variable, ang_hun_bit, ang_ten_bit, and so on.
What I need to do is convert these chars to a useable number to average and check.
I know there is the atof() function to convert a string to a float, but I can't get the chars combined to one string. Here is my code so far:
[/code]
float parse(char *hun, char *ten, char *one, char *tenth, char *hundth)
{
char data_string[8];
float runner;
char *ptr;
// ptr = data_string;
ptr = strcpy(data_string, hun);
ptr = strcat(data_string, ten);
ptr = strcat(data_string, one);
ptr = strcat(data_string, decimal);
ptr = strcat(data_string, tenth);
ptr = strcat(data_string, hundth);
lcd_gotoxy(1,2);
printf(lcd_putc,"%6s", ptr);
runner = atof(ptr);
// lcd_gotoxy(1,2);
// printf(lcd_putc,"%f", runner);
average += runner;
avg_counter++;
return runner;
}[/code]
I am passing all of the individual numbers of the measurement sent by the protractor into this function. The pointers are working correctly as I can print each indiviudally via printf(lcd_putc, "%c%c%c.%c%c", [variable list].
Thanks for your help[/code] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 23, 2006 3:49 pm |
|
|
You're treating each incoming character as if it's a separate string.
I really doubt that this is how the instrument sends the data.
Can you post a link to the unit's manual, which describes the protocol
in detail ? |
|
|
epideath
Joined: 07 Jun 2006 Posts: 47
|
|
Posted: Fri Jun 23, 2006 3:59 pm |
|
|
One way that you could accomplish this, if you always get charaters for each digit sould be to do something like this:
Code: |
char data_string[8] = " "; // 7 spaces allowing 1 for the 0x00
data_string[0] = hun;
data_string[1] = ten;
data_string[2] = one;
data_string[3] = '.';
data_string[4] = teth;
data_string[5] = hundrth;
data_string[6] = 0x00;
runner = atof(data_string);
|
but this would be better if you did it in the function where you are actually reading the data in.
you could do something like:
Code: |
for(i=0; i<6; i++)
data_string[i] = read_char();
data_string[6] = 0x00;
|
There are many other ways to accomplish this using the strcpy and strcat functions also.
hope this helps |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Jun 23, 2006 9:37 pm |
|
|
Once I had a similar problem with a magnetic compass. The problem was that the output
string doesn�t keep the same size, instead it depends of the value.
Typical output string format was as follow:
Code: |
<sign> XXX.XX <CR> <LF>
+ 123.40 <CR> <LF>
- 12.30 <CR> <LF>
+ 5.45 <CR> <LF>
|
Normally to cover the full 360 degree, the range and the readout is between -180 deg. and +180 deg.
You must store the incoming chars in a buffer, then in main the first task is to know the "size" of
the incoming value, I solved it looking where was stored the delimiter 'point', (I mean in wich buffer position.)
Also you can use the atof() function to convert a string to a float, to compare if it generate a shorter code,
but this is your job.
NOTE: Following code not tested, just to show you how I did it.
Code: |
#define point '.'
#define plus '+'
#define minus '-'
int8 hun, ten, one, teth, hundrth;
char start_of_string;
float Ftotal;
start_of_string = FALSE;
if((data[0] == plus) || (data[0] == minus))
{
sign = (data[0]; // always the first char is the sign
if(data[4] == point)
{
hun = (data[1] & 0x0F) * 100 ; // max expected value is 100
ten = (data[2] & 0x0F) * 10;
one = (data[3] & 0x0F);
total_integer = (hun + ten + one);
teth = (data[5] & 0x0F);
(float) teth = (float) teth / 10.0;
hundrth = (data[6] & 0x0F);
(float) hundrth = (float) hundrth / 100.0;
}
if(data[3] == point)
{
ten = (data[1] & 0x0F) * 10;
one = (data[2] & 0x0F);
total_integer = ( ten + one);
teth = (data[4] & 0x0F);
(float) teth = (float) teth / 10.0;
hundrth = (data[5] & 0x0F);
(float) hundrth = (float) hundrth / 100.0;
}
if(data[2] == point)
{
one = (data[1] & 0x0F);
total_integer = one;
teth = (data[3] & 0x0F);
(float) teth = (float) teth / 10.0;
hundrth = (data[4] & 0x0F);
(float) hundrth = (float) hundrth / 100.0;
}
(float) Ftotal = ( (float) total_integer + (float) hundrth + (float) teth);
}
|
Hope you understand the idea.
Humberto |
|
|
Channing
Joined: 23 Jun 2006 Posts: 2 Location: Canton, MS
|
Thanks guys |
Posted: Mon Jun 26, 2006 8:34 am |
|
|
I just simply assigned the individual pointers to each element of the array, and now I have 100% precision (or as close as I am going to attempt to get).
As they always say, sometimes is just take a second opinion on code, to step away and come back.
Thanks again guys for the help. I am sure I will be back for more help with future projects. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|