Pages

Thursday 9 July 2015

INTEGRATED EMBEDDED SYSTEM WITH Vb.Net- Chapter 2

USB RFID READER







Visit
http://researchdesignlab.com/rfid-reader-usb.html
to buy this product.
                Radio-frequency identification (RFID) is the wireless use of electromagnetic fields to transfer data, for the purposes of automatically identifying and tracking tags attached to objects. The tags contain electronically stored information. Some tags are powered byelectromagnetic induction from magnetic fields produced near the reader. Some types collect energy from the interrogating radio waves and act as a passive transponder. Other types have a local power source such as a battery and may operate at hundreds of meters from the reader. Unlike a barcode, the tag does not necessarily need to be within line of sight of the reader, and may be embedded in the tracked object. Radio frequency identification (RFID) is one method for Automatic Identification and Data Capture (AIDC).

Reading USB RFID data from serial port

                We can use Serial port for Reading the Tag values from the RF-ID Reader. For this we need to connect the RF-ID Reader using the Serial cable to the port and use the relevant COM Port No# to the Serial port.

Normally the System.Net Contains the Serial Port Class and also available in the Toolbox as Serial port component for your Win Forms App
The following code is for reading RFID data from serial port.
Imports System.Data.SqlClient
Imports System.Net.Sockets
Imports System.Text
Public Class Form4
    Public k, pname, desc, pid, recom
    Public Shared temp1, InBuff, smt
    Dim price As Integer
    Dim total As Integer
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream
    Dim readData As String
    Dim infiniteCounter As Integer
    Dim clientip, hostname, ip
    Dim rng As Long
    Dim cntrl As Integer
    Dim n1, n2 As String
    Dim len1, len2, len, res As String
    Dim abc
    Public user As String
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form2.Show()
        Me.Hide()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Hide()
        Recipe.Show()

    End Sub

    
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label11.Text = "Hi" + " " + Form2.TextBox1.Text + " , " + "Welcome you to PSA"
        user = Form2.TextBox1.Text


        total = 0
        k = 0
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If
        Try
            With SerialPort1
                .PortName = "COM5"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
            End With
            SerialPort1.Open()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub

   

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      
        Dim xi As Integer
        xi = Label2.Location.X
        If xi > 3 Then
            xi = xi - 10
        Else
            xi = 613
        End If
        Label2.Location = New Point(xi, 0)

        Dim b1x As Integer
        Dim b2x As Integer
        Dim b3x As Integer
        Dim b4x As Integer
        b1x = Button1.Location.X
        b2x = Button2.Location.X
        b3x = Button3.Location.X
        '      b4x = Button4.Location.X

        If b1x < 320 Then
            b1x = 1062
        Else
            b1x = b1x - 10

        End If
        Button1.Location = New Point(b1x, 608)
        If b2x < 320 Then
            b2x = 1062
        Else
            b2x = b2x - 10

        End If
        Button2.Location = New Point(b2x, 608)
        If b3x < 320 Then
            b3x = 1062
        Else
            b3x = b3x - 10

        End If
        Button3.Location = New Point(b3x, 608)
        If b4x < 320 Then
            b4x = 1062
        Else
            b4x = b4x - 10

        End If
        'Button4.Location = New Point(b4x, 608)

    End Sub

    Public Sub selectdetail()
        conn.Close()
        conn.Open()
        'lbldesc.Text = ""
        'lblid.Text = ""
        'lblprodname.Text = ""
        pid = ""
        pname = ""
        desc = ""
        price = 0
        temp1 = Mid(temp1, 1, 10)


        Dim cmd As New SqlCommand("select * from pinfo1 where pid='" & temp1 & "' ",    conn)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        If dr.Read = True Then
            pid = dr(0).ToString()
            pname = dr(1).ToString()
            desc = dr(2).ToString()
            recom = dr(4).ToString()

            price = dr(3)
            ' PictureBox3.ImageLocation = dr(4).ToString()
           


            lblid.BeginInvoke(New myDelegate(AddressOf updateTextBox), New Object() {})
            lblprodname.BeginInvoke(New myDelegate(AddressOf updateTextBox1), New Object() {})
            lbldesc.BeginInvoke(New myDelegate(AddressOf updateTextBox2), New Object() {})
            lblprice.BeginInvoke(New myDelegate(AddressOf updateTextBox3), New Object() {})
            Label16.BeginInvoke((New myDelegate(AddressOf updateTextBox12)), New Object() {})
            ListBox2.BeginInvoke(New myDelegate(AddressOf updateTextBox5), New Object() {})
            '  ListBox2.BeginInvoke(New myDelegate(AddressOf updateTextBox6), New Object() {})
            lbltotal.BeginInvoke(New myDelegate(AddressOf updateTextBox4), New Object() {})
        End If
        'ListBox1.DisplayMember = pname
        'ListBox1.ValueMember = lblid.Text
        'ListBox2.DisplayMember = price
       
        conn.Close()
    End Sub


    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ' MessageBox.Show("new data is recived")

        'Timer1.Stop()
        'Form4.TextBox1.Text = filename
        Sleep(300)
        'temp = ""
        price = 0
        ' total =
        pname = ""
        ' Static InBuff As String
     
        InBuff = SerialPort1.ReadExisting()
        'InBuff = SerialPort1.ReadLine()



        temp1 = Mid(InBuff, 1, 10)

        conn.Close()
        conn.Open()
        If temp1 = "" Then
            MsgBox("......................")
        Else


            Dim cmd1 As New SqlCommand("select * from cart2 where pid='" & temp1 & "' ", conn)

            Dim dr As SqlDataReader
            dr = cmd1.ExecuteReader()
            If dr.Read = True Then
                pname = dr(1).ToString
                price = dr.GetValue(2)


                lbltotal.BeginInvoke(New myDelegate(AddressOf updateTextBox10), New Object() {})
                ListBox2.BeginInvoke(New myDelegate(AddressOf updateTextBox7), New Object() {})
                ListBox2.BeginInvoke(New myDelegate(AddressOf updateTextBox8), New Object() {})
                conn.Close()
                conn.Open()
                Sleep(100)
                cmd1 = New SqlCommand("delete from cart2 where pid='" & temp1 & "'", conn)
                cmd1.ExecuteNonQuery()



            Else
                'lblid.BeginInvoke(New myDelegate(AddressOf updateTextBox), New Object() {})
                selectdetail()
                cart()
                'abc = ListBox1.SelectedItem
            End If
        End If
    End Sub
    
    Public Sub cart()
        conn.Close()
        conn.Open()
        Dim cmd As New SqlCommand("insert into cart2 values('" & pid & "','" & pname & "','" & price & "')", conn)
        cmd.ExecuteNonQuery()
        'MsgBox(pname & " " & "is added to the cart", MsgBoxStyle.Information, "Result")
        conn.Close()
        updateTextBox9()

    End Sub
    Public Sub updateTextBox9()
        'Dim total As Integer
        Sleep(100)
        total = Val(lbltotal.Text) + price
        ' smt = total


    End Sub

    Public Sub updateTextBox10()
        Sleep(100)
      

        With lbltotal

            total = Val(lbltotal.Text) - price
            .Text = total

            'End If
            '.ScrollToCaret()
        End With
    End Sub
    Public Sub updateTextBox8()
        Sleep(100)
        'With TextBox2

      

        With ListBox2
            .Items.Remove(price.ToString())
            '.ValueMember = lblid.Text
            '.ScrollToCaret()
        End With
    End Sub
    Public Sub updateTextBox7()
        Sleep(100)
        With ListBox2
            .Items.Remove(pname)
            '.ValueMember = lblid.Text
            '.ScrollToCaret()
        End With
    End Sub
    Public Sub updateTextBox5()
        Sleep(100)



 
        With ListBox2
            .Items.Add(pname)
            '.ValueMember = lblid.Text
            '.ScrollToCaret()
        End With
        With ListBox2
            .Items.Add(price.ToString())

            '.ValueMember = price
            '.ScrollToCaret()
        End With
    End Sub
    Public Sub updateTextBox6()
        Sleep(100)
        With ListBox2
            .Items.Add(price.ToString())

            '.ValueMember = price
            '.ScrollToCaret()
        End With
    End Sub
    Public Delegate Sub myDelegate()
    Public Sub updateTextBox4()
        Sleep(100)
        With lbltotal
            ' .Text = ""
            .Text = Val(lbltotal.Text) + price
            '.Text = total + price
            'id = .Text
            '.ScrollToCaret()
        End With
    End Sub
    Public Sub updateTextBox()
        Sleep(100)
        With lblid
            .Text = ""

            .Text = pid.ToString()




        End With
    End Sub
    Public Sub updateTextBox1()
        Sleep(100)
        With lblprodname
            .Text = ""

            .Text = pname
            'id = .Text
            'ListBox1.Items.Add(.Text.ToString)
            '.ScrollToCaret()
        End With
    End Sub
    Public Sub updateTextBox2()
        ' Sleep(100)
        With lbldesc
            .Text = ""

            .Text = desc
            'id = .Text
            '.ScrollToCaret()
        End With
    End Sub

    Public Sub updateTextBox12()
        ' Sleep(100)
        With Label16
            .Text = ""

            .Text = recom
            'id = .Text
            '.ScrollToCaret()
        End With
    End Sub

    Public Sub updateTextBox3()
        ' Sleep(100)
        With lblprice
            .Text = ""

            .Text = price.ToString()
            'id = .Text
            '.ScrollToCaret()
        End With
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Hide()
        bookmark.Show()

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If ListBox2.Items.Count = 0 Then
            bookmark.ListBox2.Items.Clear()
            Me.Hide()
            Form1.Show()
            Form2.TextBox1.Text = ""
            Form2.TextBox2.Text = ""
            Label9.Text = ""
            Label11.Text = ""

        Else
            Label9.Text = "pls remove the items in the cart before logging out"

        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Me.Hide()
        'offer.Show()

    End Sub

   

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Hide()
        search.Show()

    End Sub

    

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click


     



        readData = "Conected to Server ..."
        'msg()
        clientSocket.Connect("127.0.0.1", 8888)
        ' Label1.Text = "Client Socket Program - Server Connected ..."
        serverStream = clientSocket.GetStream()


        For i As Integer = 0 To ListBox2.Items.Count - 1
            'ListBox1.SetSelected(i, True)
            ListBox2.SetSelected(i, True)


            'lst1(i)

            lst2(i)
            msg()



             
        Next i


        ftotal()

        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""


        MsgBox("Bill Has Been Done. Thank You.")
        conn.Close()

        conn.Open()

        Dim cmd3 As New SqlCommand



        cmd3 = New SqlCommand("delete from cart2 ", conn)
        cmd3.ExecuteNonQuery()
        conn.Close()

        Form1.Show()
        Me.Hide()


    End Sub

    Sub lst2(ByVal x)
      
        ListBox2.SetSelected(x, True)


        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(ListBox2.SelectedItem + "$")

            serverStream.Write(outStream, 0, outStream.Length)
            serverStream.Flush()
        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)
        ctThread.Start()




            'System.Text.Encoding.ASCII.GetBytes(ListBox2.SelectedItem + "$")
            'serverStream.Write(outStream, 0, outStream.Length)
            'serverStream.Flush()
            'Dim ctThread1 As Threading.Thread = New Threading.Thread(AddressOf getMessage)
            'ctThread1.Start()



    End Sub
     

    Private Sub msg()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf msg))
        Else

            TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + readData
        End If
    End Sub


    Private Sub getMessage()
        For Me.infiniteCounter = 1 To 3
            infiniteCounter = 1
            serverStream = clientSocket.GetStream()
            Dim buffSize As Integer
            Dim inStream(10024) As Byte
            buffSize = clientSocket.ReceiveBufferSize

            serverStream.Read(inStream, 0, buffSize)
            Dim returndata As String = _
            System.Text.Encoding.ASCII.GetString(inStream)
            'readData = " ." + returndata
            'msg()
        Next

    End Sub
    Sub ftotal()
        ' lbltotal.Text = smt



        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes("Total: " + lbltotal.Text + "$")

        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()
        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)
        ctThread.Start()
    End Sub

    

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click


        Dim b As New SqlCommand


        conn.Close()
        conn.Open()
     

        lbltotal.BeginInvoke(New myDelegate(AddressOf updateTextBox4), New Object() {})
        Dim name1 = TextBox1.Text
        Dim price1 = TextBox2.Text
        b = New SqlCommand("insert into cart2(pid,pname,price) values ('" & TextBox3.Text & "', '" & TextBox1.Text & "', '" & TextBox2.Text & "')", conn)
        b.ExecuteNonQuery()


        MsgBox(" New Item Added")

        ListBox2.Items.Add(TextBox1.Text)
        '.ValueMember = lblid.Text
        '.ScrollToCaret()

        ListBox2.Items.Add(TextBox2.Text)

    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        conn.Close()
        conn.Open()
        Dim cmd As New SqlCommand("select pname from pinfo1 where pid like '%" & TextBox3.Text & "%' ", conn)
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader
        ListBox1.Items.Clear()



        While rd.Read = True
            ListBox1.Items.Add(rd.GetValue(0).ToString)
            'MessageBox.Show("1 item is added")

        End While
        ListBox1.Sorted = True
   
    End Sub

    Private Sub ListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        TextBox1.Text = ListBox1.SelectedItem
        conn.Close()
        conn.Open()
        Dim cmd As New SqlCommand("select * from pinfo1 where pname='" & TextBox1.Text & "'", conn)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        If dr.Read = True Then
            pid = dr(0).ToString()
            pname = dr(1).ToString()
            desc = dr(2).ToString()

            price = dr(3)
            PictureBox3.ImageLocation = dr(4).ToString()
            TextBox3.Text = pid
            TextBox1.Text = pname
            TextBox2.Text = price
        End If

    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        lbltotal.BeginInvoke(New myDelegate(AddressOf updateTextBox10), New Object() {})
        ListBox2.BeginInvoke(New myDelegate(AddressOf updateTextBox7), New Object() {})
        ListBox2.BeginInvoke(New myDelegate(AddressOf updateTextBox8), New Object() {})
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs)
        Form1.Show()

    End Sub

    Private Function this() As Object
        Throw New NotImplementedException
    End Function


    

   

End Class