PDA

View Full Version : RFID Issues.... HELP !!!!!



andybarrett1
- 1st September 2014, 21:34
Thank you for looking at this... I am attempting to use a EM4100 Reader to read some cards and light some leds on the bench...

I have stolen some code and added some code... The original worked and still works

However mine doesn't... I susspect it is foing astraty in the for/next loop

Any advise before i go back to Ibuttons.... I thought this might be easier :-)

Hardware and Fuses are all fine.... 4 tags/ four doors !


Thank you



'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 24/08/2014 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************

adcon1 = 7
DEFINE OSC 12 'Set oscillator in MHz
'OSCCON = $70

' -----[ Variables ]-------------------------------------------------------

buf VAR Byte(10) ' RFID bytes buffer
tagNum VAR Byte ' from EEPROM table
idx VAR Byte ' tag byte index
char VAR Byte ' character from table



' -----[ EEPROM Data ]-----------------------------------------------------

Tag1 DATA "1400434B9C"
Tag2 DATA "1500649EC3"
Tag3 DATA "150063C82C"
Tag4 DATA "X50064A4CA"


' -----[ Program Code ]----------------------------------------------------

Main:

SERIN2 portb.1,84, [WAIT($02), STR buf\10] ' Read RFID

Check_List:

FOR tagNum = 1 to 4 ' scan through known tags
FOR idx = 0 TO 9 ' scan bytes in tag
READ (((tagNum-1) * 10) + idx), char ' get tag data from table
IF (char = buf(idx)) THEN loc1
IF (char = buf(idx)) THEN loc2
IF (char = buf(idx)) THEN loc3
IF (char = buf(idx)) THEN loc4
Next
Next
goto main

loc1:
high PortB.4
PAUSE 1000
LOW PortB.4
PAUSE 1000
GOTO Main

loc2:
high PortB.5
PAUSE 1000
LOW PortB.5
PAUSE 1000
GOTO Main

loc3:
high PortB.6
PAUSE 1000
LOW PortB.6
PAUSE 1000
GOTO Main

loc4:
high PortB.7
PAUSE 1000
LOW PortB.7
PAUSE 1000
GOTO Main
End

EarlyBird2
- 1st September 2014, 21:48
FOR tagNum = 1 to 4 ' scan through known tags
FOR idx = 0 TO 9 ' scan bytes in tag
READ (((tagNum-1) * 10) + idx), char ' get tag data from table
IF (char = buf(idx)) THEN loc1
IF (char = buf(idx)) THEN loc2
IF (char = buf(idx)) THEN loc3
IF (char = buf(idx)) THEN loc4
Next
Next
goto main

If you scan a card with "1" as the first character three doors will open? No always the first will open.

andybarrett1
- 1st September 2014, 21:52
Hi Steve.

Has to see full 10 digits.... the X on the last one is my checksum.... just to see what happens if Number not in eprom

Four doors

Four different keys

one key for each door !!

EarlyBird2
- 1st September 2014, 21:59
Hi Steve.

Has to see full 10 digits.... the X on the last one is my checksum.... just to see what happens if Number not in eprom

Four doors

Four different keys

one key for each door !!

But this line

IF (char = buf(idx)) THEN loc1

unlocks loc1 when char = buff(idx)

if you scan any card the first character is 1
the first data character is 1 at buff(0)
therefore they match and loc1 opens
which it should not do obviously.

andybarrett1
- 1st September 2014, 22:07
Ah.....

OK

Rethink needed then :-)

EarlyBird2
- 1st September 2014, 22:14
What you need to do is

For each id check each character and if one does not match jump out. If all match unlock door.

FOR idx = 0 TO 9 ' scan bytes in tag
READ (((tagNum-1) * 10) + idx), char ' get tag data from table
IF (char != buf(idx)) THEN goto nextTag
Next

high PortB.4
PAUSE 1000
LOW PortB.4
PAUSE 1000
GOTO Main

nextTag:

I think!

andybarrett1
- 1st September 2014, 22:18
Ok ....

Food for thought... I got to get kit ready for tomorrow...Off to Newcastle .. change from Scunny.

Queen Anne looking like November turn on.... Bonfire Night !!!

Thanks for help

andybarrett1
- 1st September 2014, 22:46
Now Then Steve...

