Imports System.IO.Ports
Imports System.IO
Public Class Form1
Private iCmdSent As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'Event handler for the Connect button
If SerialPort1.IsOpen() Then
SerialPort1.Close()
End If
Try
With SerialPort1
.PortName = ComboBox1.SelectedItem
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.Two
.ReadBufferSize = 64
.ReadTimeout = 500
.ReceivedBytesThreshold = 13
.Handshake = IO.Ports.Handshake.None
.RtsEnable = True
.DtrEnable = True
End With
SerialPort1.Open()
Label1.Text = ComboBox1.Text & " Connected."
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
'command for reading mark frequency
SendRequestCommand(Chr(76) & Chr(48) & Chr(13))
iCmdSent = (Chr(76) & Chr(48) & Chr(13))
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
'command for reading step frequency
SendRequestCommand(Chr(76) & Chr(49) & Chr(13))
iCmdSent = (Chr(76) & Chr(49) & Chr(13))
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
'command for reading level data
SendRequestCommand(Chr(82) & Chr(49) & Chr(13))
iCmdSent = (Chr(82) & Chr(51) & Chr(13))
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub OnDataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
Try
TextBox1.Invoke(New myDelegate(AddressOf UpdateTextBox), New Object() {})
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Delegate Sub myDelegate()
Public Sub UpdateTextBox()
Dim strInData As String = ""
While SerialPort1.BytesToRead > 0
strInData &= ChrW(SerialPort1.ReadByte())
End While
'display output
Select Case iCmdSent
Case (Chr(76) & Chr(48) & Chr(13))
'display mark frequency
TextBox1.Text = strInData.Substring(3, 9)
TextBox1.Refresh()
System.Threading.Thread.Sleep(1000)
SendRequestCommand(Chr(76) & Chr(48) & Chr(13))
Case (Chr(76) & Chr(49) & Chr(13))
'display step frequency
TextBox2.Text = strInData.Substring(3, 6)
TextBox2.Refresh()
System.Threading.Thread.Sleep(1000)
SendRequestCommand(Chr(76) & Chr(49) & Chr(13))
Case (Chr(82) & Chr(51) & Chr(13))
'display level data
TextBox3.Text = strInData
TextBox1.Refresh()
System.Threading.Thread.Sleep(1000)
SendRequestCommand(Chr(82) & Chr(51) & Chr(13))
End Select
End Sub
Private Sub SendRequestCommand(ByVal cmd As String)
If SerialPort1.IsOpen() Then
SerialPort1.DiscardInBuffer()
SerialPort1.Write(cmd)
Else
MsgBox("COM port is closed!")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To _
My.Computer.Ports.SerialPortNames.Count - 1
ComboBox1.Items.Add( _
My.Computer.Ports.SerialPortNames(i))
Next
End Sub
End Class