Click here to Skip to main content
15,867,834 members
Articles / Programming Languages / C#

C# ZKTeco Biometric Device Getting Started

Rate me:
Please Sign up or sign in to vote.
4.95/5 (61 votes)
7 Feb 2020CPOL5 min read 534.5K   120.2K   68   260
An implementation of a Zkteco (K14) Biometric Device using C#
This article is written as a getting started guide with a few pieces of source code such that it helps C# developers integrate a biometric device into their own application. The article and the source code provided will help you with the following things: Registering the SDK components, ping the device, connecting to the device, extracting data out of the device (Log Data, User Info, etc.), and how to register to device specific events.

Introduction

There are requirements where you have to integrate a biometric device into your own application or some features of it like fetching the attendance recordors, user information, backing up the fingerprint templates into your system database or simply export the data stored in the device to some remote endpoint.

For that very purpose, this article is written as a getting started guide with a few pieces of source code such that it helps C# developers to save some valuable time with all the troublesome work.

Note: Though the devices will be of various models, with a different set of sdk, the implementation procedure will be somewhat the same.

The following article contains some useful old pieces of code, if you ever get your hands on a biometric device (fingerprint reader for Time & Attendance) and want to extract gold(data) out of it.

Implemented Device Specification

Device Name : K14
Firmware Ver : ver. 4.0.1 ( build 39 )
Vendor : ZKTeco Inc.
FP : 500
Records : 50000

 

NOTE:
If you are working with a different model of the device like ZK4500, ZK9500, SLK20M or SLK20RHere is a good starting point for those device models.

If you are working with a different brand like the Realtime Biometric

Prior to going through the following article, it would be beneficial if you have some good understanding of the following things:

  • How a biometric device works, its features and functionalities
  • How to Register/ Unregister DLLs using the Regsvr32 utility
  • Some knowledge about how the ref and out parameters work

The article and the source code provided will help you with the following things:

  • Registering the SDK components
  • Ping the device
  • Connecting to the device
  • Extracting some random data out of the device (Log Data, User Info, etc.)
  • Source code to upload data to the device
  • Restarting the device
  • How to register to device specific events [Not Implemented/ You will have to implement it yourself per your need]

Background

Biometric Devices are being used extensively in many corporations through out the world these days. Companies like ZKTeco manufacture biometric access control and Time and Attendance in various shapes and sizes with different set of features per model. They provide a better way of user authentication and security in organizations who implement it. It is a growing demand in today's world. Hence, developers are likely to come across its implementation at some point.

Using the Code

Note: The follow procedures are applicable for the K14 model devices. Other devices might have a different set of SDK, hence a slightly different set of procedures.

In order to implement any biometric device, you will have to register the related components in the system, which can be easily done by using the Regsvr32 utility. If you don't know how it works, please go through some blog posts in the related topic. We already have a batch file ready for that very purpose, Hence, we are cool here.

You can download the SDK from here. I have already attached an SDK sample along with this article. You can go through the source code inside the Register_SDK.bat  file to see the registration mechanism.

Registering the Components

  1. Fire up a Command Prompt with Administrator Privilege
  2. Navigate to the SDK location
  3. Execute the Register_SDK batch fileImage 1

On Successful Registration

You should be seeing the following dialog once your registration is complete:

Image 2

Now, To implement the biometric device, you will have to use the zkemkeeper.dll.

 

Implementing the SDK

  1. Add Reference to the zkemkeeper.dll in your project.
  2. Create a class file and implement the IZKEM interface provided by the above dll.
  3. Use the CZKEM class file to perform device related operations.

Let's say your Biometric device is connected in the local area network. To make a successful communication with the device, you need to know the IP address and the Port No.

IP address : Assign a static IP in the device itself
Default Port : 4370
Machine No : 1

Now we have a valid IP and Port, let's go through the source code used in the demo project entitled BioMetrix which I have attached along with this article. The application helps by showing some of the API implementations.

The ZKemClient.cs file implements the IZKEM interface which contains the following code.

 

Connecting to the Device

The following code helps connect to the device at the given IPAddress and Port and on successful connection, it registers some events.

(The events are not implemented though, you will have to implement it yourself as per your need.)

