LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to change types of variables, array of char to numbers and back to array of char?

Hello,
Im using LabWindows to acquire data from the serial port.
My serial port is getting the following data:
A30<cr>
B540<cr>
C1023<cr>
D78<cr>
The letters are to indicate which sensor is sending the data and the number is the data itself. The numbers will vary in time going from nothing (  ) to 1023, it doesnt get zeros. Im using the carriage return to identify the end of a data, thats the <cr>.
The problem is:
labview put this data after the ComRdTerm in a array of characters, so if my variable is defined as "char escrita[20]" the value of escrita after reading the port would be "A30". I need to work in the value "30" as number, so how can i put that value in a variable of type integer? The value "A" can be lost. After working with the number i need to put it back as a array of char so i can write it in a textbox, im using the command Update Textbox to show the number on screen.
Another problem:
When i use the command ComRdTerm, the carriage return (ASCII value 13) goes in the variable or not?
If it goes then the value of escrita would be "A30<cr>"?

Thanks,
Sandro
0 Kudos
Message 1 of 13
(5,248 Views)
Just complementing the above.
How would be if i want the array of char to change to type floating point instead of a integer and then back to array of char?

0 Kudos
Message 2 of 13
(5,245 Views)
You might use the C standard functions sscanf()/sprintf() or the functions  Scan()/Fmt() from NIs Formatting and IO Library.
0 Kudos
Message 3 of 13
(5,244 Views)
Thanks for the reply, but i have no idea of how to use those commands. Can you write a little example using them?
0 Kudos
Message 4 of 13
(5,239 Views)

OK, to read the number from the buffer you may use

sscanf (readBuffer + 1, "%d", &number);   // This fills an integer variable with your data, skipping initial character

sscanf (readBuffer + 1, "%f", &number);    // This read back in a double variable

To create a string with your data to show in the text box, simply use the following:

sprintf (buffer, "%d", number);    // Writes the first number

sprintf (buffer, "%s\n%d", buffer, number);    // Writes the 2nd (3rd, 4th...) number to the string

In the above statements "%d" can be substituted with "%f" for floating points numbers.

 

As an alternative, you may want to look of the examples in the online help, looking into the "Formatting and I/O library" chapter of the user guide: these will drive you in the use of Scan and Fmt native functions.

 

sscanf and sprintf are very big and complex commands that can be customized to fulfill your needs: their explanation goes beyond the scpe of this forum and I strongly reccommend that you study them on a c-language manual in order to use them at the best.

Message Edited by Roberto Bozzolo on 11-28-2005 09:44 PM



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 13
(5,240 Views)

You might find it easier to use:

number = atoi (readBuffer + 1);

Noting that number is an integer, which you can copy to a float/double type variable later, if you prefer.

JR

0 Kudos
Message 6 of 13
(5,226 Views)
Thanks, that solved the problem. Actually i used the atof command but it works the same way, simple and easy.
Now i have to return the floating point value to an array of char, do you know what is the command for that?
0 Kudos
Message 7 of 13
(5,205 Views)

As Roberto suggested, you will need the sprintf() function. Something like:

char string [32];
float number = 1234;

sprintf (string, "%f", number);  // "%g" is an alternative if there is never a fractional part

There is a wide variety of formatting options, such as: number of decimal places; forced sign inclusion; engineering/scientific notation etc. You may need to read up on all the options for the function in a good ANCI C text book.

JR

0 Kudos
Message 8 of 13
(5,191 Views)
Smiley Happy

It worked, thanks.
Now the last and simpler question, how do i round the number?
Example:
I have the number 24.932743539
I wanto to round it to 24.9
only 1 decimal.

And just to check, the sprintf didnt show any results in screen, is that right, if it is then good thats the way i need it.
0 Kudos
Message 9 of 13
(5,179 Views)
You can use the floor() function in the ANSI C library to round a number. If you just wanted to round it to nearest whole number, you would do this:

    newNumber = floor (oldNumber + 0.5);

But since you want to round it with one decimal point, you can do this instead:

    newNumber = floor (10 * oldNumber + 0.5) / 10;

Luis
NI



0 Kudos
Message 10 of 13
(5,177 Views)