Pull data from Biometric Machine with face and finger recognition to Process and Upload

Pull data from Biometric Machine to Process and Upload

Here is the code to pull data from multiple biometric machines having  face and finger recognition through tcp/ip using same port.

1. Connect with Machines,,
2. Get data and save into Datatable
3. Load csv file havind mapping of Biometric ID with Employee ID
4. Sort the datatable to Process the Data and mark In punch and Out punch in required format.
5. Finally generate the CSV File
6. Alternatively same data can be uploaded directly to HRMS Server database.


Prerequisite:
VB.Net
zkemkeeper.dll
My machines with different brands worked with above dll.
Here is the code

Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
        '  Try
        '' Load Bio ID from CSV file
        Dim bio As New Dictionary(Of String, String)
            Try
                Dim sr As New StreamReader("C:\bio\eid.csv")

                Do While sr.Peek <> -1
                    Dim linearray As String() = sr.ReadLine().Split(",")
                    If linearray.Length = 2 Then
                        bio.Add(linearray(0), linearray(1))
                    End If
                Loop
                ''
                '' to get emp no give id like bio("bio id")
                ''
            Catch ex1 As Exception
                MsgBox("Error reading employee number from eid.csv")
                Return
            End Try
            Dim idwErrorCode As Integer
            Dim sdwEnrollNumber As String = ""
            Dim idwVerifyMode As Integer
            Dim idwInOutMode As Integer
            Dim idwYear As Integer
            Dim idwMonth As Integer
            Dim idwDay As Integer
            Dim idwHour As Integer
            Dim idwMinute As Integer
            Dim idwSecond As Integer
        Dim idwWorkcode As Integer
        Dim attDate() As String = txtDate.Text.Split(".")

        Cursor = Cursors.WaitCursor
            If btnConnect.Text = "Disconnect" Then
                axCZKEM1.Disconnect()
                bIsConnected = False
                btnConnect.Text = "Connect"
                lblState.Text = "Current State:Disconnected"
                Cursor = Cursors.Default
                Return
            End If
        Dim finalDT As New System.Data.DataTable()
        finalDT.Columns.Add("bioid", GetType(Int32))
        finalDT.Columns.Add("pdate", GetType(Int32))
        finalDT.Columns.Add("ptime", GetType(Int32))
        finalDT.Columns.Add("eid", GetType(String))
        finalDT.Columns.Add("adate", GetType(String))
        finalDT.Columns.Add("flag", GetType(String))
        finalDT.Columns.Add("atime", GetType(String))
        '   Dim machine() As String = {"10.8.215.4", "10.8.215.5", "192.168.1.201"}
        Dim machine() As String = txtIPs.Text.Split(",")
        For m As Integer = 0 To machine.Length - 1
            '' Connect to machine one by one
            bIsConnected = axCZKEM1.Connect_Net(machine(m).Trim(), Convert.ToInt32(txtPorts.Text.Trim()))
            If bIsConnected = True Then
                btnConnect.Text = "Disconnect"
                btnConnect.Refresh()
                lblState.Text = "Current State:Connected"
                iMachineNumber = 1 'In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
                axCZKEM1.RegEvent(iMachineNumber, 65535) 'Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
            Else
                axCZKEM1.GetLastError(idwErrorCode)
                MsgBox("Unable to connect the first device,ErrorCode=" & idwErrorCode, MsgBoxStyle.Exclamation, "Error")
                '  Return
            End If
            Cursor = Cursors.Default
            ''' Now download data and process
            Dim iGLCount = 0

            Cursor = Cursors.WaitCursor
            axCZKEM1.EnableDevice(iMachineNumber, False) 'disable the device
            If axCZKEM1.ReadGeneralLogData(iMachineNumber) Then 'read all the attendance records to the memory
                'get records from the memory
                While axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, sdwEnrollNumber, idwVerifyMode, idwInOutMode, idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond, idwWorkcode)
                    iGLCount  = 1
                    Dim bioid As String = sdwEnrollNumber.ToString()
                    Dim pDate = idwYear.ToString() & idwMonth.ToString().PadLeft(2, "0") & idwDay.ToString().PadLeft(2, "0")
                    Dim ptime = idwHour.ToString().PadLeft(2, "0") & idwMinute.ToString().PadLeft(2, "0")
                    Dim aDate = idwDay.ToString().PadLeft(2, "0") & "/" & idwMonth.ToString().PadLeft(2, "0") & "/" & idwYear.ToString()
                    Dim atime = idwHour.ToString().PadLeft(2, "0") & ":" & idwMinute.ToString().PadLeft(2, "0")
                    '' only add records where date is more than dd.mm.yyyy
                    If idwDay >= attDate(0) And idwMonth = attDate(1) And idwYear = attDate(2) Then
                        finalDT.Rows.Add(bioid, pDate, ptime, "0", aDate, "I", atime)
                    End If
                End While
                ''' data recieved from first machine now disconnect
                axCZKEM1.Disconnect()
                bIsConnected = False
                btnConnect.Text = "Connect"
                lblState.Text = "Current State:Disconnected"
                Cursor = Cursors.Default
            Else
                Cursor = Cursors.Default
                axCZKEM1.GetLastError(idwErrorCode)
                If idwErrorCode <> 0 Then
                    MsgBox("Reading data from terminal failed,ErrorCode: " & idwErrorCode, MsgBoxStyle.Exclamation, "Error")
                Else
                    MsgBox("No data from terminal returns!", MsgBoxStyle.Exclamation, "Error")
                End If
            End If
            '' now connect second machine
            axCZKEM1.EnableDevice(iMachineNumber, True) 'enable the device
            Cursor = Cursors.Default
        Next
        '' Now merge both data
        'finalDT.Merge(finalDT1)
        finalDT.DefaultView.Sort = " bioid, pdate, ptime ASC"
        'Dim dtView = finalDT.DefaultView
        'dtView.Sort = " eid, pdate, ptime ASC"
        'Dim finalDT1 = dtView
        Dim bioidnotFound = ""
        finalDT = finalDT.DefaultView.ToTable
            Dim bioidLast = "0"
            Dim pdateLast = 0
            Dim ptimelast = 0
            For Each r As DataRow In finalDT.Rows
                If bioidLast = r(0) And pdateLast = r(1) Then
                    r(5) = "O"
                Else
                    r(5) = "I" '& eidLast & " " & pdateLast
                End If
            ''find emp id
            Try
                r(3) = bio(r(0).ToString)
            Catch ex As Exception
                r(3) = r(0).ToString
                bioidnotFound = bioidnotFound & r(0).ToString & ", "
            End Try
            bioidLast = r(0)
                pdateLast = r(1)
                ptimelast = r(2)
            Next
        DataGridView1.DataSource = finalDT
        'Dim max As DateTime = finalDT.Select("pDate='2019-01-21'").AsEnumerable().Min(Function(r) r.Field(Of DateTime)("pTime"))
        'txtIP.Text = max.ToString
        ' DataGridView1.DataBindings
        ''' Now write to CSV File
        ''' 
        'Build the CSV file data as a Comma separated string.
        Dim csv As String = String.Empty

        'Add the Header row for CSV file.
        'For Each column As DataColumn In finalDT.Columns
        '    csv  = column.ColumnName & ","c
        'Next

        'Add new line.
        '  csv  = vbCr & vbLf

        'Adding the Rows
        Dim lastrecord = ""
        For Each row As DataRow In finalDT.Rows
            Dim j = 0
            If row(3).ToString.StartsWith("E") Then
                Dim newrecord = ""
                For Each cell As String In row.ItemArray
                    'Add the Data rows.
                    If j > 2 And j < finalDT.Columns.Count - 1 Then
                        newrecord = newrecord   cell.ToString().Replace(",", ";") & ","
                    ElseIf j > 2 Then
                        newrecord = newrecord   cell.ToString().Replace(",", ";")
                    End If
                    j = j   1
                Next
                'Add new line. don't add duplicate rows
                If newrecord <> lastrecord Then csv = csv & newrecord & vbCr & vbLf
                lastrecord = newrecord
            End If
        Next

        'Exporting to Excel
        Dim folderPath As String = "C:\bio\"
            If Not System.IO.Directory.Exists(folderPath) Then
                Directory.CreateDirectory(folderPath)
            End If
            File.WriteAllText(folderPath & "Attendence.csv", csv)

        lbLog.Text = "CSV Generated BioID not Found " & bioidnotFound
        ''' CSV File created.
        '''  Now try to push data to remote SQL server
        '''  
        Dim constring = ""

        'Catch ex As Exception
        '    Dim trace As System.Diagnostics.StackTrace = New System.Diagnostics.StackTrace(ex, True)
        '    MsgBox(trace.GetFrame(0).GetFileLineNumber() & ex.Message)


        'End Try

    End Sub
Final Output in CSV Format
Emp No , Date, Flag(In/Out), time
ENI09283,15/01/2020,I,9:12
ENI09283,15/01/2020,O,18:52
ENI09283,16/01/2020,I,08:53
ENI09283,16/01/2020,O,18:32


Written By Vinod Kotiya

Comments

ifykh said…
Hi vinod,

I am Irfan Khan here. I need some work regarding bio matric. Can you give me sometime?
Kindly share your whatsapp number.
Can we pull the data directly from the machine and save it in MS SQL?
Help me.

Popular Posts