Did it a lazy way.... But hey it works now !!!




Main:

SERIN2 portb.1,84, [WAIT($02), STR buf\10] ' Read RFID

IF ("C" = buf(9)) and ("9" = buf(8))THEN loc1
IF ("C" = buf(9)) and ("2" = buf(8))THEN loc2
IF ("A" = buf(9)) and ("C" = buf(8))THEN loc3
IF ("3" = buf(9)) and ("C" = buf(8))THEN loc4

goto main

I know it rough... But I can see how it works !!!

EarlyBird2
- 2nd September 2014, 07:29
Now Then Steve...

Did it a lazy way.... But hey it works now !!!


Main:

SERIN2 portb.1,84, [WAIT($02), STR buf\10] ' Read RFID

IF ("C" = buf(9)) and ("9" = buf(8))THEN loc1
IF ("C" = buf(9)) and ("2" = buf(8))THEN loc2
IF ("A" = buf(9)) and ("C" = buf(8))THEN loc3
IF ("3" = buf(9)) and ("C" = buf(8))THEN loc4

goto main

I know it rough... But I can see how it works !!!

Are you sure you can see how it works?

IF ("C" = buf(9)) and ("9" = buf(8))THEN loc1

Looks like

IF ("C" = buf(9)) THEN
IF ("9" = buf(8)) THEN
loc1
ENDIF
ENDIF

which is the positive and obvious way of looking at it but there is another way.


IF ("C" != buf(9)) THEN nextTag
IF ("9" != buf(8)) THEN nextTag
loc1
nextTag:

Call this the negative vision. This is looking for a failure and is more efficient because if the first character fails then no others are tested.

But it works more like this

compFlag var bit
compFlag=1

IF ("C" != buf(9)) THEN compFlag=0
IF ("9" != buf(8)) THEN compFlag=0
IF compFlag=1 THEN loc1


You have almost spotted another reason your code did not work there is a clue in this line

IF ("C" = buf(9)) and ("9" = buf(8))THEN loc1

which indicates to me that you have worked it out.

I will leave that one as more food for thought.

andybarrett1
- 4th September 2014, 08:04
Hi Steve

Have puzzled over this for a whilst…..I have to say I haven't a clue… What have I missed or nearly found !!!

BR
Andy

EarlyBird2
- 4th September 2014, 08:23
Hi Steve

Have puzzled over this for a whilst…..I have to say I haven't a clue… What have I missed or nearly found !!!

BR
Andy



' -----[ EEPROM Data ]-----------------------------------------------------

Tag1 DATA "1400434B9C"
Tag2 DATA "1500649EC3"
Tag3 DATA "150063C82C"
Tag4 DATA "X50064A4CA"


' -----[ Program Code ]----------------------------------------------------

Main:

SERIN2 portb.1,84, [WAIT($02), STR buf\10] ' Read RFID

When you read the code buf(0) will be "C" for Tag1
When you read the Data the first char will be "1"

So

READ (((tagNum-1) * 10) + idx), char
IF (char = buf(idx)) THEN loc1

When idx is 0 what are the values of char and buf(0)?

andybarrett1
- 5th September 2014, 16:04
No...

Sorry..... I totally missing it !

If I could get a fluke in there in with a chance..... I suspect it is is to do with the "buf" count being backward or stopping, but really don't know.

Need to get a simulator working really or a debug out !

BR
And

Amoque
- 5th September 2014, 18:40
I appreciate how tempting it is to use other's code, but this can be dangerous if you aren't clear on how every step works - consider if it HAD worked, but through some oversight, left the doors to open with any card with a 1 in the fourth position or other flaw - unlikely in this case, but do you see my point? My suggestion would be:

Limit your ID to 1 tag and 1 "buf", start by IDing only the first character then, when that works, build a loop to check all 10 characters of one tag (or until the first mismatch). Consider blinking an LED as each character is checked and, perhaps adding a second red led to flash when a character match fails. This modular approach works very well for me and helps my understanding keep up with increasingly complex code. Also, if 80% of the code worked as expected before the last 20% was added, it is much easier to determine what I did that didn't result in what i expected to happen happening. This technique is very much like how most people make sense of complex sentances as well. Of course, then add the final loop that loops through each card DATA statement - similar to what you have now.

