Вы находитесь на странице: 1из 4

6/13/2019 RFID Projek: Student Attendance Class using MySQL database - Projek Elektronik

RFID Projek: Student Attendance Class using MySQL database


 Abg Lan  August 29, 2015  Senarai Artikel, Uncategorized, Vedios, Videos  13,497 Views

RFID Projek: Student Attendance Class using MySQL database

Hello Frenz,

This is source code for RFID MySQL attendant class. You can just copy and paste this to your visual basic.

I’m not using arduino to read the RFID but directly through RS232 com port.

1 Imports System
2 Imports System.IO.Ports
3 Imports System.Data.OleDb
4 Public Class Form1
5 Dim Com As String = "Com1"
6 Dim BaudRate As Integer = 9600
7 Dim DataRead As String
8 Dim DataRead2 As String
9 Dim Comport As New SerialPort(Com, BaudRate, Parity.None, 8, StopBits.One)
10 Dim ds As New DataSet()
11 Dim intCurrentIndex As Integer = 0
12 Dim da As New OleDbDataAdapter()
13 Dim conn As New OleDbConnection()
14 Dim dr As OleDbDataReader
15  
16 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
17 Try
18 Comport.Open()
19 If Comport.IsOpen Then
20 SerialData.Text = "Connected" & vbCr
21 Console.Write("Connected")
22 End If
23 Catch ex As System.IO.IOException
24 MsgBox(ex.Message)
25 SerialData.Text = "Disconnect" & vbCr
26 End Try
27 End Sub
28  
29 Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
30 If Comport.IsOpen Then
31 Comport.Close()
32 End If 
33 End Sub

