Imports System
Imports System.IO
Public Class ComTester
Private _inpuData As String = String.Empty
'delegate for asynchronous calls
'Set the property text on the text box control
Private Delegate Sub SetTextCallBack(ByVal text As String)
Private Sub ComTester_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
If Not Me.SerialPort1 Is Nothing AndAlso Me.SerialPort1.IsOpen Then
Me.SerialPort1.Close()
Me.SerialPort1.Dispose()
End If
End Sub
Private Sub ComTester_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each sp As String In System.IO.Ports.SerialPort.GetPortNames
Me.ComboBoxPorts.Items.Add(sp)
Next sp
Me.SerialPort1 = Nothing
Me.PropertyGridPorts.SelectedObject = Me.SerialPort1
Me.TabControl1.SelectedTab = Me.TabPageSetup
End Sub
#Region "Tab Setup"
Private Sub ButtonConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConnect.Click
If Me.ComboBoxPorts.SelectedItem Is Nothing Then
MessageBox.Show("The port name can't be empty.", "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Me.ComboBoxPorts.Focus()
Exit Sub
End If
Me.SerialPort1 = New System.IO.Ports.SerialPort(Me.ComboBoxPorts.SelectedItem.ToString())
Try
If Me.SerialPort1.IsOpen Then Me.SerialPort1.Close()
Me.SerialPort1.Open()
Catch ex As Exception
MessageBox.Show("Error during the opening of the port " & Me.SerialPort1.PortName & "." & Environment.NewLine & ex.Message, "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
MessageBox.Show("Com port " & Me.SerialPort1.PortName & " successfully opened.", "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub ComboBoxPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxPorts.SelectedIndexChanged
If Not Me.ComboBoxPorts.SelectedItem Is Nothing Then _
Me.SerialPort1 = New System.IO.Ports.SerialPort(Me.ComboBoxPorts.SelectedItem.ToString())
Me.PropertyGridPorts.SelectedObject = Me.SerialPort1
End Sub
#End Region
#Region "Tab Sending"
Private Sub Send(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonSendText.Click
Dim pressedButton As Button = CType(sender, Button)
Dim value As Integer
If Not Me.TextBoxText.Text.Trim.Equals("+") AndAlso Not Me.TextBoxText.Text.Trim.Equals("-") Then
If Not Integer.TryParse(Me.TextBoxText.Text.Trim, value) Then
MessageBox.Show("Invalid input")
Exit Sub
End If
If value < 0 OrElse value > 400 Then
MessageBox.Show("Out of bounds input")
Exit Sub
End If
End If
Select Case pressedButton.Name
Case Me.ButtonSendText.Name
Try
Me.SerialPort1.WriteLine(Me.TextBoxText.Text.Trim & " ")
Catch ex As Exception
MessageBox.Show("Error while sending text to the port." & Environment.NewLine & ex.Message, "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Select
End Sub
#End Region
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me._inpuData = Me.SerialPort1.ReadExisting()
If Not Me._inpuData.Equals(String.Empty) Then Me.SetText(Me._inpuData)
End Sub
Private Sub SetText(ByVal text As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.TextBoxData.InvokeRequired Then
Dim d As SetTextCallBack = New SetTextCallBack(AddressOf SetText)
Me.Invoke(d, New Object() {text})
Else
Me.TextBoxData.Text &= text
End If
End Sub
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
Dim response As DialogResult = Me.SaveFileDialog1.ShowDialog()
Try
If response = Windows.Forms.DialogResult.OK Then
System.IO.File.WriteAllText(Me.SaveFileDialog1.FileName, Me.TextBoxData.Text)
MessageBox.Show("Data saved in " & Me.SaveFileDialog1.FileName, "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show("Incorrect file name.", "Serial communication", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
Private Sub cmdClearText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearText.Click
Me.TextBoxData.Clear()
End Sub
End Class