Using Visual Basic Express 2008 with the FTDI virtual COM port

Microsoft Visual Basic Express 2008 offers an easy to use method to design PC based applications, it provides a multitude of buttons, timers, textboxes etc which are all available to be easily integrated into the target program. The aim of this tutorial was to show the absolute basics of what is required to talk to the COM port, from setting the port up to declaring an array buffer and then on to finally communicating with the target. More advanced error handling is not including for the purpose of keeping simplicity.

Visual studio express is provided free from Microsoft: Visual Express

Firstly comes the COM port handle definition, and a byte declared for indicating that the COM port has been successfully opened later in the code.

Public Class Form1
Dim USB_RS485 As System.IO.Ports.SerialPort
Dim port_open As Byte = 0

Next comes a subroutine that is executed only when the program starts, it adds text strings of all the available COM ports into a combo box named "Ports" and finishes by printing  "Select COM" in the combo box. This will only be seen at the start and whatever port was selected from the drop down list in the combo box will replace it thereafter.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For
Each sp As String In My.Computer.Ports.SerialPortNames
Ports.Items.Add(sp)
Next
Ports.Text = "Select COM"
End Sub

Opening a selected COM port from the combo box is the objective of the Ports_SelectedIndexChanged() function. It firstly looks to see if there is a COM port in use that was setup by itself by checking the variable port_open, if that is the case it will close the port before moving on. An attempt will then be made using a try, catch function to open the selected port specified in the combo box, a failed attempt will print "ERROR OPENING PORT" to a textbox and return. A success will print the message "COMx OPEN" and change port_open to indicate accordingly.

Private Sub Ports_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ports.SelectedIndexChanged
If (port_open) Then
USB_RS485.Close()
port_open = 0
End If
Try
USB_RS485 = My.Computer.Ports.OpenSerialPort(Ports.Text, 38400, 0, 8, 2) ' attempt to open a new COM port
Catch
TextBox1.Text = "ERROR OPENING PORT"
Return
End Try
USB_RS485.ReadTimeout = 500
TextBox1.Text = Ports.Text + " OPEN"
port_open = 1
End Sub

When the Go button is clicked this subroutine sends data to the serial port and waits for some in return. It starts by checking the port_open variable to see whether a COM port is open. Next it loads a byte array buffer with data, before using the Write() function to send the data to the COM port specified by the USB_RS485 handle. The function now looks for four bytes in return and waits until the COM port has these ready, once this condition is met the Read() function is used to move the data into the byte array buffer.  

Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click
Dim SerBuf(20) As Byte
If
(port_open = 0) Then
TextBox1.Text = "NO PORT OPEN"
Return
End If  

'USB_RS485.BreakState = True            'use the following two lines if a break is required at start of comms frame
'USB_RS485.BreakState = False

SerBuf(0) = 0
SerBuf(1) = 1
SerBuf(2) = 2
SerBuf(3) = 3
SerBuf(4) = 4
SerBuf(5) = 5
USB_RS485.Write(SerBuf, 0, 6)                  'Write(buffer, start, length)
Do While USB_RS485.BytesToRead < 4     'wait for four bytes to be recieved
Loop
USB_RS485.Read(SerBuf, 0, 4)                   'Read(buffer, start, length)
End Sub
End
Class