Skip to main content
j3lda
Associate II
March 30, 2013
Question

The best way to compare two buffers

  • March 30, 2013
  • 2 replies
  • 1000 views
Posted on March 30, 2013 at 10:49

Hello, I�m begineer in programming in C and microcontrollers, so I�m sorry for stupid question. I receive commands via USART. What is the best way to compare RxBuffer with command saved in memory? Now I use comparing single bytes (1.byte with 1.byte, 2.byte with 2.byte,...), but this isn�t good solution(big code,...). Thanks for answers.

uint8_t RxBuffer[100]; // for example RxBuffer[0]=82, RxBuffer[1]=83,

                                      // RxBuffer[2]=84, RxBuffer[3]=10 ...RST\n

uint8_t IDNcommand[] = ''IDN\n'';

uint8_t RSTcommand[] = ''RST\n''; 

                     .

                     .

                     .

// how to smartly compare these buffers?

// problem: they have different size

                       

#parser #parsing
    This topic has been closed for replies.

    2 replies

    Andrew Neil
    Super User
    March 30, 2013
    Posted on March 30, 2013 at 12:30

    Have you looked at the functions available in the standard 'C' library?

    http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

    One way of parsing an input stream is to use a so-called State Machine aka Finite State Machine (FSM) or Finite State Automaton (plural: Automata) eg,

    http://www.visualgps.net/WhitePapers/NMEAParser/NMEAParser.html

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    petre
    Associate II
    April 5, 2013
    Posted on April 05, 2013 at 15:20

    Hi,

     

     

    You can try:

    int strncmp ( const char * str1, const char * str2, size_t num );search the function on the web, you'll get it :)Regards,Mihai