Great, exactly what i was looking for ![]()
Imports System.IO.Ports
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Private Delegate Sub myDelegate(ByVal Buffer As String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
ConnectionPanel.Focus()
ComboBoxBaudRate.SelectedIndex = 0
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
SerialPort1.ReadTimeout = 50
Dim sData As String = Nothing
Try
sData = SerialPort1.ReadLine
Catch ex As Exception
End Try
Me.BeginInvoke((New myDelegate(AddressOf DisplayData)), sData)
End Sub
Private Sub DisplayData(ByVal sdata As String)
Dim chartvalue As Integer
Dim txtarray As String() = Split(sdata, ",")
If txtarray(0) = "Sensor1" Then
TextBox1.Text = txtarray(1) & " %"
Integer.TryParse(txtarray(1), chartvalue)
Chart1.Series("Series1").Points.Add(chartvalue)
End If
If txtarray(0) = "Sensor2" Then
TextBox2.Text = txtarray(1) & " %"
Integer.TryParse(txtarray(1), chartvalue)
Chart1.Series("Series2").Points.Add(chartvalue)
End If
End Sub
Private Sub ButtonScanPort_Click(sender As Object, e As EventArgs) Handles ButtonScanPort.Click
ConnectionPanel.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 ComboBoxPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxPort.SelectedIndexChanged
ConnectionPanel.Focus()
End Sub
Private Sub ComboBoxPort_DropDown(sender As Object, e As EventArgs) Handles ComboBoxPort.DropDown
ConnectionPanel.Focus()
End Sub
Private Sub LinkLabel_LinkClicked_1(sender As Object, e As LinkLabelLinkClickedEventArgs)
End Sub
Private Sub ComboBoxPort_Click(sender As Object, e As EventArgs) Handles ComboBoxPort.Click
If LabelStatus.Text = "Status : Connected" Then
MsgBox("Connection in progress, please Disconnect to change COM.", MsgBoxStyle.Critical, "Warning !!!")
Return
End If
End Sub
Private Sub ComboBoxBaudRate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxBaudRate.SelectedIndexChanged
ConnectionPanel.Focus()
End Sub
Private Sub ComboBoxBaudRate_DropDown(sender As Object, e As EventArgs) Handles ComboBoxBaudRate.DropDown
ConnectionPanel.Focus()
End Sub
Private Sub ComboBoxBaudRate_Click(sender As Object, e As 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 ButtonConnect_Click(sender As Object, e As EventArgs) Handles ButtonConnect.Click
ConnectionPanel.Focus()
Try
SerialPort1.BaudRate = ComboBoxBaudRate.SelectedItem
SerialPort1.PortName = ComboBoxPort.SelectedItem
SerialPort1.Open()
TimerSerial.Start()
LabelStatus.Text = "Status : Connected"
ButtonConnect.SendToBack()
ButtonDisconnect.BringToFront()
PictureBoxConnectionStatus.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(sender As Object, e As EventArgs) Handles ButtonDisconnect.Click
ConnectionPanel.Focus()
TimerSerial.Stop()
SerialPort1.Close()
ButtonDisconnect.SendToBack()
ButtonConnect.BringToFront()
LabelStatus.Text = "Status : Disconnect"
PictureBoxConnectionStatus.Visible = True
PictureBoxConnectionStatus.BackColor = Color.Red
End Sub
Private Sub ChartSettings()
Chart1.BackColor = Color.Bisque
Chart1.Series("Series1").ChartType = SeriesChartType.Spline
Chart1.Series("Series2").ChartType = SeriesChartType.Spline
Chart1.Series("Series1").BorderWidth = 2
Chart1.Series("Series2").BorderWidth = 2
Chart1.Series("Series1").Color = Color.Red
Chart1.Series("Series2").Color = Color.DarkBlue
With Chart1.ChartAreas(0).AxisX
.Maximum = 300
.Minimum = 0
.LabelStyle.Format = "0S"
.Title = "Humidity"
.TitleFont = New Font(New FontFamily("Arial"), 9, FontStyle.Bold)
.MajorGrid.Interval = 1
.MajorGrid.Enabled = True
.MajorTickMark.Enabled = True
.MajorTickMark.Interval = 1
.MajorTickMark.Size = 2
.LabelStyle.Enabled = True
.LabelStyle.Interval = 10
End With
With Chart1.ChartAreas(0).AxisY
.Maximum = 100
.Minimum = 0
.MajorGrid.Interval = 10
.MajorGrid.Enabled = True
.MajorTickMark.Enabled = True
.MajorTickMark.Interval = 10
.MajorTickMark.Size = 2
.LabelStyle.Enabled = True
.LabelStyle.Interval = 10
End With
Chart1.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = False
Chart1.ChartAreas(0).AxisX.ScaleView.Zoom(1, 59)
End Sub
'====================================== Connection Panel End ================================================'
End Class
