VB Serial Ports

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I was wondering if I could get feedback about this code. I am new to coding and am very nervous.
This is the beginning of a larger project, so I have imported some systems that I expect to need later on.
I need to monitor an external device and convert the output into various formats.
My nest hurdle is knowing how to interpret/format the data received from the port.
ThanksImports System
Imports System.IO.Ports
Imports System.IO.Stream
Imports System.ComponentModel
Imports System.Threading

Public Class Form1

Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

myPort = IO.Ports.SerialPort.GetPortNames()
Try
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
cmbPort2.Items.Add(myPort(i))

Next
cmbPort.Text = cmbPort.Items.Item(0)
cmbPort2.Text = cmbPort2.Items.Item(0)
btnOpen.Enabled = True
btnOpen2.Enabled = True

Catch ex As ArgumentOutOfRangeException
rtbFeedback.Text = "NO ports available!"

End Try

End Sub

Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click

Try
SerialPort1.PortName = cmbPort.Text
SerialPort1.Parity = Parity.None
SerialPort1.BaudRate = 9600
SerialPort1.StopBits = StopBits.One
SerialPort1.DataBits = 8

SerialPort1.Open()
btnOpen.Enabled = False
btnClose.Enabled = True

Catch ex As Exception
rtbFeedback.Text = "Port NOT available!"
End Try

End Sub

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click

SerialPort1.Close()
btnOpen.Enabled = True
btnClose.Enabled = False

End Sub

Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click

Try
Dim Data1 As String
Data1 = txtSend.Text & vbCr
SerialPort1.WriteLine(Data1)

If Data1 IsNot Nothing Then
txtSend.Text = ""
Else
rtbFeedback.Text = "No Data1"
End If

Catch ex As InvalidOperationException
rtbFeedback.Text = "Port CLOSED!"

End Try

End Sub

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

ReceivedText(SerialPort1.ReadExisting()) Automatically called every time a data is received at the serialPort
End Sub

Private Sub ReceivedText(ByVal [text] As String)
compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceive.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceive.Text &= [text]
End If

End Sub

Private Sub SerialPort1_Send(sender As Object, e As EventArgs) Handles SerialPort1.DataReceived

End Sub

Function ReceiveSerialData() As String
Receive strings from a serial port.
Dim SerialPort1 As IO.Ports.SerialPort = Nothing

Try
SerialPort1.ReadTimeout = 1000

Do
Dim Incoming As String = SerialPort1.ReadLine()

If Incoming IsNot Nothing Then
rtbReceive.Text &= Incoming & vbCrLf
Else
rtbReceive.Text = "NO Data"
End If
Loop

Catch ex As TimeoutException
rtbReceive.Text = "Error: Serial Port read timed out."
End Try

Return rtbReceive.Text
End Function

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

rtbReceive.Text = ""

End Sub
=============================
PORT 2.
=============================

Private Sub btnOpen2_Click(sender As Object, e As EventArgs) Handles btnOpen2.Click

Try
SerialPort2.PortName = cmbPort2.Text
SerialPort2.Parity = Parity.None
SerialPort2.BaudRate = 9600
SerialPort2.StopBits = StopBits.One
SerialPort2.DataBits = 8

SerialPort2.Open()
btnOpen2.Enabled = False
btnClose2.Enabled = True
Catch ex As Exception

rtbFeedback.Text = "Port NOT available!"
End Try

End Sub

Private Sub btnClose2_Click(sender As Object, e As EventArgs) Handles btnClose2.Click

SerialPort2.Close()
btnOpen2.Enabled = True
btnClose2.Enabled = False

End Sub

Private Sub btnSend2_Click(sender As Object, e As EventArgs) Handles btnSend2.Click

Try
Dim Data2 As String
Data2 = txtSend2.Text & vbCr
SerialPort2.WriteLine(Data2)

If Data2 IsNot Nothing Then
txtSend2.Text = ""
Else
rtbFeedback.Text = "No Data2"
End If

Catch ex As InvalidOperationException
rtbFeedback.Text = "Port CLOSED!"

End Try

End Sub

Private Sub btnClear2_Click(sender As Object, e As EventArgs) Handles btnClear2.Click

rtbReceive2.Text = ""

End Sub

Private Sub SerialPort2_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived

ReceivedText2(SerialPort2.ReadExisting()) Automatically called every time a data is received at the serialPort
End Sub

Private Sub ReceivedText2(ByVal [text] As String)
compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceive2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText2)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceive2.Text &= [text]
End If

End Sub

Function ReceiveSerialData2() As String
Receive strings from a serial port.

Dim SerialPort2 As IO.Ports.SerialPort = Nothing

Try
SerialPort2.ReadTimeout = 1000

Do
Dim Incoming As String = SerialPort2.ReadLine()

If Incoming IsNot Nothing Then
rtbReceive2.Text &= Incoming & vbCrLf
Else
rtbReceive2.Text = "NO Data"
End If

Loop

Catch ex As TimeoutException
rtbReceive2.Text = "Error: Serial Port read timed out."
End Try

Return rtbReceive2.Text
End Function

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click

SerialPort1.Close()
btnOpen.Enabled = True
btnClose.Enabled = False

SerialPort2.Close()
btnOpen2.Enabled = True
btnClose2.Enabled = False

txtSend.Text = ""
txtSend2.Text = ""
rtbReceive.Text = ""
rtbReceive2.Text = ""
rtbFeedback.Text = ""

End Sub

Private Sub btnClearFeed_Click(sender As Object, e As EventArgs) Handles btnClearFeed.Click

rtbFeedback.Text = ""

End Sub

End Class


If at first you dont succeed = true then try try again catch mistakes and never end

View the full article
 
Back
Top