

Imports VB = Microsoft.VisualBasic
Imports VB6 = Microsoft.VisualBasic

Public Class ParkingForm
    Public s As Object
    Public k, sss, mm, pp, qq, rr, yy, nm

    Public hh, tt, jj
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)

    Private Sub ParkingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Out(888, 0)

        '
        'k = 0
        k = 0

        ' u = 0
        If serialPort.IsOpen Then
            serialPort.Close()
        End If

        Try
            With serialPort
                .PortName = "COM1"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None

            End With
            serialPort.Open()
            ' Sleep(500)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        'cmdOpen()
    End Sub
   
    
    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
        

        Dim InBuff As String
        InBuff = SerialPort.ReadExisting()
        hello(InBuff)


    End Sub
    Sub hello(ByVal inbuff)
        
        



        'Dim b As Object


        'Dim I As Short
        'Dim c As Short
        's = ""
        ' b = Len(inbuff)
        'For I = 1 To Len(inbuff)
        '     c = Asc(Mid(inbuff, I, 1))


        '    If (c < 32 Or c > 126) Then
        '         s = s & "" & Hex(c) & ""

        '    Else


        '        s = s & Hex(c)

        '    End If


        'Next I


        If k = 1 Then
            k = 0

            s = s & inbuff

            s = s.Substring(0, 10)
            TextBox1.BeginInvoke(New myDelegate(AddressOf updateTextBox), New Object() {})
            'MsgBox(s)
            Change()





        Else
            s = ""
            s = s & inbuff

            k = 1
            Exit Sub

        End If
    End Sub

    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
        '---for receiving plain ASCII text---
        With TextBox1
            .Text = s
            .ScrollToCaret()
        End With
        
    End Sub
    Public Sub updateTextBoxy()
        '---for receiving plain ASCII text---
        With Text4
            .Text = qq
            .ScrollToCaret()
        End With

    End Sub
    Public Sub updateTextBoxz()
         With Text3
            .Text = pp
            .ScrollToCaret()
        End With

    End Sub

    Public Sub updateTextBoxp()

        With Text2
            .Text = mm
            .ScrollToCaret()
        End With

    End Sub
    Private Sub Change()
        Dim Str1 As Object

        Str1 = s
        If con.State = 1 Then
            con.Close()

        End If
        qq = ""
        Text4.BeginInvoke(New myDelegate(AddressOf updateTextBoxy), New Object() {})

        mm = " "
        Text2.BeginInvoke(New myDelegate(AddressOf updateTextBoxp), New Object() {})

        pp = ""
        Text3.BeginInvoke(New myDelegate(AddressOf updateTextBoxz), New Object() {})
          qry("select * from vehicledetails where id='" & Str1 & "'")


        If dr.Read = False Then
            qq = "Invalid User"
            Text4.BeginInvoke(New myDelegate(AddressOf updateTextBoxy), New Object() {})

            Out(888, 4)
            Sleep(2000)
            Out(888, 0)

            dr.Close()
        Else
            hh = dr.Item(0).ToString

            nm = dr.Item(1).ToString
            PictureBox2.ImageLocation = impt + dr.Item(7).ToString
            dr.Close()

            fun1()
            
           
        End If
        Exit Sub




    End Sub

    Private Sub fun1()
        Dim rr
        'If rs.State = 1 Then rs.Close()

        'rs.CursorType = ADODB.CursorTypeEnum.adOpenKeyset
        'rs.LockType = ADODB.LockTypeEnum.adLockOptimistic
        rr = hh.ToString
        rr = hh.ToString
        dr.Close()
        qry("select max(sid) from vehiclelog where cardno='" & hh & "' and outtime is NULL")
        ' MsgBox(hh)



        dr.Read()

        jj = dr.Item(0).ToString & ""
       
        If jj <> "" Then


            'MsgBox(dr.Item(0).ToString & "ii")

            ' MsgBox(jj)


            dr.Close()
            qq = "Vehicle Exit"
            Text4.BeginInvoke(New myDelegate(AddressOf updateTextBoxy), New Object() {})
            mm = nm
            Text2.BeginInvoke(New myDelegate(AddressOf updateTextBoxp), New Object() {})
            tt = VB6.Format(TimeOfDay, "hh:mm:ss")

            qry1("update vehiclelog set outtime='" & tt & "' where cardno='" & hh & "'  and logdate=#" & Today & "# and sid=" & jj & "")
            qry("select  * from vehiclelog where sid=" & jj & "")
            dr.Read()
            Dim st, mt
            st = dr.Item(0).ToString & "," & dr.Item(1).ToString & "," & dr.Item(2).ToString & "," & dr.Item(3).ToString & "," & dr.Item(4).ToString & Chr(13)
            mt = pat & "\park.csv"
            dr.Close()
            My.Computer.FileSystem.WriteAllText(mt, st, True)


            pp = tt & "" & ""
            Text3.BeginInvoke(New myDelegate(AddressOf updateTextBoxz), New Object() {})
            mm = nm
            Text2.BeginInvoke(New myDelegate(AddressOf updateTextBoxp), New Object() {})

        Else
            msgbox("jjjj")
            dr.Close()
            Dim mt
            qry("select amount from vehicledetails where id='" & rr & "'")
            If dr.Read() = True Then
                If Val(dr.Item(0).ToString) < 4 Then
                    dr.Close()

                    MsgBox("No Balance")
                    Exit Sub
                End If





                mt = Val(dr.Item(0).ToString) - 4
                dr.Close()
                qry1("update vehicledetails set amount=" & Val(mt) & " where id='" & rr & "'")

                qry1("Insert into vehiclelog(cardno,logdate,name,intime,amount) values('" & rr.ToString & "',#" & Today & "#,'" & nm & "','" & VB6.Format(TimeOfDay, "hh:mm:ss") & "'," & val(mt) & ")")





                sss = hh


                qq = "Vehicle Entry"
                Text4.BeginInvoke(New myDelegate(AddressOf updateTextBoxy), New Object() {})
                'Text4.Text = "Vehicle Entry"
                mm = nm & " "
                Text2.BeginInvoke(New myDelegate(AddressOf updateTextBoxp), New Object() {})
                'Text2.Text = temprs.Fields(1).Value & " "
                pp = VB6.Format(TimeOfDay, "hh:mm:ss")
                Text3.BeginInvoke(New myDelegate(AddressOf updateTextBoxz), New Object() {})
                ' Text3.Text = VB6.Format(TimeOfDay, "hh:mm:ss")


            End If
           



        End If
        Out(888, 2)
        Sleep(2000)

        Out(888, 0)
        Sleep(2000)
        Out(888, 1)
        Sleep(2000)
        Out(888, 0)

    End Sub
    




    
End Class