Skip to content

USBUART

USBUART is a cross-platform libusb-based library for reading/wring data via USB-UART adapters with Android support.
It implements a relay from endpoints of a USB-UART converter to a pair of I/O resources, either appointed by given file descriptors, or created inside the library with pipe(2). User application may then use standard I/O operations for reading and writing data.

USBUART Library provides API for three languages – C++, C and Java.

Usage with C++

// Instantiate a context
context ctx;

// Attach USB via a pipe channel
channel chnl;
ctx.pipe(device_id{0x067b,0x0609},chnl,_115200_8N1n);

//Run loop in one thread
while(ctx.loop(10) >= -error_t::no_channel);

//Read/write data in other thread(s)
char buff[256];
read(chnl.fd_read, buff, sizeof(buff));

//or use non-blocking I/O in the loop body

Usage on Android

// Instantiate a context
ctx = new UsbUartContext();

// Start a thread running event loop 
new Thread(ctx).start();

// Obtain permission to the USB device
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
usbManager.requestPermission(device, PendingIntent.getBroadcast(...));

// Open device
UsbDeviceConnection connection = usbManager.openDevice(device);

// Create pipe channel
Channel channel = ctx.pipe(connection, 0, EIA_TIA_232_Info._115200_8N1n());

// Open streams
InputStream input = channel.getInputStream();   
OutputStream output = channel.getOutputStream();    

// Perfrom I/O operations with the streams

Library source available on github: https://github.com/hutorny/usbuart

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

*