Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Serial Port String comparison and action

Status
Not open for further replies.

RyanHan

Junior Member level 3
Joined
Apr 24, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,514
Dear all expert,

I had setup the serial port of my MCU and its working fine. I am storing whatever I had recieved into a data buffer called

Code:
unsigned char DATA[30];

- - - Updated - - -

I am waiting for my serial port to send a string called "GAME START" and I will do some processing to it. My serial port will received other command like "GAME ENDED" and therefore I need to compare the exact string to do different coding.

Can I know what are the best way or easier way to compare the serial port string using microcontroller C programming.
Hope all expert can help. Thanks a lot
 

If those strings (like "GAME START") are not to be changed, then you could store them in flash memory. Then when a string is arrived you could compare character by character if it is the same. If you find out that even one character is not the same, then the comparison has failed. Else it succeeded.

Let's assume two strings, str1 and str2. You could then write a code as follows:

Code:
unsigned char failed = 0;
for(i = 0; i < _STRING_SIZE; i++)
{
  if (str1[i] != str2[i])
  {
    failed = 1;
    break;
  }
}

From fail flag you will know after the for loop if the two strings are matched or not. _STRING_SIZE is supposed to be a defined value.

There is also the strcmp() option, but it seems it is not convenient for your case.
https://www.thinkage.ca/english/gcos/expl/c/lib/strcmp.html


Hope it helped,
Alexandros
 

Hi alexxx,

Thanks for your prompt reply. I understand your program. However, GAME START and GAME ENDED is just nice having the same character number of 10. Therefore I can define _STRING_SIZE to be 10. However, I had other string that need to be compared and is not a character of 10. In this case, how can I go about doing it?

Everytime my serial port finishes its string sending, a CR or (0x0D) will be added behind the string. How can I make used of this to know that once I had finish my string received, I can start to do the compare for correct string

BTW, for serial port receiving, I am using Serial Port interrupt to store all the value into the DATA[30] buffer, once I received a CR, I will compare and reset the buffer pointer back to 0 so I can wait for another serial string.
 

_STRING_SIZE could be the sizeof() of the stored string in flash. Keep in mind that in some cases the size of a string could be one more byte from the actual size. Please refer to the following thread to understand what I'm talking about.

https://www.edaboard.com/threads/254210/#post1089349

Furthermore, if all the characters until the end of the loop are the same, then the next character of the incoming string from serial port must be CR in order to finish the comparison succesfully. That's an easy way I can think of.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top