Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
HID RFID card reader API (Wiegand Protocol)

 
Post new topic   Reply to topic    Forum Index -> Files
Author Message
lewtwo



Joined: 21 Oct 2007
Posts: 12

Posted: 27 October 2007, 5:17 AM    Post subject: HID RFID card reader API (Wiegand Protocol) Reply with quote

This is an API to read RFID cards using a standard Wiegand Protocol HID reader. Only two wires are needed and no additional hardware. It will read cards with any number of bits. The output is stored in an array of bytes. It may be retrieved as binary string, decimal string or unsigned long.

Have a nice day ...



RFID_API.ZIP
 Description:
Source code to read 125Khz HID cards with HID reader.

Download
 Filename:  RFID_API.ZIP
 Filesize:  5.91 KB
 Downloaded:  5510 Time(s)

Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2975
Location: Portland, OR

Posted: 28 October 2007, 22:53 PM    Post subject: Reply with quote

Thanks for contributing. Are you using this in a commercial product?
Back to top
lewtwo



Joined: 21 Oct 2007
Posts: 12

Posted: 29 October 2007, 1:58 AM    Post subject: Reply with quote

dkinzer wrote:
Thanks for contributing. Are you using this in a commercial product?


No ... just internally to get around a wee problem.
Back to top
mikep



Joined: 24 Sep 2005
Posts: 792

Posted: 29 October 2007, 2:35 AM    Post subject: Re: HID RFID card reader API (Wiegand Protocol) Reply with quote

lewtwo wrote:
This is an API to read RFID cards using a standard Wiegand Protocol HID reader. Only two wires are needed and no additional hardware.

I did a quick code review and I found some places that could be improved. It is up to you if you want to take these changes but I thought I would pass them on as they are also instructive for other people.

Here are the first three things that hit me:
1. The multitasking and sharing of variable seems very dependent on the hardware protocol. You may want to look at writing more robust code that uses a semaphore to access the shared memory.
2. There are a number of hard-coded constants and dependent constants that could be stored in a lookup table.
3. The line
Code:
 J=Shl(J,1)   'same as multiplying by 2
looks a bit incongruous. This shift left is hardly going to save any time. Then looking at the problem further I realized that you could eliminate the calculate loop and save memory as well.

The basic problem is that you are using an array of bytes rather than bits and not using the Alias feature of ZBasic. This is a very powerful feature that allows the memory for variables to be overlaid, within some constraints. Here is the declarations reusing some of your variable names. Note the 4 aliases of UnsignedLongs with byte alignment.
Code:
Private RfidIdx as Byte
Private Const RFID_MAX_BITS as Byte = 64
Private ByteAlign RFIDQue(1 To RFID_MAX_BITS) As Bit
Private RFIDValues(1 to RFID_MAX_BITS\8) As Byte Alias RFIDQue(1) ' only used for initialization
Private RFCODE26 as UnsignedLong Alias RFIDQue(1)
Private RFCODE34 as UnsignedLong Alias RFIDQue(1)
Private RFID26 as UnsignedLong Alias RFIDQue(9)
Private RFID34 as UnsignedLong Alias RFIDQue(17)

I initialized the array of 64 bits as follows to provide a test case:
Code:
   ' initialize bit values
   RFIDValues(1) = FlipBits(&H07)
   RFIDValues(2) = FlipBits(&H08)
   RFIDValues(3) = FlipBits(&H40)
   RFIDValues(4) = FlipBits(&HA6)   
   RFIDValues(5) = FlipBits(&H80)
   RFIDValues(6) = FlipBits(&H00)
   RFIDValues(7) = FlipBits(&H00)
   RFIDValues(8) = FlipBits(&H00)

Here is the rewrite of the function to retrieve the ID without a loop. Note the shifts to get the correct 15/16 bits and the method to convert the big-endian bit format to a little-endian number. There is a small bug in this code for 26-bit devices - I will leave it to you resolve it.
Code:
Public Function RfIDDec2() as UnsignedLong
   Dim Val As UnsignedInteger
   
      Select Case RfidIdx
      Case 26
          RfIDDec2 = RFID26
         Val = CUInt(Shr(RfIDDec2,1))
         RfIDDec2 = CULng(MakeWord(FlipBits(HiByte(Val)), FlipBits(LoByte(Val))))
      Case 34
          RfIDDec2 = RFID34
         Val = CUInt(Shr(RfIDDec2,1))
         RfIDDec2 = CULng(MakeWord(FlipBits(HiByte(Val)), FlipBits(LoByte(Val))))      
      Case Else
         RfIDDec2=0
   End Select
End Function

Here is the simplified code to retrieve the facility code that uses a similar mechanism of bit shifts and endian flipping to retrieve the correct code as an unsigned long:
Code:
Public Function RfIDFacility2() as UnsignedLong
   Dim Val As UnsignedInteger
   
      Select Case RfidIdx
      Case 26
         RfIDFacility2 = RFCODE26
         Val = CUInt(Shl(Shr(RfIDFacility2,1),7))
         RfIDFacility2 = CULng(MakeWord(FlipBits(HiByte(Val)), FlipBits(LoByte(Val))))
      Case 34
         RfIDFacility2 = RFCODE34
         Val = CUInt(Shr(RfIDFacility2,1))
         RfIDFacility2 = CULng(MakeWord(FlipBits(HiByte(Val)), FlipBits(LoByte(Val))))
      Case Else
         RfIDFacility2=0
   End Select
End Function

There is a more cleanup that could be applied to this code but I wanted to give you an initial idea of the 9x speedup that can be achieved and leave the rest to you. Here are some performance results in microseconds for the old and new code:

Card TypeOld CodeImproved Code
Facility Code2637359
ID2653157
Facility Code3462363
ID3458063

Mike Perks
Check out the new ZX-128e and ZX-1281e ZBasic devices at OakMicros.com
Back to top
lewtwo



Joined: 21 Oct 2007
Posts: 12

Posted: 29 October 2007, 3:46 AM    Post subject: Reply with quote

Thank you.

I am new to the MPU environment. I just got into this a couple of weeks ago because tossing a dedicated Intel based XP machine at the problem seemed a little extreme. I first tried a Basic Stamp, then a New Media BX24 and moved to the ZX24 because it had the handy interrupts (negating the need for extra hardware).

I completely missed the "alias" feature . Even the ZX-24a is over kill for what I am trying to do. On the other hand there is not point to wasting cycles or ram. I will work on the code and post a cleaned up version later.

Currently I am learning how to design a circuit board and get it made so I can have real RS-232 levels (I was so trying to avoid doing anything at that hardware level). I did look at ZX-24ae but I did not find any readily available generic carrier boards.
Back to top
DocJC



Joined: 16 Mar 2006
Posts: 110
Location: Cleveland, OH

Posted: 31 October 2007, 3:47 AM    Post subject: Reply with quote

The number of options for getting circuit boards made keeps growing, a good thing.
I've used ExpressPCB (www.expresspcb.com) numerous times, always with good results.
The user interface is easy to learn, the software is provided without charge, and the board prices are reasonable.
JC
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> Files Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2017 Elba Corp. All rights reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group