1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
| Imports System.IO
Imports MySql.Data.MySqlClient
Public Class Form1
Public ds As New DataSet
Public strSQL As String
Public cmd As New MySqlCommand
Public dr As MySqlDataReader
Public table As New DataTable
Dim DataQ As Queue(Of String) = New Queue(Of String)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If SerialPort1.IsOpen() Then
MessageBox.Show("Disconnect before closing")
e.Cancel = True
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
For Each s In System.IO.Ports.SerialPort.GetPortNames()
lstPorts.Items.Add(s)
Next s
Catch ex As Exception
End Try
End Sub
Private Sub btndisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisconnect.Click
SerialPort1.Close()
Timer1.Stop()
btndisconnect.Enabled = False
timer_for_systemTime.Stop()
MsgBox("All data are saved to a database.")
End Sub
Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconnect.Click
timeref.Start()
btnStop.Enabled = True
btnStart.Enabled = False
If lstPorts.SelectedIndex = -1 Then
MsgBox("Please select a port")
Exit Sub
Else
SerialPort1.RtsEnable = True 'this is for the line communication of our serial port(important)
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 7
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.PortName = lstPorts.SelectedItem.ToString
SerialPort1.Open()
Timer1.Start()
btndisconnect.Enabled = True
btnconnect.Enabled = False
timer_for_systemTime.Start()
timer_for_startTime.Start()
Me.Close()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Query As String
Dim Query2 As String
Dim Query3 As String
SyncLock DataQ
While DataQ.Count > 0
txtRealtime.Text &= DataQ.Dequeue
End While
End SyncLock
'START HERE\\\\\\\\\\\\\\\\\
Dim lv As ListViewItem = lvdata.Items.Add(txtRealtime.Text.Substring(0, 2))
lv.SubItems.Add(txtRealtime.Text.Substring(2, 2))
lv.SubItems.Add(lblsystemdate.Text)
lv.SubItems.Add(lblSystemTime.Text)
'END HERE\\\\\\\\\\\\\\\\\\\
Query = "INSERT INTO data2(Temperature, Humidity, Date, Time) VALUES ('" + txtRealtime.Text.Substring(0, 2) + "', '" + txtRealtime.Text.Substring(2, 2) + "', '" + lblsystemdate.Text + "', '" + lblSystemTime.Text + "')"
Query2 = "INSERT INTO temperature(dataRead, DateRead, TimeRead) VALUES ('" + txtRealtime.Text.Substring(0, 2) + "', '" + lblsystemdate.Text + "', '" + lblSystemTime.Text + "')"
Query3 = "INSERT INTO humidity(rhread, DateRead, TimeRead) VALUES ('" + txtRealtime.Text.Substring(2, 2) + "', '" + lblsystemdate.Text + "', '" + lblSystemTime.Text + "')"
MyCon.Open()
Dim sql As MySqlCommand = New MySqlCommand(Query, MyCon)
sql.ExecuteNonQuery()
Dim sql2 As MySqlCommand = New MySqlCommand(Query2, MyCon)
sql2.ExecuteNonQuery()
Dim sql3 As MySqlCommand = New MySqlCommand(Query3, MyCon)
sql3.ExecuteNonQuery()
MyCon.Close()
End Sub |