C#
public bool Connect_Net(string IPAdd, int Port)
{
    if (objCZKEM.Connect_Net(IPAdd, Port))
    {
        if (objCZKEM.RegEvent(1, 65535))
        {
            objCZKEM.OnConnected += ObjCZKEM_OnConnected;
            objCZKEM.OnDisConnected += objCZKEM_OnDisConnected; 
            objCZKEM.OnEnrollFinger += ObjCZKEM_OnEnrollFinger; 
            objCZKEM.OnFinger += ObjCZKEM_OnFinger; 
            objCZKEM.OnAttTransactionEx += 
               new _IZKEMEvents_OnAttTransactionExEventHandler(zkemClient_OnAttTransactionEx);
        }
        return true;
    }
    return false;
}

 

Registering Events

To fire any event when something occurs in the device, we need to register the event:

C#
 bool RegEvent(int dwMachineNumber, int EventMask); 
// Entering 65535 for the EventMask value registers for all events

The EventMask requires an integer value whose value varies based on the type of event.
The complete list of EventMask values are given below:

1 OnAttTransaction, OnAttTransactionEx  
2 (1<<1) OnFinger
4 (1<<2) OnNewUser
8 (1<<3) OnEnrollFinger
16 (1<<4) OnKeyPress
256 (1<<7) OnVerify
512 (1<<8) OnFingerFeature
1024 (1<<9) OnDoor, OnAlarm
2048 (1<<10) OnHIDNum
4096 (1<<11) OnWriteCard
8192 (1<<12) OnEmptyCard
16384 (1<<13) OnDeleteTemplate

To register for multiple-events, we can perform XOR operations between binary codes of related events.

To register all real-time events, the value of EventMask can be set as 65535 which I have implemented in the above code.

 

Fetching User Info

The user information can be fetched by providing a machine number ( It is generally 1 ). The API implements the out parameter to give back the output. I had stored some random user data in the fingerprint device and used the following code to retrieve it back and display it.  Here, The TmpData is the users (fingerprint) template data.

C#
public ICollection<UserInfo> GetAllUserInfo(ZkemClient objZkeeper, int machineNumber)
{
    string sdwEnrollNumber = string.Empty, sName = string.Empty, 
                             sPassword = string.Empty, sTmpData = string.Empty;    
    int iPrivilege = 0, iTmpLength = 0, iFlag = 0, idwFingerIndex;
    bool bEnabled = false;

    ICollection<UserInfo> lstFPTemplates = new List<UserInfo>();
    objZkeeper.ReadAllUserID(machineNumber);
    objZkeeper.ReadAllTemplate(machineNumber);
    
    while (objZkeeper.SSR_GetAllUserInfo(machineNumber, out sdwEnrollNumber, 
                      out sName, out sPassword, out iPrivilege, out bEnabled))
    {
        for (idwFingerIndex = 0; idwFingerIndex < 10; idwFingerIndex++)
        {
            if (objZkeeper.GetUserTmpExStr(machineNumber, sdwEnrollNumber, 
                   idwFingerIndex, out iFlag, out sTmpData, out iTmpLength))
            {
                UserInfo fpInfo = new UserInfo();
                fpInfo.MachineNumber = machineNumber;
                fpInfo.EnrollNumber = sdwEnrollNumber;
                fpInfo.Name = sName;
                fpInfo.FingerIndex = idwFingerIndex;
                fpInfo.TmpData = sTmpData;
                fpInfo.Privelage = iPrivilege;
                fpInfo.Password = sPassword;
                fpInfo.Enabled = bEnabled;
                fpInfo.iFlag = iFlag.ToString();

                lstFPTemplates.Add(fpInfo);
            }
        }
    }
    return lstFPTemplates;
}

 

Result

Image 3

Conclusion

Well, that gives you the starting point for your project. With a biometric device in hand, you can now experiment with the source code yourself and play along.

There are numerous procedures and events to be implemented which you can use per you need. Also, you might want to take help from the SDK that I have attached along with this article. There are other SDK/manuals which you can find online for more detailed implementation.

Feel free for any queries and feedbacks.

Peace !!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Assurance IQ
Nepal Nepal
I am a Software Engineer and a developer by profession.

The following things describes me:
1. I am a real time/run time developer : Basically learn and implement things on the fly.
2. Definitely not a technology racist : Love to work and explore any technology that i come across.
3. Prefer Beer over H2O : Code with passion and for fun

