USBmicro

Your source for USB electrical device interface products.

  • Home
  • Services
    • Design
    • Contact USBmicro
  • Where to buy…
You are here: Home / Documentation / Programming Overview / General VB Example

General VB Example

This example uses VB to initialize the U4x1. The very minimum application would be to open the USB device and transmit a single command. This example shows the code that it takes to open the U4x1 device and initialize the ports. The VBLIB files need to be included as part of this project.

The communication with the U4xx is done by transferring a group of eight bytes via the WriteUSBdevice call of the U4x1 VB library. Eight bytes are returned from the device and read via the ReadUSBdevice U4x1 VB call. These bytes are located in two arrays that are defined below.

Option Explicit
Dim OutBuffer(10) As Byte
Dim InBuffer(10) As Byte

When the form for this project is loaded, a call to OpenUSBdevice with the appropriate parameters returns true if the device has been detected.

The OpenUSBdevice parameters are used to find the device. A single parameter (in this example the string “U401”) will open the first device located that matches that string. Multiple parameters allow devices to be opened via manufacturer name, VID, etc.

When a single parameter is used, the other parameters should be either 0, or a null string, as in the example.

Multiple parameters are used when device selection refinement is necessary, such as when multiple U4x1 devices are used on a single machine. In that case, the serial number string should be used to refine the selection.

‘ Form load
‘ When the form is loaded at startup, find the hardware.
‘ Indicate status in “DeviceStatus” box.

Private Sub Form_Load()

    If OpenUSBdevice(“U401”, “”, 0, 0, 0, “”) Then
        DeviceStatus.Caption = “USB Device Found”
    Else
        DeviceStatus.Caption = “USB Device Not Found”
    End If

End Sub

Calling the function WriteReadUSB moves the eight command bytes of the array “OutBuffer” to the device and fills “InBuffer” with the 8 bytes sent from the U4xx.

‘ USB Transfer

Public Sub WriteReadUSB()

    Call WriteUSBdevice(AddressFor(OutBuffer(0)), 8)
    DoEvents
    Call ReadUSBdevice(AddressFor(InBuffer(0)), 8)

End Sub

The single button on the form of this application example transmits the InitPorts Command string of 00-00-00-00-00-00-00-00 to the U4xx.

‘ Button: Cmd

‘ Send a command to the device

Private Sub Cmd_Click()

    OutBuffer(0) = &H0
    OutBuffer(1) = &H0
    OutBuffer(2) = &H0
    OutBuffer(3) = &H0
    OutBuffer(4) = &H0
    OutBuffer(5) = &H0
    OutBuffer(6) = &H0
    OutBuffer(7) = &H0

    Call WriteReadUSB

End Sub

Documentation

Open all | Close all