View previous topic :: View next topic |
Author |
Message |
young
Joined: 24 Jun 2004 Posts: 285
|
Are there same function as printf to receive data? |
Posted: Thu Aug 12, 2004 6:23 am |
|
|
printf("data: %f %i",a, b) send data to RS232 port, on the other side, I receive the sended data as ASCII data, How I can interpret them into a float and a int data. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Aug 12, 2004 6:36 am |
|
|
The C function for that would be scanf(). I am not sure if CCS has this function. You will just have to look to see. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Aug 12, 2004 6:43 am |
|
|
There are also other functions to convert strings to number
atoi()
atol()
...
See the manual for more |
|
|
Ttelmah Guest
|
Re: Are there same function as printf to receive data? |
Posted: Thu Aug 12, 2004 6:44 am |
|
|
young wrote: | printf("data: %f %i",a, b) send data to RS232 port, on the other side, I receive the sended data as ASCII data, How I can interpret them into a float and a int data. |
In normal C, the 'inverse' function, is sscanf. The problem is that as you will have allready found, if you look at the code sizes generated by printf, 'general purpose' functions, tend to be bulky. There have been some implementations of sscanf published in the past, but generic versions tend to be large. However if your string is in a relatively simple 'known' format (which your question suggests), then the answer is to use the standard string functions, then search the string for the character that seperates the two numbers, take the section in front of this, and use the STRTOD function to convert this to a float, then use the STRTOL or STRTOUL function on the rest of the string, to change this into a (long) integer.
Best Wishes |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Aug 12, 2004 6:48 am |
|
|
It is nt defined in CCS, so any other ways? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Aug 12, 2004 6:53 am |
|
|
Are you handling a single case or do you require a general purpose function to handle multiple cases. For a single case (or just a case) Ttelmah suggestion is the way to go. If you can afford the overhead and require multiple cases, then implement the sscanf() yourself. I have written a sscanf() for the C18 compiler. It uses variable arguments which I don't think CCS supports. I could probably change it around a bit to work with CCS but search the forum and see if someone has already posted one. If not, then maybe I'll put one in the code section. |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Aug 12, 2004 7:02 am |
|
|
Thank you Mark:
A special case solution will not be a bad idea for current use, but if you developed any general purpose function, it will be great, Waiting for your code! |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Aug 12, 2004 7:32 am |
|
|
Hi Ttelmah:
Do you have any code that separate the string into different part?
thank you |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Aug 12, 2004 7:38 am |
|
|
To find a string in a string CCS has a function strstr()
To break up a string based on tokens use strtok() |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
gets() |
Posted: Tue Aug 17, 2004 2:03 pm |
|
|
I would use:
Code: |
gets(floater);
gets(interger);
atoi(interger);
atof(floater);
|
|
|
|
|