I am interested in Research & Development based tasks, exploring, experimenting and trying out new things.
Technologies i have been using up until now are C#, ASP.NET, Win Services, Web Services, Restful Web API, Windows Application, Windows Phone Application, Store Apps, couple of JavaScript frameworks, Xamarin Forms, NodeJS, React, ReactNative, AngularJS, SQL Server, MongoDB, Postgres etc.

Comments and Discussions

 
QuestionClear Log Pin
Bilal Saeed Alam4-Jun-17 18:47
Bilal Saeed Alam4-Jun-17 18:47 
QuestionNot able to add user to device Pin
Ranijt-Singh4-Jun-17 15:48
Ranijt-Singh4-Jun-17 15:48 
Questionwhat is the other way of implement zkemkeeper Pin
Supun Rathnayaka12-May-17 1:41
Supun Rathnayaka12-May-17 1:41 
AnswerRe: what is the other way of implement zkemkeeper Pin
Ozesh Thapa14-May-17 23:53
professionalOzesh Thapa14-May-17 23:53 
GeneralRe: what is the other way of implement zkemkeeper Pin
Member 1120444312-Dec-17 21:29
Member 1120444312-Dec-17 21:29 
GeneralRe: what is the other way of implement zkemkeeper Pin
Member 1120444313-Dec-17 2:39
Member 1120444313-Dec-17 2:39 
Questioncan we retrive finger print and save it in database Pin
AZHAR SAYYAD8-May-17 0:52
AZHAR SAYYAD8-May-17 0:52 
AnswerRe: can we retrive finger print and save it in database Pin
Ozesh Thapa8-May-17 23:47
professionalOzesh Thapa8-May-17 23:47 
After you have enrolled a user in the device, An encoded string will be created for that particular user, which is actually your fingerprint template (in the above image - TmpData is your fingerprint templates ). It can be treated as a normal string and hence can be stored in your database. You can export that string template to other ZKTECO devices to verify that user.
QuestionConnect Via USB Port Pin
Member 1260309521-Apr-17 3:14
Member 1260309521-Apr-17 3:14 
AnswerMessage Closed Pin
21-Apr-17 15:10
Member 1260309521-Apr-17 15:10 
QuestionAbout devices from other branches Pin
Eternal Will6-Apr-17 22:43
Eternal Will6-Apr-17 22:43 
AnswerRe: About devices from other branches Pin
Ozesh Thapa9-May-17 0:05
professionalOzesh Thapa9-May-17 0:05 
Questionerror Pin
Member 130913464-Apr-17 20:43
Member 130913464-Apr-17 20:43 
AnswerRe: error Pin
Member 130913464-Apr-17 20:50
Member 130913464-Apr-17 20:50 
AnswerRe: error Pin
Ozesh Thapa5-Apr-17 1:39
professionalOzesh Thapa5-Apr-17 1:39 
GeneralRe: error Pin
Member 130913467-Apr-17 21:08
Member 130913467-Apr-17 21:08 
GeneralRe: error Pin
Member 1309134610-Apr-17 5:45
Member 1309134610-Apr-17 5:45 
GeneralRe: error Pin
Ozesh Thapa8-May-17 23:56
professionalOzesh Thapa8-May-17 23:56 
GeneralRe: error Pin
Member 130913469-May-17 5:58
Member 130913469-May-17 5:58 
GeneralRe: error Pin
Ghulam11-Sep-21 2:48
Ghulam11-Sep-21 2:48 
QuestionConnect to local database for verification Pin
Member 1108071822-Mar-17 22:13
Member 1108071822-Mar-17 22:13 
QuestionUrgent help required Pin
ruchi 1301671413-Mar-17 22:57
ruchi 1301671413-Mar-17 22:57 
AnswerRe: Urgent help required Pin
Ozesh Thapa14-Mar-17 1:23
professionalOzesh Thapa14-Mar-17 1:23 
QuestionDevice disconnected after sometime Pin
Khushi837-Mar-17 20:06
Khushi837-Mar-17 20:06 
AnswerRe: Device disconnected after sometime Pin
Ozesh Thapa12-Mar-17 20:52
professionalOzesh Thapa12-Mar-17 20:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.