beginner question on string uart

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

i'm a beginner with not a strong knowledge in c-programming. So now from my USART i'm receiving character which i move them in BUFFER[10]. BUFFER[10] should then be compare with a string called 'START'. How do i do it :s

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This has nothing to do with AVRs or any other micro, this is pure C programming and knowledge of the standard C library, where you can find functions like strcmp, strncmp, and the likes... Google is your friend here, but again, it's not AVR specific!

-- Thilo

Viel Erfolg!

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oh, ok, i'll just google then :D

i think strcat() everytime i get a character will do the trick.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Assuming the trick you're searching is to compare a string of received characters against a constant string, then the answer to your search result with strcat is NO. strcat appends one string to another, your question was how to compare two strings. That's what's provided by str[n]cmp.

Some advice: get yourself familiar with a free C compiler that runs under your OS of choice (Windows, Linux, DOS...) and omit all the micro-specifics for a while. Then construct a basic program frame that accepts characters from the keyboard and does some string mangling, which you can easily watch via a debugger. Then, when you mastered the basics of the language, proceed to implement it on a micro.

-- Thilo

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since u mentioned that my post is irrelevant to this thread, thats why i didnt elaborate much, what i wanted to do with the strcat is to combine the received single character together to form a string and then compare it with another string. (but maybe it won't work, i haven't test this)

after a while of thinking, actually it might be easier if i just use a uart_gets() function which goes like this:

void UART_gets(char *p, int max)  {
	int n = 0;
	
	while (n < max) {
		*p = UART_getc();
		if (*p++ == '\r')  {
			break;
		}
		++n;
	}
	*p = 0;
}

then just compare my received string with my other string, of course using strcmp()

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If "max" is the size of the available space behind "p", like:

char str[10];
UART_gets(str,10);

you need to test "n < max-1". Otherwise there is no room left for the zero termination.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Since u mentioned that my post is irrelevant to this thread,

If you are programming an AVR with the GCC compiler then I personally don't see why your question would be off-topic.

The string functions you might want to use for string comparison include:

strcmp(); - compare two strings
strncmp(); - compare two strings, limit to n chars
strchr(); - find if chr appears in string
strstr(); - see if sub-string occurs in string
strtok(); - split string into parts (tokens).

As "START" is shorter than BUFFER[] I often find strstr() to be the most useful in this kind of circumstance. If you were to use (just for example):

if (strstr(BUFFER, "ST") != 0 ) {...

then

"ST"
"START"
" START"
" ST"
"DO START"
etc.

would all match. Though it might also pickup the "ST" part way through some completely different string - so maybe "STA" or even longer?

However "start" wouldn't - so you may also want to make use of strupr() to first convert the contents of BUFFER[] to all upper case:

if (strstr(strupr(BUFFER), "ST") != 0 ) {...

HTH