Reply by Dave Hylands October 16, 20092009-10-16
Check out the string functions available

On Thu, Oct 15, 2009 at 11:30 PM, David Kelly wrote:
>
> On Oct 15, 2009, at 7:15 PM, Dan Roganti wrote:
>
>> I took a look some more at the avr_libc, and I see there are several
>> string functions available. I'm just not entirely sure how to use them
>> yet. There aren't that many examples in avr-libc to show how to use
>> these. The one in particular is scanf for input strings and strncmp
>> for
>> comparing strings.
>>

...snip...

> A wonderful example of how NOT to write an AVR-class embedded
> application.
>
> scanf() and family are terrible resource hogs. One does NOT casually
> use floating point.

...snip...

Check out here for the string functions provided by the C runtime library


strtok is good for tokenizing a string (i.e. extract keywords)

I like strtol and strtoul for parsing numbers.

To get more detailed information about any of the runtime functions, I
like to use www.die.net and search using the function name. This gives
the glibc documentation, which avr-libc may not replicate 100%, but it
will mostly be right, and there are usually samples.

--
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/
Reply by STEVEN HOLDER October 16, 20092009-10-16
Dan,

As mentioned by others , scanf and printf are not a good way of dealling with this, they resource hogs, and given the use below can use a lot of ram if not carefully used. The functions are relatively slow as well.

I try to avoid floating point stuff like the plague on 8 bit systems like the avr mega range, the floating point libraries again are resource hogs and cumbersome, you need to be a bit more creative and use integer type maths , that way you won't need to the floating point libraries, functions will run quicker and if interrupts are around you will have less chance of corrupting data.

I tend to design systems that need to communicate while running, so they need to be quick in response, and modest in their resource requirements. The example below is exactly how i wouldn't do it.

