Help with strcomp
Don M
Posts: 1,652
There is another thread talking about strcomp that got me to thinking about using it. I have been playing around with some RFID cards and was trying to make use of this but it doesn't work the way I thought it would. What am I doing wrong here?
I have a byte variable called RFIDdata2[10] that the data is read into from a card:
I can display it on the terminal fine by using the repeat i routine. It shows 0F03040185 for one card and 0415D87A73 for the other on the terminal:
In the DAT section I have only the 1 card listed as:
So I'm trying to use strcomp to compare what is in the RFIDdata2 buffer to the Tag1 in the DAT section but it doesn't work right:
I always get a "Match !". It seems as though it is only comparing the first byte which is a "0" for the 2 different tags I have. The second byte is different. One tag is "4" the other is "F". I tried different combinations of adding the "@" before RFIDdata2 and Tag1 but didn't make any difference.
Help!
I have a byte variable called RFIDdata2[10] that the data is read into from a card:
VAR byte i, RFIDdata2[10]
I can display it on the terminal fine by using the repeat i routine. It shows 0F03040185 for one card and 0415D87A73 for the other on the terminal:
repeat i from 0 to 9 Term.tx(RFIDdata2[i])
In the DAT section I have only the 1 card listed as:
DAT Tag1 byte "0F03040185"
So I'm trying to use strcomp to compare what is in the RFIDdata2 buffer to the Tag1 in the DAT section but it doesn't work right:
PUB CheckRFID if strcomp(RFIDdata2, Tag1) term.str(string("Match !")) else term.str(string("No match !"))
I always get a "Match !". It seems as though it is only comparing the first byte which is a "0" for the 2 different tags I have. The second byte is different. One tag is "4" the other is "F". I tried different combinations of adding the "@" before RFIDdata2 and Tag1 but didn't make any difference.
Help!
Comments
Try making the RFIDdata2 array 11 elements in length and assigning zero to the last element of you RFID string then add a zero to your DAT string.
Jeff T.
Jeff T.
In some cases a char array containing zeros is not a string at all but a protocol. It depends on the application.
Make sure your tags in the DAT section have a terminating zero (zero, not "0")
Use the address operator (@) with strcomp
Thanks JonnyMac! That fixed it.
Once you're comfortable with pointers (the address of an element), you could write a method like this that will take your tag and compare it to a known database (in a DAT section). If your tag is found then you'll get 0..n back, if not found, it will return -1.
Written on-the-fly -- you'll need to test.
Thanks Jonny. I'm playing with the code now to see how it works.
So my DAT database looks like this:
And I invoked you code this way:
The actual tags I have are the second and fourth tags. The other 2 are just made up numbers.
It seems to work with the exception of it doesn't matter if I put in a value of 0, 1, 2, 3 or 4 for the limit of elements. It still returns 1 for the second tag and 3 for the fourth tag.
Edit: Oops.. my bad. I missed copying the last line of code. Sorry. Yes it works. Thanks again!
As written, the method returns a 0-based index into the tags database. You can use this index with another database (e.g., names, features, etc.) if desired.
It's very easy to convert to a 1-based index; by doing this you can evaluate the return as true (non-zero) or false (0) -- this simplifies things if you only care about the existence of a tag in the database and aren't connecting the tag index to anything else.
Here's the modification:
Thanks. I knew it was a zero based index and how the numbering scheme worked. I just worded my response in such a way that it sounded like I didn't understand that the first item would be 0 and so on and that I could have changed the index start to 1 instead of zero. Sorry about that. I had it working fine after I discovered that I had left out the last line of code from copying from the webpage.
I was also working on just as you mentioned- another database that indexes the tags to the names. I have a few bugs to iron out yet but I'll yell if I get stuck.
You must have read my mind... that is the part I was having trouble with. I padded the names I am using now with spaces to make them all the same length.
I've been around that block a few times. Padding works, but creates problems should you need to increase the maximum length of a name. If you are going to pad to create fixed-width entries, pad with zeros instead of spaces -- this prevents display problems later. For example:
In this case, the longest name is nine characters; the first entry is only three so six additional zeros are added to create the pad.