I think that EarlyBird2's posts here are with some of the best I've ever read. The need and willingness to study not only the function, but the logic behind the function is what seperates amateurs like me from professionals like him - and avoids the need to recall millions of cars because of some issue not discovered until FAR too late..

EarlyBird2
- 6th September 2014, 08:29
Need to get a simulator working really or a debug out !


This is exactly what you need to do.

Yes when the DATA places the Tag in the EEPROM the largest number is in 0 and smallest in 9

DATA "1400434B9C"
locatn 0123456789

and when the Tag is read into buf

buf 1400434B9C
idx 9876543210

which obviously is the opposite order, well obvious when explained.


FOR idx = 0 TO 9 ' scan bytes in tag
READ (((tagNum-1) * 10) + idx), char ' get tag data from table
IF (char != buf(idx)) THEN goto nextTag
Next
To repeat myself in the above code when idx is 0 char will be 1 and buf(0) will be C.

You could reverse the order in the DATA statement but this could become a burden if you have thousands of Tags. A neater solution is to change the code.



FOR idx = 0 TO 9 ' scan bytes in tag
READ (((tagNum-1) * 10) +9- idx), char ' get tag data from table
IF (char != buf(idx)) THEN goto nextTag
Next

Let me know if I am wrong because I hate misleading posts.

andybarrett1
- 8th September 2014, 16:31
Thank you all for help.

I "think" I now see what was going on...Not been able to prove it though ... yet !

Amoque is right about his comment re:- other peoples code..... Point taken :-)

Arrays and counts are not my area at all in software..... Well non of it is really!

I struggle.... But I give it a go..... And sometimes things work... Mainly thanks to this forum

Thank you all for help... As Always.

Back to sunny Scunny on Wednesday Steve... BOS is calling!

Demon
- 9th September 2014, 00:15
When I hit a wall, I copy just the specific logic on a 16F628, or whatever else you can run the debugger (I'm still on PBP 2.60c).

I read in values as they are read (doesn't have to be an actual RFID reader, as long as sequence of characters is the same).

Then I go line by line, check each variable, making sure they contain what I expect, that the logic flows as expected. This is the hard part in using other people's code.

If I can't figure it out using the debugger, I ditch the code and try another approach.

Robert

andybarrett1
- 9th September 2014, 11:55
Ditch the code and try another approach

Is What I did..... Not as pretty as using counts and for/next but it works..... Does what I want :-)

I am picking up lots of help and advice here though.... Which will come out along the way !!

I have just upgraded to 2.60C.... !!! Really not sure if I need 3.00

Seems most folks are 2.50x or 2.60x

EarlyBird2
- 10th September 2014, 07:59
Is What I did..... Not as pretty as using counts and for/next but it works..... Does what I want :-)

I am picking up lots of help and advice here though.... Which will come out along the way !!

I have just upgraded to 2.60C.... !!! Really not sure if I need 3.00

Seems most folks are 2.50x or 2.60x

I am sad to hear this and I feel that I have failed in some way. Never give up in the search for perfection and ignore the defeatists is my advice. So now it works you should have time to try again to perfect the counts and loops method.

Amoque
- 10th September 2014, 23:32
Once again EarlyBird provides sagely advice... I have build an aquarium controller recently (turns on/ off lights, monitors temperature, warns of "low flow"... that sort of thing). My first version works, but is very inefficient and not complete as I'd like, so version two was started. Most every weekend I wrestle with it, but Sunday nights I reload version one. I'm surprised at both the software and the hardware I made work as my skills have improved. I hope you find this as well, Andy.

andybarrett1
- 11th September 2014, 10:00
I am sad to hear this and I feel that I have failed in some way. Never give up in the search for perfection and ignore the defeatists is my advice. So now it works you should have time to try again to perfect the counts and loops method.

Not at all the case Steve... Work commitments and lack of time due to other ventures dictates when I can play with Pics.

The RFID issues and the Morse are ongoing projects.... and are being worked on as a rolling living thing...The updated Morse code is almost complete if you can remember your useful suggestions re :- labels.

RFID is taking a little longer... but it is a steep learning curve for me.

Also I need to look up new words "sagely".... Means Wise Advice. He is right.

Thank you again... Sagely one !