Shop OBEX P1 Docs P2 Docs Learn Events
Help with comparing bytes in an array with buffer — Parallax Forums

Help with comparing bytes in an array with buffer

Don MDon M Posts: 1,652
edited 2014-09-07 16:42 in Propeller 1
I am playing around with the RFID reader and an EMIC board. I have some tags and I want to do a compare with a list of tags then it will speak the appropriate name for the tag.

Here' my compare logic for just one tag. No matter what tag I scan it always seems to be true:
PUB CheckBusTag
  repeat i from 0 to 11                             'Compare tag's 10 unique ID #s against ID #s stored in data table
    if byte[@RFIDdata[i]] == byte[@Tag1[i]]
      serial.TX("S")
      serial.Str(@BusTag1)                         ' Send the desired string to convert to speech (stored in the DAT section below)
      serial.TX(pst#NL)
      repeat until serial.RxCheck == ":"           ' Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
      return

DAT

Tag1    byte  10, 65, 48, 48, 48, 48, 68, 66, 66, 56, 48, 13       '<-- Enter your own RFID tag ID numbers here
Tag2    byte  10, 48, 70, 48, 50, 65, 54, 55, 48, 69, 48, 13

BusTag1       byte "Good Morning Amy Lee. Thank you for scanning your tag", 0
BusTag2       byte "Good Morning Janelle. Thank you for scanning your tag", 0
BusTag3       byte "Good Morning Trevin. Thank you for scanning your tag", 0

What am I doing wrong here?

Thanks.
Don

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-09-07 10:40
    Don M wrote: »
    What am I doing wrong here?

    It looks like you're only checking a single byte before taking action. You really need to run through all the bytes in question before deciding if there's a match or not.

    Here's my take:
    PUB CheckBusTag | i
    
      repeat i from 0 to 1 ' since there's only two tags listed
        result := CompareBuffers(@RFIDdata, @Tag1[i * 11], 11)                           
        if result == 0
          serial.TX("S")
          serial.Str(FindString(@BusTag1, i)                         ' Send the desired string to convert to speech (stored in the DAT section below)
          serial.TX(pst#NL)
          repeat until serial.RxCheck == ":"           ' Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
          return
    
    
    PUB CompareBuffers(pointer0, pointer1, size)
    
    
      repeat size
        if byte[pointer0] <> byte[pointer1]
          result++
    
    
      
    PUB FindString(firstStr, stringIndex)      
    '' Finds start address of one string in a list
    '' of string. "firstStr" is the address of 
    '' string #0 in the list. "stringIndex"
    '' indicates which of the strings in the list
    '' the method is to find.
    '' Version from HexapodRemote140128a
    '' Version Hexapod140129a, removed commented
    '' out code.
    
    
      result := firstStr
      repeat while stringIndex    
        repeat while byte[result++]  
        stringIndex--
      
    DAT
    
    
    Tag1    byte  10, 65, 48, 48, 48, 48, 68, 66, 66, 56, 48, 13       '<-- Enter your own RFID tag ID numbers here
    Tag2    byte  10, 48, 70, 48, 50, 65, 54, 55, 48, 69, 48, 13
    
    
    BusTag1       byte "Good Morning Amy Lee. Thank you for scanning your tag", 0
    BusTag2       byte "Good Morning Janelle. Thank you for scanning your tag", 0
    BusTag3       byte "Good Morning Trevin. Thank you for scanning your tag", 0
    
    
    

    BusTag3 will never be read since there are only two serial numbers.

    I have some RFID code you might want to look at. Here's a link.
  • Don MDon M Posts: 1,652
    edited 2014-09-07 10:46
    Thanks Duane.

    What I tried next was to just append a "0" to the end of the data read into RFIDdata buffer and also added a "0" to end of tags in DAT section and tried a strcomp. That seems to work too.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-09-07 10:59
    Don M wrote: »
    Thanks Duane.

    What I tried next was to just append a "0" to the end of the data read into RFIDdata buffer and also added a "0" to end of tags in DAT section and tried a strcomp. That seems to work too.

    I wondered about using strcomp but then you can't have a zero as one of your data bytes.

    Make sure and test out the method "FindString". I find it extremely useful when wanting to display text based on some index number.
  • Don MDon M Posts: 1,652
    edited 2014-09-07 11:29
    Duane Degn wrote: »
    Make sure and test out the method "FindString". I find it extremely useful when wanting to display text based on some index number.

    Where do I find this method?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-09-07 13:19
    Don M wrote: »
    Where do I find this method?

    It's included in the code posted in post #2.
  • Don MDon M Posts: 1,652
    edited 2014-09-07 13:53
    Duh.. my bad. Thanks Duane.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-09-07 16:42
    Assuming you have a way of reading the tag data into a buffer, this little demo shows another way to compare the buffer with known tags. From that index, you can do interesting things with string output using @@.
    pub get_tag_id(p_buf) | tidx, p_check, bidx
    
    '' Compares tag data in ram (at p_buf) with known tags
    '' -- returns tag index (0..LAST_TAG) if found
    '' -- returns -1 if tag not found
    
      repeat tidx from 0 to LAST_TAG                                ' loop through known tags
        p_check := @@Tags[tidx]                                     ' get hub address of tag being tested
        repeat bidx from 0 to 11                                    ' loop through bytes in tag
          if (byte[p_buf][bidx] <> byte[p_check][bidx])             ' if byte mismatch
            quit                                                    ' abort this tag
        if (bidx == 12)                                             ' if all bytes matched
          return tidx                                               ' return this tag id
          
      return -1                                                     ' return not found
      
    
    dat { rfid tags / names / greetings }
    
      Tag0        byte      10, 65, 48, 48, 48, 48, 68, 66, 66, 56, 48, 13 
      Tag1        byte      10, 48, 70, 48, 50, 65, 54, 55, 48, 69, 48, 13
    
      Tags        word      @Tag0, @Tag1
    
    
      Name0       byte      "JonnyMac", 0
      Name1       byte      "Lynda", 0
    
      Names       word      @Name0, @Name1
    
    
      Hello       byte      "Hello, ", 0
      Goodbye     byte      "Good-byte, ", 0
      Period      byte      ". ", 0                                 ' after name punctuation
      ThankYou    byte      "Thank you for checking in.", 0
      Sorry       byte      "Sorry, your tag is not in our database.", 0
    
      Messages    word      @Hello, @Goodbye, @Period, @ThankYou, @Sorry
    
Sign In or Register to comment.