📄 form1.vb
字号:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button2 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(328, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(128, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Click here to run"
'
'TextBox1
'
Me.TextBox1.BackColor = System.Drawing.SystemColors.InactiveCaptionText
Me.TextBox1.Enabled = False
Me.TextBox1.Location = New System.Drawing.Point(152, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(144, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(320, 352)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(128, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Exit"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(112, 16)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Number of Devices"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'TextBox2
'
Me.TextBox2.BackColor = System.Drawing.SystemColors.InactiveCaptionText
Me.TextBox2.Enabled = False
Me.TextBox2.Location = New System.Drawing.Point(152, 80)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(144, 20)
Me.TextBox2.TabIndex = 4
Me.TextBox2.Text = ""
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(136, 23)
Me.Label2.TabIndex = 5
Me.Label2.Text = "Serial Number of Device 0"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 120)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(128, 23)
Me.Label3.TabIndex = 6
Me.Label3.Text = "Description of Device 0"
'
'TextBox3
'
Me.TextBox3.BackColor = System.Drawing.SystemColors.InactiveCaptionText
Me.TextBox3.Enabled = False
Me.TextBox3.Location = New System.Drawing.Point(152, 112)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(144, 20)
Me.TextBox3.TabIndex = 7
Me.TextBox3.Text = ""
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(152, 200)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(144, 20)
Me.TextBox4.TabIndex = 8
Me.TextBox4.Text = ""
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(16, 200)
Me.Label4.Name = "Label4"
Me.Label4.TabIndex = 9
Me.Label4.Text = "String to Write"
'
'TextBox5
'
Me.TextBox5.BackColor = System.Drawing.SystemColors.InactiveCaptionText
Me.TextBox5.Enabled = False
Me.TextBox5.Location = New System.Drawing.Point(152, 280)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(144, 20)
Me.TextBox5.TabIndex = 10
Me.TextBox5.Text = ""
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(16, 280)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(80, 16)
Me.Label5.TabIndex = 11
Me.Label5.Text = "String Read"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(472, 405)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DeviceCount As Integer
Dim DeviceIndex As Integer
Dim TempDevString As String
Dim BytesWritten As Integer
Dim TempStringData As String
Dim BytesRead As Integer
' Get the number of device attached
FT_Status = FT_GetNumberOfDevices(DeviceCount, vbNullChar, FT_LIST_NUMBER_ONLY)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Display device count on form
TextBox1.Text = DeviceCount.ToString
' 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
TextBox2.Text = FT_Serial_Number
' Get description of device with index 0
' Allocate space for string variable
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
TextBox3.Text = FT_Description
'Open device by serial number
FT_Status = FT_OpenByDescription(FT_Description, 2, FT_Handle)
If FT_Status <> FT_OK Then
MsgBox("Failed to open device.", , )
Exit Sub
End If
' Reset device
FT_Status = FT_ResetDevice(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Purge buffers
FT_Status = FT_Purge(FT_Handle, FT_PURGE_RX Or FT_PURGE_TX)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Set Baud Rate
FT_Status = FT_SetBaudRate(FT_Handle, 115200)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Set parameters
FT_Status = FT_SetDataCharacteristics(FT_Handle, FT_DATA_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Set Flow Control
FT_Status = FT_SetFlowControl(FT_Handle, FT_FLOW_RTS_CTS, 0, 0)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Set RTS
FT_Status = FT_SetRts(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Set DTR
FT_Status = FT_SetDtr(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Write string data to device
FT_Status = FT_Write_String(FT_Handle, TextBox4.Text, Len(TextBox4.Text), BytesWritten)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Wait
Sleep(100)
' Get number of bytes waiting to be read
FT_Status = FT_GetQueueStatus(FT_Handle, FT_RxQ_Bytes)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Read number of bytes waiting
' Allocate string to recieve data
TempStringData = Space(FT_RxQ_Bytes + 1)
FT_Status = FT_Read_String(FT_Handle, TempStringData, FT_RxQ_Bytes, BytesRead)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Display string on form
TextBox5.Text = Trim(TempStringData)
' Close device
FT_Status = FT_Close(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox("Connect an FTDI device with D2XX drivers." & Chr(13) & Chr(10) & "Connect a loop-back connector to write and read data through the device.", , )
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -