使用带有 Visual Basic 6 的 FTDI D2XX 驱动程序读取和写入串行端口

Reading and writing to serial port using FTDI D2XX drivers with Visual Basic 6

我正在尝试使用 VB6 建立连接并从虚拟 COM 端口读取数据,接着是我在此处的查询:。我正在使用 FTDI 驱动程序通过 USB VCP 与设备通信。

我正在使用 Visual Basic 6 上的 FTD2XX 库来显示设备的名称和序列号(这已经可以工作)、设置停止位数、设置波特率和数据位数。我现在想从串口读写,我有一些代码和问题。我的代码如下:

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

我的问题如下:

1) 我已经使用 FD2XX 库设置了波特率、停止位和数据位数。完成后,是否可以直接连接到串行端口并使用 FTDI 库中没有的函数发送或接收数据?我问这个是因为我不确定 FTD2XX 驱动程序是否与 VCP 分开,并且 FTDI 不提供有关使用 USB VCP 进行串行通信的文档。

2) 是否有任何有据可查的函数 libraries/code 建议可以让我阅读它?如果这需要某种形式的转换,请问是否也可以建议一个记录良好的函数库?

3) 是否有任何文档完善的函数库可将无符号整数写入我通过 USB VCP 与之通信的设备?

我不太熟悉 VB 但我经常通过 C 和 python 使用 FT 设备。所以这就是我所知道的:

避免误解的一般评论:VCP 代表虚拟 COM 端口。因此,这实际上使系统能够在不需要使用特定库(如 D2XX)的情况下对 FT 设备进行寻址。大多数语言都提供一些 "native" 访问 com 端口。因此,对于常规的 com 端口操作,根本不需要处理 D2XX。据我所知,它主要用于替代操作模式和访问 MPSSE。

1) 如果您通过 D2XX 打开一个端口,它将无法用于其他访问。如果您释放它并通过其他方式打开它(例如 MSComm 或 IO.Ports.SerialPort,如果是 auf .net),设置将被覆盖(或者至少应该自动覆盖)。

2) 恐怕只有 FT 的示例项目才是你最好的选择。但也许其他人可以指出更好的方法。

3) 通常(C 和 python)的本机访问允许您写入和读取纯字节字符串。所以你唯一需要做的就是 "transform" 正确的类型。 ctype / CByte / CInt 似乎是你的提示。

对了,题中出现的FT_Write_StringFT_Write_Bytes函数是从哪里来的?

在FTDI代码示例中是FT_Write,FT_WriteByte, FT_W32_WriteFile

Visual Basic Examples

D2XX Module

many of the Visual Basic examples posted on this page use a module to interface to the D2XX DLL. To download the unit (D2XX_Module.bas) for Visual Basic 6, click here.
Please note that the code examples below may already contain a module handling the D2XX DLL interface. There may be differences between the current module file and the ones distributed with the examples.

D2XX_Module.bas

Public Declare Function FT_Write Lib "FTD2XX.DLL" (ByVal lngHandle As Long, ByVal lpszBuffer As String, ByVal lngBufferSize As Long, ByRef lngBytesWritten As Long) As Long
Public Declare Function FT_WriteByte Lib "FTD2XX.DLL" Alias "FT_Write" (ByVal lngHandle As Long, ByRef lpszBuffer As Any, ByVal lngBufferSize As Long, ByRef lngBytesWritten As Long) As Long

Public Declare Function FT_W32_WriteFile Lib "FTD2XX.DLL" (ByVal lngHandle As Long, ByVal lpszBuffer As String, ByVal lngBufferSize As Long, ByRef lngBytesWritten As Long, ByRef lpftOverlapped As lpOverlapped) As Long

注:不过,FT_W32_WriteFileByVal lpszBuffer As String参数好像是ByRef lpszBuffer As Any的错误。

Example 6

Our thanks go to Bob Freeth for providing this VB6 example of using the FT2232C MPSSE for SPI communication with a MAX187 ADC.
Source code and executable are available for free download. This code is provided "as-is" for illustration purposes only and as such neither FTDI or Bob Freeth provide technical support for this VB6 code.


Visual Basic 的字符串变量是 Unicode,因此它们不适合处理二进制数据。

与其替换String变量,不如将数据显式存储在字节数组变量中,并使用FT_WriteByte函数写入。

基于以上内容,将如下。

Dim SendData(nnn) As Byte ' nnn is value of send data size - 1  
SendData(0) = 121
SendData(1) = xxx
SendData(2) = yyy
.
.
.

FT_Status = FT_WriteByte(FT_Handle, SendData(0), Len(SendData), BytesWritten)