【发布时间】:2019-10-03 17:20:43
【问题描述】:
我正在尝试使用 VB6 建立连接并从虚拟 COM 端口读取数据,继我的查询之后:Baud rate limits in software and serial communication with an external device。我正在使用 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) 是否有任何有据可查的函数库/代码建议可以让我从中阅读?如果这需要某种形式的转换,是否也可以建议一个有据可查的函数库?
3) 是否有任何有据可查的函数库用于将无符号整数写入通过 USB VCP 与之通信的设备?
【问题讨论】:
-
什么是“FTDI 库之外的函数”?我认为包括读/写功能。
-
@kunif 自从在这里发布以来我一直在尝试 FT_Write_Bytes 和 FT_Write_Strings 函数,但到目前为止我还没有成功。该设备需要向其发送两次二进制数字才能工作。目前我已通过代码修改如下以将数据作为字符串发送: Dim TempStringData As String = 121 然后我使用此命令两次将字符串写入设备:'将字符串数据写入设备 FT_Status = FT_Write_String(FT_Handle , TempStringData, Len(TempStringData), BytesWritten) If FT_Status FT_OK Then Exit Sub End If
-
@kunif 为了将字节写入设备而不是字符串,我在脚本顶部声明变量,如 DimBytestoDevice 为 Byte = 121,然后使用此命令两次发送命令:'写入字符串数据到设备 FT_Status = FT_Write_String(FT_Handle, DimBytestoDevice, Len(DimBytestoDevice ), BytesWritten) If FT_Status FT_OK Then Exit Sub End两个命令。
标签: vb6 serial-port ftdi