Get started with HIDAPI classes in Xojo
If you have an USB device with HID protocol, you can easily use it from your Xojo application. Please check out the HIDAPIDeviceMBS class for macOS, Windows and Linux. With the recent 24.1 plugins, we upgraded the classes to work even better.
You may start with enumerating of available devices. This should find all the HID devices connected to your computer like various keyboards, mice, joysticks or whatever you connect. For Linux, you may need to use chmod with sudo to grant permissions to all applications to connect to the devices. On macOS and Windows, you may just freely connect to your device as long as no standard driver grabbed it.
Let's enumerate the devices and look for ours:
The HIDAPIDeviceInfoMBS class provides all the details on the device, so you know which device you have and you can show manufacturer name, the product string and the serial number.
Please note, that on Linux we may not be able to get the strings if we don't have sufficient permissions to access it. You may see a device, but not get details or connect. Make sure you grant permissions to your app for access or run the app as root. e.g. a rules file in /etc/udev/rules.d folder can define, that a certain device would get permissions for everyone to read and write:
SUBSYSTEMS=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="2345", GROUP="users", MODE="0666"
Once you know which device to open, you can use the path to open it. Since you may have multiple devices, the path should make it unique on which of the devices you want to connect:
Alternatively you can of course skip all the above and try to connect to the first device matching the vendor and product IDs.
Once you got the connection, you can query various details. One of the thing you need to know is the package length for read and writing. This should be in the documentation of the device vendor or you may check the FeatureReportLength property for the report length.
When you like to send a data package to the device, please allocate a matching MemoryBlock including a byte in the front for the report ID. Then you put in the data to send in the other bytes and call the Write method.
Data arriving is collected in a buffer and you can regularly read it in a thread or timer to process data as it comes in.
When done, you can call Close method or let the last reference go.
Enjoy the class and see if you can find an use for it. Especially if you code something for the Raspberry Pi, you can use the HIDAPIDeviceMBS class to connect to a device.