I tried to send an example to your robot site e-mail address pghrobot@comcast.net but it has bounced back. Let me know an e-mail add i can get you on (i'm sure i had it before but i then i have lost it sorry) and i'll send you a method i regulary employ. Remember if you need to do any complex processing, let the pc do it, just get the "raw data" back from the microcontroller as quickly and efficiently as possible. Try not to send large "intrepreted strings " from the controller, like i say let the pc do the grunt work, thats what its for.

Have a look at the method employed in the files, like i say it is buffered data in a char array, the receiver is interrupt driven, but the transmitter is not (although you could do it in an interrupt manner and i generally do with RTOS/SCHEDULER TYPE SYSTEMS.

See what you think.

Regards
--- On Fri, 16/10/09, Dan Roganti wrote:
From: Dan Roganti
Subject: Re: [AVR club] Is there an example showing how to input strings ?
To: a...
Date: Friday, 16 October, 2009, 1:15 AM


I took a look some more at the avr_libc, and I see there are several
string functions available. I'm just not entirely sure how to use them
yet. There aren't that many examples in avr-libc to show how to use
these. The one in particular is scanf for input strings and strncmp for
comparing strings.

I found this demo program elsewhere online when I googled for tutorial
and scanf , I plan to try this
//---------- --------- --------- --------- --------- --------
#include

void main (void) {

/* We will use a floating-point and an integer variable. */

double x;
int n;

/* Read in an integer. */

printf("Please enter an integer: ");
scanf("%d", &n);
printf("The integer was %d\n\n", n);

/* Read in a double. */

printf("Please enter a double: ");
scanf("%lf", &x);
printf("The double was %g\n\n", x);

/* Read in an integer and a double. */

printf("Please enter an integer and a floating-point number: ");
scanf("%d%lf" , &n, &x);
printf("The numbers were %d %g\n", n, x);

}
//---------- --------- --------- --------- --------- --------

Is this what your using ?

=Dan

STEVEN HOLDER wrote:
>
>
> Dan,
>
> I have a standard code set for parsing string commands, it uses a
> buffered char set as there is no native "string" variable in c, rather
> an array of characters. I tend to use a serial protocol packet based
> system, will send you the files , see what you think.
>



Reply by David Kelly October 16, 20092009-10-16
On Oct 15, 2009, at 7:15 PM, Dan Roganti wrote:

> I took a look some more at the avr_libc, and I see there are several
> string functions available. I'm just not entirely sure how to use them
> yet. There aren't that many examples in avr-libc to show how to use
> these. The one in particular is scanf for input strings and strncmp
> for
> comparing strings.
>
> I found this demo program elsewhere online when I googled for tutorial
> and scanf , I plan to try this
> //------------------
> #include void main (void) {
>
> /* We will use a floating-point and an integer variable. */
>
> double x;
> int n;

[...]

A wonderful example of how NOT to write an AVR-class embedded
application.

scanf() and family are terrible resource hogs. One does NOT casually
use floating point.

Is my understanding that avr-libc does a respectable job of
implementing stdin, stdout, and stderr, but as a general rule I find
it best to avoid those as well.

Start by writing your own getchar() routine to read whatever it is you
expect input. Maybe its a keypad, maybe its a UART.

Then make your own gets() which reads getchar() in a loop. I strongly
suggest you don't pattern it after the C library gets() due to lack of
bounds checking. Perhaps something like this:

uint16_t my_gets( char *dest, uint16_t size, char eol )
{
int16_t c;
uint16_t count;

for( count = 0 ; count < size ; count++ ) {
dest[count] = 0;
c = my_getchar();
if( c == EOT )
break;
if( eol && ( c == eol ) )
break;
dest[count] = c;
}
return count;
}

--
David Kelly N4HHE, d...@HiWAAY.net
=======================================================================Whom computers would destroy, they must first drive mad.

Reply by Dave Hylands October 16, 20092009-10-16
Hi Dan,

> I found this demo program elsewhere online when I googled for tutorial
> and scanf , I plan to try this

Personally, I have a strong dislike for scanf. It doesn't do error
recovery very well.

I find using fgets to read in a line at a time, and then using sscanf
on the line tht was read in works much better for reporting errors and
recovering from errors.

--
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/
Reply by Dan Roganti October 16, 20092009-10-16
I took a look some more at the avr_libc, and I see there are several
string functions available. I'm just not entirely sure how to use them
yet. There aren't that many examples in avr-libc to show how to use
these. The one in particular is scanf for input strings and strncmp for
comparing strings.

I found this demo program elsewhere online when I googled for tutorial
and scanf , I plan to try this
//------------------
#include

void main (void) {

/* We will use a floating-point and an integer variable. */

double x;
int n;
/* Read in an integer. */

printf("Please enter an integer: ");
scanf("%d", &n);
printf("The integer was %d\n\n", n);
/* Read in a double. */

printf("Please enter a double: ");
scanf("%lf", &x);
printf("The double was %g\n\n", x);
/* Read in an integer and a double. */

printf("Please enter an integer and a floating-point number: ");
scanf("%d%lf", &n, &x);
printf("The numbers were %d %g\n", n, x);

}
//------------------
Is this what your using ?



STEVEN HOLDER wrote:
>
>
> Dan,
>
> I have a standard code set for parsing string commands, it uses a
> buffered char set as there is no native "string" variable in c, rather
> an array of characters. I tend to use a serial protocol packet based
> system, will send you the files , see what you think.
>

Reply by STEVEN HOLDER October 15, 20092009-10-15
Dan,

I have a standard code set for parsing string commands, it uses a buffered char set as there is no native "string" variable in c, rather an array of characters. I tend to use a serial protocol packet based system, will send you the files , see what you think.

Regards
--- On Thu, 15/10/09, Dan Roganti wrote:
From: Dan Roganti
Subject: [AVR club] Is there an example showing how to input strings ?
To: a...
Date: Thursday, 15 October, 2009, 2:55 AM


I'm using WinAVR and trying to learn how to input strings. I'm using a
ATmega32 controller with the serial port connected to my PC where I have
a terminal program that I use to communicate with the micro. I already
have several tests written for this which I send data and text to the
terminal. But I like to be able to enter a string for a command- much
like a command shell- and parse this so it can determine what action to
take next.

Is there an example showing how to input strings ?

thanks
=Dan



Reply by Dan Roganti October 14, 20092009-10-14
I'm using WinAVR and trying to learn how to input strings. I'm using a
ATmega32 controller with the serial port connected to my PC where I have
a terminal program that I use to communicate with the micro. I already
have several tests written for this which I send data and text to the
terminal. But I like to be able to enter a string for a command- much
like a command shell- and parse this so it can determine what action to
take next.

Is there an example showing how to input strings ?

thanks