Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
software:libusbjava:first_steps:start [2015-03-31 11:45] – kalberer | software:libusbjava:first_steps:start [2022-06-17 17:14] (aktuell) – gelöscht Urs Graf | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== First Steps with LibusbJava ====== | ||
- | The following short example will show you how to use LibusbJava in your Java-Project. | ||
- | The following code describes the required steps to do a bulk transfer to your device. | ||
- | - Create a new Context (initialises the libusb, returns a context) | ||
- | - Search your device, for example via Vendor- and Product-ID | ||
- | - (optionally) get information out of the USB Descriptors | ||
- | - Open your device | ||
- | - Claim the desired interface | ||
- | - write some data with a bulk transfer to your device | ||
- | - Release interface | ||
- | - Close the device | ||
- | < | ||
- | Device usbDev = null; | ||
- | try { | ||
- | useCtx = new Context(); | ||
- | } catch (LibusbException e) { | ||
- | System.out.println(" | ||
- | e.printStackTrace(); | ||
- | } | ||
- | |||
- | System.out.println(" | ||
- | try { | ||
- | usbDev = Device.search(useCtx, | ||
- | |||
- | } catch (LibusbException e) { | ||
- | System.out.println(" | ||
- | e.printStackTrace(); | ||
- | } | ||
- | |||
- | if(usbDev == null) return; | ||
- | |||
- | try { | ||
- | usbDev.open(); | ||
- | usbDev.claimInterface(0); | ||
- | byte[] data = {(byte)0x40, | ||
- | int res = usbDev.bulkTransfer(2, | ||
- | if(res == data.length){ | ||
- | System.out.println(" | ||
- | } | ||
- | else{ | ||
- | System.out.println(" | ||
- | } | ||
- | usbDev.reset(); | ||
- | res = 0; | ||
- | res = usbDev.bulkTransfer(2, | ||
- | if(res == data.length){ | ||
- | System.out.println(" | ||
- | } | ||
- | else{ | ||
- | System.out.println(" | ||
- | } | ||
- | usbDev.releaseInterface(0); | ||
- | usbDev.close(); | ||
- | System.out.println(" | ||
- | } catch (LibusbException e) { | ||
- | System.out.println(" | ||
- | e.printStackTrace(); | ||
- | } | ||
- | </ |