Shop OBEX P1 Docs P2 Docs Learn Events
strcomp — Parallax Forums

strcomp

TumblerTumbler Posts: 323
edited 2012-10-16 09:52 in Propeller 1
Hello

I have some troubles with a sub.

This one works:
PUB waitOK    | succes  succes := false
  repeat until Gsm.rxcheck < 0
  if gsm.Rx == 13
    if gsm.Rx == 10
      if gsm.Rx == 79
        if gsm.Rx == 75
          if gsm.Rx == 13
            if gsm.Rx == 10
             succes := true
  gsm.rxflush
return succes            
And this one doesn't. was wondering why.
PUB waitOK | succes
  succes:= false
  repeat
    gsm.RXstr(@Buff)
  while Buff[0]== -1       
   
  if strcomp(@Buff,@ok)
    succes := true


  
  gsm.rxflush
  return succes

Dat

ok      Byte 13,10,79,75,13,10,0

Comments

  • JonnyMacJonnyMac Posts: 9,108
    edited 2012-10-16 09:52
    The strcomp method works with z-strings so both need to be properly terminated with 0.

    I wrote this method for checking the presence of one string inside another -- can even specify the number of characters to scan. I use this when I am parsing commands out of longer strings.
    pub strncmp(str1, str2, n) | match
    
    '' Compares n characters of str2 with str1
    '' -- str1 and str2 are pointers
    '' -- 0 if str1 == str2, 1 if str1 > str2, -1 if str1 < str2
    
      match := 0
    
      if (n > 0)
        repeat n
          if (byte[str1] > byte[str2])
            ++match
            quit
          elseif (byte[str1] < byte[str2])  
            --match
            quit
          else
            ++str1
            ++str2
    
      return match
    


    As you're providing the number of characters/bytes to compare, it will work with any two arrays of bytes.
Sign In or Register to comment.