0% found this document useful (0 votes)
16 views

Appendix A Source Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Appendix A Source Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Appendix A

RELEVANT SOURCE CODE

Imports System.Data.OleDb

Public Class Form4

Dim da As New OleDbDataAdapter

Dim dset As New DataSet

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load

da = New OleDbDataAdapter("Select * from account", conn)

dset = New DataSet

da.Fill(dset, "account")

dgv.DataSource = dset.Tables("account").DefaultView

End Sub

Function populate()

da = New OleDbDataAdapter("Select * from account", conn)

dset = New DataSet

da.Fill(dset, "account")

dgv.DataSource = dset.Tables("account").DefaultView

Return True

End Function

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnAdd.Click
Dim comm As OleDbCommand

connect()

comm = New OleDbCommand

comm.Connection = conn

comm.CommandText = "Insert into account values('" & txtID.Text & "', '" & txtFName.Text
& "', '" & txtLName.Text & "','" & txtUName.Text & "' , '" & txtPassword.Text & "')"

comm.ExecuteNonQuery()

populate()

MessageBox.Show("Successfully Added")

txtID.Clear()

txtFName.Clear()

txtLName.Clear()

txtUName.Clear()

txtPassword.Clear()

End Sub

Private Sub dgv_CellClick(ByVal sender As Object, ByVal e As


System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellClick

txtID.Text = dgv.Rows(e.RowIndex).Cells(0).Value.ToString

txtFName.Text = dgv.Rows(e.RowIndex).Cells(1).Value.ToString

txtLName.Text = dgv.Rows(e.RowIndex).Cells(2).Value.ToString

txtUName.Text = dgv.Rows(e.RowIndex).Cells(3).Value.ToString
txtPassword.Text = dgv.Rows(e.RowIndex).Cells(4).Value.ToString

End Sub

Private Sub dgv_CellContentClick(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick

End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnUpdate.Click

Dim comm As OleDbCommand

connect()

comm = New OleDbCommand

comm.Connection = conn

comm.CommandText = "Update account set Fname='" & txtFName.Text & "',Lname ='" &
txtLName.Text & " ', Uname= '" & txtUName.Text & "',Passwd='" & txtPassword.Text & "'
where ID='" & txtID.Text & "'"

comm.ExecuteNonQuery()

populate()

MessageBox.Show("Account Updated")

txtID.Clear()

txtFName.Clear()

txtLName.Clear()
txtUName.Clear()

txtPassword.Clear()

End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnDelete.Click

Dim comm As OleDbCommand

connect()

comm = New OleDbCommand

comm.Connection = conn

comm.CommandText = "Delete from account where ID='" & txtID.Text & "'"

comm.ExecuteNonQuery()

MessageBox.Show("Successfully Deleted")

populate()

txtID.Clear()

txtFName.Clear()

txtLName.Clear()

txtUName.Clear()

txtPassword.Clear()

End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnCancel.Click

txtID.Clear()

txtFName.Clear()

txtLName.Clear()

txtUName.Clear()

txtPassword.Clear()

End Sub

End Class

Imports System

Imports System.IO.Ports

Public Class Form1

Dim vpb_sy, vpb_ly As Integer

Dim TempL, HumL As Integer

Dim Temp, Hum, TempResult, HumResult As String

Dim TempToProgressBar As Single

Dim ChartLimit As Integer = 30

Dim StrSerialIn, StrSerialInRam, value As String

Dim currenttime As String


Dim messagetime, messagetime1, messagetime2, messagetime3, messagetime4,
messagetime5 As String

Dim connect As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load

Timer1.Enabled = True

Me.CenterToScreen()

PanelConnection.Focus()

CircularProgressBarHumidity.Value = 0

ComboBoxBaudRate.SelectedIndex = 0

btnON.Enabled = False

btnOFF.Enabled = False

For i = 0 To 30 Step 1

Chart1.Series("Humidity ").Points.AddY(0)

If Chart1.Series(0).Points.Count = ChartLimit Then

Chart1.Series(0).Points.RemoveAt(0)

End If
Chart2.Series("Temperature").Points.AddY(0)

If Chart2.Series(0).Points.Count = ChartLimit Then

Chart2.Series(0).Points.RemoveAt(0)

End If

Next

Chart1.ChartAreas(0).AxisY.Maximum = 180

Chart1.ChartAreas(0).AxisY.Minimum = -20

Chart1.ChartAreas("ChartArea1").AxisX.LabelStyle.Enabled = False

Chart2.ChartAreas(0).AxisY.Maximum = 70

Chart2.ChartAreas(0).AxisY.Minimum = -30

Chart2.ChartAreas("ChartArea1").AxisX.LabelStyle.Enabled = False

PictureBoxPBTemp.Height = 0

End Sub

Private Sub ComboBoxPort_Click(ByVal sender As Object, ByVal e As System.EventArgs)


Handles ComboBoxPort.Click

If LabelStatus.Text = "Status : Connected" Then

MsgBox("Conncetion in progress, please Disconnect to change COM.",


MsgBoxStyle.Critical, "Warning !!!")

Return

End If

End Sub
Private Sub ComboBoxPort_DropDown(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ComboBoxPort.DropDown

PanelConnection.Focus()

End Sub

Private Sub ComboBoxPort_SelectedIndexChanged(ByVal sender As Object, ByVal e As


System.EventArgs) Handles ComboBoxPort.SelectedIndexChanged

PanelConnection.Focus()

End Sub

Private Sub ComboBoxBaudRate_Click(ByVal sender As Object, ByVal e As


System.EventArgs) Handles ComboBoxBaudRate.Click

If LabelStatus.Text = "Status : Connected" Then

MsgBox("Conncetion in progress, please Disconnect to change Baud Rate.",


MsgBoxStyle.Critical, "Warning !!!")

Return

End If

End Sub

Private Sub ComboBoxBaudRate_DropDown(ByVal sender As Object, ByVal e As


System.EventArgs) Handles ComboBoxBaudRate.DropDown

PanelConnection.Focus()

End Sub
Private Sub ComboBoxBaudRate_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ComboBoxBaudRate.SelectedIndexChanged

PanelConnection.Focus()

End Sub

Private Sub ButtonScanPort_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles ButtonScanPort.Click

PanelConnection.Focus()

If LabelStatus.Text = "Status : Connected" Then

MsgBox("Conncetion in progress, please Disconnect to scan the new port.",


MsgBoxStyle.Critical, "Warning !!!")

Return

End If

ComboBoxPort.Items.Clear()

Dim myPort As Array

Dim i As Integer

myPort = IO.Ports.SerialPort.GetPortNames()

ComboBoxPort.Items.AddRange(myPort)

i = ComboBoxPort.Items.Count

i=i-i

Try

ComboBoxPort.SelectedIndex = i

ButtonConnect.Enabled = True

Catch ex As Exception
MsgBox("Com port not detected", MsgBoxStyle.Critical, "Warning !!!")

ComboBoxPort.Text = ""

ComboBoxPort.Items.Clear()

Return

End Try

ComboBoxPort.DroppedDown = True

End Sub

Private Sub ButtonConnect_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles ButtonConnect.Click

PanelConnection.Focus()

Try

SerialPort1.BaudRate = ComboBoxBaudRate.SelectedItem

SerialPort1.PortName = ComboBoxPort.SelectedItem

SerialPort1.Open()

TimerSerial.Start()

LabelStatus.Text = "Status : Connected"

ButtonConnect.SendToBack()

ButtonDisconnect.BringToFront()

PictureBoxStatusConnection.BackColor = Color.Green

Catch ex As Exception
MsgBox("Please check the Hardware, COM, Baud Rate and try again.",
MsgBoxStyle.Critical, "Connection failed !!!")

End Try

End Sub

Private Sub ButtonDisconnect_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles ButtonDisconnect.Click

PanelConnection.Focus()

TimerSerial.Stop()

SerialPort1.Close()

ButtonDisconnect.SendToBack()

ButtonConnect.BringToFront()

LabelStatus.Text = "Status : Disconnect"

PictureBoxStatusConnection.Visible = True

PictureBoxStatusConnection.BackColor = Color.Red

End Sub

Function MapVPB(ByVal X As Single, ByVal In_min As Single, ByVal In_max As Single,


ByVal Out_min As Single, ByVal Out_max As Single) As Integer

Dim A As Single

Dim B As Single

A = X - In_min

B = Out_max - Out_min

A=A*B

B = In_max - In_min
A=A/B

MapVPB = A + Out_min

End Function

Private Sub TimerSerial_Tick(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TimerSerial.Tick

Try

StrSerialIn = SerialPort1.ReadExisting

Dim TB As New TextBox

TB.Multiline = True

TB.Text = StrSerialIn

If TB.Lines.Count > 0 Then

If TB.Lines(0) = "Failed to read from DHT sensor!" Then

TimerSerial.Stop()

SerialPort1.Close()

LabelStatus.Text = "Status : Disconnect"

ButtonDisconnect.SendToBack()

ButtonConnect.BringToFront()

PictureBoxStatusConnection.Visible = True

PictureBoxStatusConnection.BackColor = Color.Red

MsgBox("Failed to read from DHT sensor !!!, Please check the Hardware and
Please connect again.", MsgBoxStyle.Critical, "Connection failed !!!")
Return

End If

StrSerialInRam = TB.Lines(0).Substring(0, 1)

If StrSerialInRam = "H" Then

Hum = TB.Lines(0)

HumL = Hum.Length

Else

Hum = Hum

End If

StrSerialInRam = TB.Lines(1).Substring(0, 1)

If StrSerialInRam = "T" Then

Temp = TB.Lines(1)

TempL = Temp.Length

Else

Temp = Temp

End If

HumResult = Mid(Hum, 2, HumL)

TempResult = Mid(Temp, 2, TempL)

TempToProgressBar = TempResult
CircularProgressBarHumidity.Value = HumResult

CircularProgressBarHumidity.Text = CircularProgressBarHumidity.Value & " %"

LabelTemperature.Text = TempResult & " °C"

vpb_sy = MapVPB(TempToProgressBar, -20.0, 60.0, 0, 120)

If vpb_sy > 120 Then

vpb_sy = 120

End If

If vpb_sy < 0 Then

vpb_sy = 0

End If

PictureBoxPBTemp.Height = PictureBoxPBTempBack.Height * vpb_sy / 120

vpb_ly = (PictureBoxPBTempBack.Height - vpb_sy) +


PictureBoxPBTempBack.Location.Y

PictureBoxPBTemp.Location = New Point(PictureBoxPBTemp.Location.X, vpb_ly)

Chart1.Series("Humidity ").Points.AddY(HumResult)

If Chart1.Series(0).Points.Count = ChartLimit Then

Chart1.Series(0).Points.RemoveAt(0)

End If

Chart2.Series("Temperature").Points.AddY(TempResult)

If Chart2.Series(0).Points.Count = ChartLimit Then


Chart2.Series(0).Points.RemoveAt(0)

End If

If PictureBoxStatusConnection.Visible = True Then

PictureBoxStatusConnection.Visible = False

ElseIf PictureBoxStatusConnection.Visible = False Then

PictureBoxStatusConnection.Visible = True

End If

End If

Catch ex As Exception

TimerSerial.Stop()

SerialPort1.Close()

LabelStatus.Text = "Status : Disconnect"

ButtonDisconnect.SendToBack()

ButtonConnect.BringToFront()

PictureBoxStatusConnection.BackColor = Color.Red

MsgBox("Please check the Hardware and Please connect again." & ex.Message,
MsgBoxStyle.Critical, "Connection failed !!!")

Return

End Try

End Sub
Private Sub RdTemp_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RdTemp.CheckedChanged

TxtMinTemp.Enabled = False

TxtMaxTemp.Enabled = False

BtnTemp.Enabled = False

btnResTemp.Enabled = False

If RdTemp.Checked = True Then

TxtMinTemp.Enabled = True

TxtMaxTemp.Enabled = True

BtnTemp.Enabled = True

btnResTemp.Enabled = True

End If

End Sub

Private Sub RdTime_CheckedChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles RdTime.CheckedChanged

mtbMist1.Enabled = False

mtbMist2.Enabled = False

mtbMist3.Enabled = False
mtbMist1End.Enabled = False

mtbMist2End.Enabled = False

mtbMist3End.Enabled = False

btnMist1.Enabled = False

btnMist2.Enabled = False

btnMist3.Enabled = False

btnResMist1.Enabled = False

btnResMist2.Enabled = False

btnResMist3.Enabled = False

btnMist3.Enabled = False

cmbMist1.Enabled = False

cmbMist2.Enabled = False

cmbMist3.Enabled = False

cmbMist1End.Enabled = False

cmbMist2End.Enabled = False

cmbMist3End.Enabled = False

If RdTime.Checked = True Then


mtbMist1.Enabled = True

mtbMist2.Enabled = True

mtbMist3.Enabled = True

mtbMist1End.Enabled = True

mtbMist2End.Enabled = True

mtbMist3End.Enabled = True

btnMist1.Enabled = True

btnMist2.Enabled = True

btnMist3.Enabled = True

btnResMist1.Enabled = True

btnResMist2.Enabled = True

btnResMist3.Enabled = True

btnMist3.Enabled = True

cmbMist1.Enabled = True

cmbMist2.Enabled = True

cmbMist3.Enabled = True

cmbMist1End.Enabled = True

cmbMist2End.Enabled = True

cmbMist3End.Enabled = True

End If

End Sub
Private Sub BtnTemp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles BtnTemp.Click

Try

If (LabelTemperature.Text >= TxtMinTemp.Text And LabelTemperature.Text <=


TxtMaxTemp.Text) Then

connect = True

value = "1"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

Else

value = "0"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

End If

Catch ex As Exception

MsgBox("Please check the Hardware and Please connect again." & ex.Message,
MsgBoxStyle.Critical, "Connection failed !!!")

End Try
TxtMinTemp.Enabled = False

TxtMaxTemp.Enabled = False

End Sub

Public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Timer1.Tick

lblTimer.Text = Date.Now.ToString("MMM dd yyyy")

currenttime = TimeOfDay.ToString("hh:mm:ss tt")

lblTime.Text = currenttime

messagetime = mtbMist1.Text + " " + cmbMist1.Text

messagetime1 = mtbMist1End.Text + " " + cmbMist1End.Text

If lblTime.Text = messagetime Then

connect = True

value = "1"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

ElseIf lblTime.Text = messagetime1 Then

Timer2.Stop()

value = "0"
SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

End If

messagetime2 = mtbMist2.Text + " " + cmbMist2.Text

messagetime3 = mtbMist2End.Text + " " + cmbMist2End.Text

If lblTime.Text = messagetime2 Then

connect = True

value = "1"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

ElseIf lblTime.Text = messagetime3 Then

Timer2.Stop()

value = "0"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

End If

messagetime4 = mtbMist3.Text + " " + cmbMist3.Text

messagetime5 = mtbMist3End.Text + " " + cmbMist3End.Text


If lblTime.Text = messagetime4 Then

connect = True

value = "1"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

ElseIf lblTime.Text = messagetime5 Then

Timer2.Stop()

value = "0"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

End If

End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

TxtMinTemp.Enabled = True

TxtMaxTemp.Enabled = True

End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Timer2.Tick

End Sub
Public Sub btnMist1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnMist1.Click

mtbMist1.Enabled = False

cmbMist1.Enabled = False

mtbMist1End.Enabled = False

cmbMist1End.Enabled = False

End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnConnect1.Click

If connect = False Then

For Each PortDispose As String In

My.Computer.Ports.SerialPortNames

cmbMotor.Items.Add(PortDispose)

Next

If cmbMotor.Items.Count > 0 Then

cmbMotor.Text = cmbMotor.Items(0)
SerialPort2.Open()

btnON.Enabled = True

btnOFF.Enabled = True

cmbMotor.Enabled = False

MsgBox("CONNECTED")

lblStatus1.Text = "Status : Connected"

btnConnect1.SendToBack()

btnDisconnect1.BringToFront()

PictureBoxStatusConnection1.BackColor = Color.Green

connect = True

Else

MsgBox("NO PORT FOUND")

End If

Else

SerialPort2.Close()

MsgBox("DISCONECTED")

connect = False

End If

End Sub
Private Sub btnON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnON.Click

If connect = True Then

value = "1"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

End If

End Sub

Private Sub btnOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnOFF.Click

If connect = True Then

value = "0"

SerialPort2.Write(value)

SerialPort2.DiscardOutBuffer()

End If

End Sub

Private Sub btnMotorScan_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnMotorScan.Click

PanelConnection.Focus()
If lblStatus1.Text = "Status : Connected" Then

MsgBox("Conncetion in progress, please Disconnect to scan the new port.",


MsgBoxStyle.Critical, "Warning !!!")

Return

End If

cmbMotor.Items.Clear()

Dim myPort1 As Array

Dim motor As Integer

myPort1 = IO.Ports.SerialPort.GetPortNames()

cmbMotor.Items.AddRange(myPort1)

motor = cmbMotor.Items.Count

motor = motor - motor

Try

cmbMotor.SelectedIndex = motor

btnConnect1.Enabled = True

Catch ex As Exception

MsgBox("Com port not detected", MsgBoxStyle.Critical, "Warning !!!")

cmbMotor.Text = ""

cmbMotor.Items.Clear()

Return

End Try

cmbMotor.DroppedDown = True

End Sub
Private Sub btnDisconnect1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDisconnect1.Click

SerialPort2.Close()

btnDisconnect1.SendToBack()

btnConnect1.BringToFront()

lblStatus1.Text = "Status : Disconnect"

PictureBoxStatusConnection1.Visible = True

PictureBoxStatusConnection1.BackColor = Color.Red

End Sub

Private Sub btnResTemp_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnResTemp.Click

TxtMinTemp.Enabled = True

TxtMaxTemp.Enabled = True

End Sub

Private Sub ManageUserToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles ManageUserToolStripMenuItem.Click

Form4.Show()

End Sub
Private Sub LogoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles LogoutToolStripMenuItem.Click

Dim ask As MsgBoxResult = MsgBox("Are you sure you want to logout?",


MsgBoxStyle.YesNo)

If ask = MsgBoxResult.Yes Then

Me.Close()

Form3.Show()

ElseIf ask = MsgBoxResult.No Then

Me.Show()

End If

End Sub

Private Sub btnMist2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnMist2.Click

mtbMist2.Enabled = False

cmbMist2.Enabled = False

mtbMist2End.Enabled = False
cmbMist2End.Enabled = False

End Sub

Private Sub btnMist3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnMist3.Click

mtbMist3.Enabled = False

cmbMist3.Enabled = False

mtbMist3End.Enabled = False

cmbMist3End.Enabled = False

End Sub

Private Sub btnResMist1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnResMist1.Click

mtbMist1.Enabled = True

cmbMist1.Enabled = True

mtbMist1End.Enabled = True

cmbMist1End.Enabled = True

End Sub

Private Sub btnResMist2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnResMist2.Click
mtbMist2.Enabled = True

cmbMist2.Enabled = True

mtbMist2End.Enabled = True

cmbMist2End.Enabled = True

End Sub

Private Sub btnResMist3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnResMist3.Click

mtbMist3.Enabled = True

cmbMist3.Enabled = True

mtbMist3End.Enabled = True

cmbMist3End.Enabled = True

End Sub

End Class

You might also like