hobbystore.com.my/tutorial/rfid-projek-student-attendance-class-using-mysql-database/ 1/4
6/13/2019 RFID Projek: Student Attendance Class using MySQL database - Projek Elektronik
34  
35  
36 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
37 For Each sp As String In My.Computer.Ports.SerialPortNames
38 ComboBox1.Items.Add(sp)
39  
40 Next
41 DBconnect()
42 If Not Comport.IsOpen Then
43 SerialData.Text = "Disconnect" & vbCr
44 End If
45 AddHandler Comport.DataReceived, AddressOf SerialPort1_DataReceived
46 End Sub
47  
48 Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial
49 Dim sp As SerialPort = CType(sender, SerialPort)
50 Dim indata As String = sp.ReadTo(Chr(3)) 'sp.ReadExisting()
51 Console.WriteLine("Data Received:")
52 Console.Write(indata)
53 DataRead = indata
54 End Sub
55  
56 Private Sub ComboBox2_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectionChan
57 Comport.BaudRate = ComboBox2.SelectedItem
58 End Sub
59  
60 Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChan
61 Com = ComboBox1.SelectedItem
62 Comport.PortName = Com
63 End Sub
64  
65 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
66 If (DataRead <> DataRead2) Then
67 DataRead2 = DataRead 'Mid(DataRead, 2, 10)
68 SerialData.Text = SerialData.Text & vbCrLf + Mid(DataRead2, 2, 10)
69 Tag_id.Text = Mid(DataRead, 2, 10)
70 Console.WriteLine("data=" & DataRead2)
71 Button4_Click(sender, e)
72  
73 End If
74 End Sub
75  
76 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
77 SerialData.Text = ""
78 End Sub
79  
80 Private Sub DBconnect()
81 conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileSystem.CurDir & "\sample.mdb;User Id=admin;Password=;
82  
83 da.SelectCommand = New OleDbCommand("SELECT ID,Tag_id, Nama,Keterangan,Group,Harga FROM RFID")
84 da.SelectCommand.Connection = conn
85  
86 da.UpdateCommand = New OleDbCommand("UPDATE [RFID] SET [Nama]=@Nama, [Keterangan]=@Keterangan, [Group]=@Group, Harga=" & harga.Text
87 da.UpdateCommand.Connection = conn
88 ' da.UpdateCommand.Parameters.Add("@FirstName", OleDbType.VarChar, 40, "FirstName")
89 ' da.UpdateCommand.Parameters.Add("@LastName", OleDbType.VarChar, 40, "LastName")
90 ' da.UpdateCommand.Parameters.Add("@Location", OleDbType.VarChar, 40, "Location")
91 ' da.UpdateCommand.Parameters.Add("@EmployeeID", OleDbType.Integer, 5, "EmployeeID") '.SourceVersion = DataRowVersion.Original
92 da.UpdateCommand.Parameters.AddWithValue("@Tag_id", Tag_id.Text)
93 da.UpdateCommand.Parameters.AddWithValue("@Nama", Nama.Text)
94 da.UpdateCommand.Parameters.AddWithValue("@Keterangan", Keterangan.Text)
95 da.UpdateCommand.Parameters.AddWithValue("@Group", Group.Text)
96 da.UpdateCommand.Parameters.AddWithValue("@Harga", harga.Text)
97  
98  
99 da.InsertCommand = New OleDbCommand("INSERT INTO tbl_Master(FirstName, LastName, Location) VALUES(@FirstName,@LastName,@Location)")
100 da.InsertCommand.Connection = conn
101 da.InsertCommand.Parameters.Add("@FirstName", OleDbType.VarChar, 40, "FirstName")
102 da.InsertCommand.Parameters.Add("@LastName", OleDbType.VarChar, 40, "LastName")
103 da.InsertCommand.Parameters.Add("@Location", OleDbType.VarChar, 40, "Location")
104  
105 da.DeleteCommand = New OleDbCommand("DELETE FROM tbl_Master WHERE EmployeeID = @EmployeeID")
106 da.DeleteCommand.Connection = conn
107 da.DeleteCommand.Parameters.Add("@EmployeeID", OleDbType.Integer, 5, "EmployeeID")
108  
109 da.Fill(ds)
110 ' MsgBox(ds.Tables(0).Rows(0).Item().ToString())
111 conn.Close()
112 If ds.Tables(0).Rows.Count > 0 Then 'Check to see if the table is empty
113 FillFields()
114 End If
115 ' da.DeleteCommand.Dispose()
116 'da.UpdateCommand.Dispose()
117 'da.InsertCommand.Dispose()
118 'da.SelectCommand.Dispose()
119 End Sub
120 Private Sub FillFields()
121 Tag_id.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Tag_id").ToString()
122 Nama.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Nama").ToString()
123 Keterangan.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Keterangan").ToString()
124 Group.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Group").ToString() 
125 harga.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Harga").ToString()

hobbystore.com.my/tutorial/rfid-projek-student-attendance-class-using-mysql-database/ 2/4
6/13/2019 RFID Projek: Student Attendance Class using MySQL database - Projek Elektronik
126 End Sub
127  
128 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
129 'conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileSystem.CurDir & "\sample.mdb;User Id=admin;Passwo
130 Dim strSql As String = "SELECT*FROM RFID WHERE Tag_ID='" & Tag_id.Text & "'"
131 Dim cmd As OleDbCommand = New OleDbCommand(strSql, conn)
132 conn.Open()
133 Dim MyImage As String = Application.StartupPath & "\" & Tag_id.Text & ".jpg"
134 dr = cmd.ExecuteReader
135 PictureBox1.Load(MyImage)
136 ShowMyImage(MyImage, 174, 161)
137  
138 While dr.Read()
139 Tag_id.Text = dr.Item("Tag_id")
140 Nama.Text = dr.Item("Nama")
141 Keterangan.Text = dr.Item("Keterangan")
142 Group.Text = dr.Item("Group")
143 ' harga.Text = dr.Item("Harga")
144 End While
145 conn.Close()
146 Dim Item As New ListViewItem
147 With ListView1.Items.Add(Tag_id.Text)
148 .SubItems.Add(Nama.Text)
149 .SubItems.Add(Keterangan.Text)
150 .SubItems.Add(Group.Text)
151 .SubItems.Add(harga.Text)
152 End With
153
154  
155 End Sub
156 Private Sub FillFields_1()
157 Nama.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Nama").ToString()
158 Keterangan.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Keterangan").ToString()
159 Group.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Group").ToString()
160 harga.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Harga").ToString()
161 End Sub
162  
163 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
164 Dim strSql As String = "UPDATE [RFID] SET [Nama]='" & Nama.Text & "', [Keterangan]='" & Keterangan.Text & "', [Group]='" & Group.Te
165 Dim cmd As New OleDbCommand(strSql, conn)
166 Dim Asx As Integer
167 Try
168 conn.Open()
169 Asx = cmd.ExecuteScalar
170 conn.Close()
171 MsgBox(Asx)
172 Catch ex As Exception
173  
174 MsgBox(ex.Message)
175  
176 End Try
177  
178  
179 End Sub
180  
181 Private MyImage As Bitmap
182  
183 Public Sub ShowMyImage(ByVal fileToDisplay As String, ByVal xSize As Integer, _
184 ByVal ySize As Integer)
185 ' Sets up an image object to be displayed.
186 If (MyImage IsNot Nothing) Then
187 MyImage.Dispose()
188 End If
189  
190 ' Stretches the image to fit the pictureBox.
191 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
192 MyImage = New Bitmap(fileToDisplay)
193 pictureBox1.ClientSize = New Size(xSize, ySize)
194 pictureBox1.Image = CType(MyImage, Image)
195 End Sub
196  
197 Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
198 harga.Text = TimeOfDay
199 End Sub
200  
201 Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
202 Dim i, j As Byte
203 i = rf_request(0, &H52, j)
204 If (i <> 0) Then
205 MsgBox("Request Fail!")
206 Exit Sub
207 End If
208 MsgBox("Request Success")
209 End Sub
210 End Class


hobbystore.com.my/tutorial/rfid-projek-student-attendance-class-using-mysql-database/ 3/4
6/13/2019 RFID Projek: Student Attendance Class using MySQL database - Projek Elektronik


hobbystore.com.my/tutorial/rfid-projek-student-attendance-class-using-mysql-database/ 4/4

Вам также может понравиться