Reading the data from a Universal Serial Bus (USB) joystick or thumb drive is possible with Microsoft Visual Basic (VB). Since USB devices are self-identifying and plug-and-play compliant, a computer system is able to easily recognise them. This makes writing a small piece of VB code to send a packet and retrieve data on the USB device easy. Sending a packet to the port is necessary, as it will detect the state of the port and send back any data found on the USB device. If there is no USB device plugged into the port, then an error message is displayed.
- Skill level:
Other People Are Reading
Open a new VB file.
Set the SendPacket function. Write this code at the top of your VB file:
Function SendPacket(ByRef TxPacket As PacketStructure) As PacketStructure
Dim lpResult As Long
Dim RxPacket As PacketStructure
Set the recipient and device model by inserting this code on the fifth line:
TxPacket.Recipient = 8
TxPacket.DeviceModel = 18
Call the SendPacket function in the VB code. Write the below code starting on the ninth line:
If 0 = DeviceIoControl(hDevice, CTL_CODE_SEND_PACKET, TxPacket,
8+TxPacket.Length, RxPacket, 8, lpResult, 0) Then
MsgBox "SendPacket function failed. Unable to retrieve USB data. " Exit Function
Call back the data retrieved with the SendPacket function. Place this code at the bottom of your VB script:
SendPacket = RxPacket
Insert your USB joystick into the computer's USB port.
Compile and execute your VB script. Data is retrieved into the program's memory, which you can use for other programming solutions and needs.
Tips and warnings
- The TxPacket.Recipient is always set to 8, and TxPacket.DeviceMode is set to 18.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for