I am trying to establish a connection and read data from a virtual COM port using VB6, following on from my query here: Baud rate limits in software and serial communication with an external device . I am using an FTDI driver to communicate with an device via a USB VCP.
I am using the FTD2XX library on Visual Basic 6 to display the name and serial number of a device (this already works), set the number of stop bits, set baud rates and the number of data bits. I would like now like to read and write from the serial port and I have some code and questions. My code is below:
Public Class FTDI1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DeviceIndex As Integer
Dim TempDevString As String
Dim Read_Result As Integer
Dim Read_Count As Integer
' Get serial number of device with index 0
' Allocate space for string variable
TempDevString = Space(16)
FT_Status = FT_GetDeviceString(DeviceIndex, TempDevString, FT_LIST_BY_INDEX Or FT_OPEN_BY_SERIAL_NUMBER)
If FT_Status <> FT_OK Then
Exit Sub
End If
FT_Serial_Number = Microsoft.VisualBasic.Left(TempDevString, InStr(1, TempDevString, vbNullChar) - 1)
' Display serial number on form
TextBox1.Text = FT_Serial_Number
' Get the model of the connected device
TempDevString = Space(64)
FT_Status = FT_GetDeviceString(DeviceIndex, TempDevString, FT_LIST_BY_INDEX Or FT_OPEN_BY_DESCRIPTION)
If FT_Status <> FT_OK Then
Exit Sub
End If
FT_Description = Microsoft.VisualBasic.Left(TempDevString, InStr(1, TempDevString, vbNullChar) - 1)
' Display serial number on form
TextBox2.Text = FT_Description
' Set baud rate of the connected device
' Set Baud Rate
FT_Status = FT_SetBaudRate(FT_Handle, 1000000)
If FT_Status <> FT_OK Then
Debug.Print("Baud rate set")
Exit Sub
End If
' Set the number of stop bits of the recorded device
' Set parameters
FT_Status = FT_SetDataCharacteristics(FT_Handle, FT_DATA_BITS_8, FT_STOP_BITS_2, FT_PARITY_NONE)
If FT_Status <> FT_OK Then
Debug.Print("Stop bits, parity and data bits set")
Exit Sub
End If
' Read bytes (not strings)
FT_Status = FT_Read_Bytes(FT_Handle, FT_In_Buffer(16), Read_Count, Read_Result)
If FT_Status <> FT_OK Then
Debug.Print(Read_Result)
Exit Sub
End If
' Display read bytes on form
TextBox3.Text = Read_Result
' Close device
FT_Status = FT_Close(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
End Sub
End Class
My questions are as follows:
1) I have set the baud rate, stop bits and number of data bits using the FD2XX library. Once this has been done, is it possible to connect to the serial port directly and send or receive data using functions that are not within the FTDI library? I ask this because I am not sure if the FTD2XX drivers are separate from the VCP and FTDI do not provide documentation on serial communication using a USB VCP.
2) Are there any well documented function libraries/code suggestions that would enable me to read from it? If this requires some form of conversion, please can a well documented function library for this be suggested too?
3) Are there any well documented function libraries for writing unsigned integers to the device I am communicating with via the USB VCP?