So, this is my code now, with working scan port and baud rate ![]()
Imports System.IO.Ports
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 SerialPort.DataReceived
SerialPort.ReadTimeout = 50
Dim sData As String = Nothing
Try
sData = SerialPort.ReadLine
Catch ex As Exception
End Try
Me.BeginInvoke((New myDelegate(AddressOf DisplayData)), sData)
End Sub
Private Sub DisplayData(ByVal sdata As String)
Dim txtarray As String() = Split(sdata, ",")
If txtarray.Count = 2 Then
If txtarray(0) = "Sensor1" Then
TextBox1.Text = "Sensor 1 value = " & txtarray(1) & " %"
End If
If txtarray(0) = "Sensor2" Then
TextBox2.Text = "Sensor 2 value = " & txtarray(1) & " %"
End If
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
SerialPort.BaudRate = ComboBoxBaudRate.SelectedItem
SerialPort.PortName = ComboBoxPort.SelectedItem
SerialPort.Open()
TimerSerial.Start()
LabelStatus.Text = "Status : Connected"
ButtonConnect.SendToBack()
ButtonDisconnect.BringToFront()
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()
SerialPort.Close()
ButtonDisconnect.SendToBack()
ButtonConnect.BringToFront()
LabelStatus.Text = "Status : Disconnect"
End Sub
'====================================== Connection Panel End ================================================'
End Class
