Imports System
Imports System.IO.Ports
Imports System.Threading.Thread
' Imports the System, System.IO.Ports and System.Threading.Thread namespaces so that e.g.
' System.IO.Ports.SerialPort may just be written as SerialPort, and System.EventArgs may just be
' written as EventArgs.
Public Class Form1
Public Delegate Sub StringSubPointer(ByVal Buffer As String)
Dim SpaceCount As Byte = 0
Dim sDataSeparator As String = " "
Dim WithEvents SerialPort As New SerialPort ' Make a new System.IO.Ports.SerialPort instance, which is able to send events
Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
' Note this subroutine is executed on the serial port thread - not the UI thread.
Dim RXByte, Nibble As Byte
Dim RXArray(2047) As Char
Dim I As Integer = 0
Dim BreakDetect As Boolean
'
Do
BreakDetect = SerialPort.BreakState ' Read COMPort.BreakState as early as possible.
' Note. Break detection above approximately 300 bit/s is not reliable because BreakState may go low again
' before this routine is running (not latched).
RXByte = SerialPort.ReadByte
If BreakDetect And (RXByte = 0) Then ' If RXByte <> 0, the Break is detected too early
RXArray(I) = "B"
I = I + 1
RXArray(I) = "r"
I = I + 1
RXArray(I) = "e"
I = I + 1
RXArray(I) = "a"
I = I + 1
RXArray(I) = "k"
I = I + 1
RXArray(I) = Chr(13) ' CR
I = I + 1
RXArray(I) = Chr(10) ' LF
I = I + 1
SpaceCount = 0
Else
Nibble = (RXByte >> 4) + 48 ' Convert received byte to Hex
If Nibble > 57 Then
Nibble = Nibble + 7
End If
RXArray(I) = Chr(Nibble)
I = I + 1
Nibble = (RXByte And 15) + 48
If Nibble > 57 Then
Nibble = Nibble + 7
End If
RXArray(I) = Chr(Nibble)
I = I + 1
RXArray(I) = sDataSeparator
I = I + 1
SpaceCount = (SpaceCount + 1) And 31 ' Insert spaces and CRLF for better readability
If SpaceCount = 0 Then ' Insert CRLF after 32 numbers
RXArray(I) = Chr(13) ' CR
I = I + 1
RXArray(I) = Chr(10) ' LF
I = I + 1
Else
If (SpaceCount And 3) = 0 Then ' Insert two extra spaces for each 4 numbers
RXArray(I) = " "
I = I + 1
RXArray(I) = " "
I = I + 1
End If
End If
End If
Loop Until (SerialPort.BytesToRead = 0)
Dim RxString As New String(RXArray, 0, I) ' Convert the first part of the Char Array to a String
' Put the display routine and the RxString on the message queue and return immediately.
txtReceivedLog.BeginInvoke(New StringSubPointer(AddressOf Display), RxString)
End Sub
' Text display routine, which appends the received string to any text in the Received TextBox form.
Private Sub Display(ByVal Buffer As String)
txtReceivedLog.AppendText(Buffer)
End Sub
Private Sub cboPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPort.SelectedIndexChanged
OpenSerialPort()
End Sub
'Handles the opening (or changing) of the port
Private Sub OpenSerialPort()
'Port has been selected or changed via the Combo List box of port names
If SerialPort.IsOpen Then
SerialPort.RtsEnable = False
SerialPort.DtrEnable = False
SerialPort.Close()
Application.DoEvents() ' Empty the message queue before the UI thread goes to sleep.
Sleep(200) ' Wait 0.2 second for port to close as this do not happen immediately.
End If
SerialPort.PortName = cboPort.Text ' Default: 9600 Baud, 8 data bits, no parity, 1 stop bit
' Set the buffer size to 2, which is minimum. The reason for this is that the buffer is only 8 bit wide,
' but the receiver FIFO in the UART is 11 bit wide as it also contains information about any Break or
' error conditions. Because of this difference, a Break condition may be detected too early.
' To minimize the problem, the buffer size is reduced as much as possible.
SerialPort.ReadBufferSize = 2
SerialPort.WriteTimeout = 5000 ' Max time to wait for CTS = 5 sec.
Try
SerialPort.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
If SerialPort Is Nothing Then
cboBitRate.Text = ""
cboBits.Text = ""
cboParity.Text = ""
cboHandshake.Text = ""
Else
cboBitRate.Text = SerialPort.BaudRate.ToString
cboBits.Text = SerialPort.DataBits.ToString
cboParity.Text = SerialPort.Parity.ToString
cboHandshake.Text = SerialPort.Handshake.ToString
SerialPort.RtsEnable = True
SerialPort.DtrEnable = True
End If
End Sub
Private Sub cboBitRate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBitRate.SelectedIndexChanged
SerialPort.BaudRate = CInt(cboBitRate.Text)
End Sub
Private Sub cboBits_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBits.SelectedIndexChanged
SerialPort.DataBits = CInt(cboBits.Text)
End Sub
Private Sub cboParity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboParity.SelectedIndexChanged
SerialPort.Parity = CType([Enum].Parse(GetType(Parity), cboParity.Text), Parity)
End Sub
Private Sub cboHandshake_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboHandshake.SelectedIndexChanged
SerialPort.Handshake = CType([Enum].Parse(GetType(Handshake), cboHandshake.Text), Handshake)
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If SerialPort.IsOpen Then SerialPort.Close() ' Close COM port when the form is terminated with [X]
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each COMString As String In My.Computer.Ports.SerialPortNames ' Load all available COM ports.
cboPort.Items.Add(COMString)
Next
cboPort.Sorted = True
cboPort.SelectedIndex = 0 'Ensure default serial port is selected
'cboBitRate.Items.Add("110")
cboBitRate.Items.Add("300")
cboBitRate.Items.Add("600")
cboBitRate.Items.Add("1200")
cboBitRate.Items.Add("1800")
cboBitRate.Items.Add("2400")
cboBitRate.Items.Add("4800")
cboBitRate.Items.Add("7200")
cboBitRate.Items.Add("9600")
cboBitRate.Items.Add("14400")
cboBitRate.Items.Add("19200") ' Min. FIFO size 3 Bytes (8030 or 8530)
'BaudRateBox.Items.Add("38400")
'BaudRateBox.Items.Add("57600") ' Min. FIFO size 8 bytes
'BaudRateBox.Items.Add("115200") ' Min. FIFO size 16 bytes (16C550)
'BaudRateBox.Items.Add("230400") ' Min. FIFO size 32 bytes (16C650)
'BaudRateBox.Items.Add("460800") ' Min. FIFO size 64 bytes (16C750)
'BaudRateBox.Items.Add("921600") ' Min. FIFO size 128 bytes (16C850 or 16C950)
cboBitRate.SelectedIndex = 5 'Set default bit rate = 4800 for PICAXE
cboBits.Items.Add("5")
cboBits.Items.Add("6")
cboBits.Items.Add("7")
cboBits.Items.Add("8")
cboParity.Items.Add("None")
cboParity.Items.Add("Odd")
cboParity.Items.Add("Even")
cboParity.Items.Add("Mark") ' Leaves the parity bit set to 1
cboParity.Items.Add("Space") ' Leaves the parity bit set to 0
cboHandshake.Items.Add("None")
cboHandshake.Items.Add("RequestToSend")
cboHandshake.Items.Add("RequestToSendXOnXOff")
cboHandshake.Items.Add("XOnXOff")
End Sub
End Class