A cross-platform library for reading/wring data via USB-UART adapters
USBUART
is a LIBUSB based library that implements a relay from USB-UART
converter’s endpoints 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.
See C/C++ API description in usbuart.h and Android API in
// 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
// 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
-
Get USBUART library sources
git clone https://github.com/hutorny/usbuart.git
-
Get libusb sources
cd usbuart git clone https://github.com/libusb/libusb.git
-
Change directory to libusb
cd libusb
-
Configure and build libusb
./autogen.sh --disable-udev make
-
Change directory to usbuart
cd ..
-
Make USBUART
make
-
Get USBUART library sources
git clone https://github.com/hutorny/usbuart.git
-
Get Android-tuned libusb fork
cd usbuart git clone https://github.com/hutorny/libusb.git
-
Download the latest NDK from: http://developer.android.com/tools/sdk/ndk/index.html
-
Extract the NDK.
-
Open a shell and make sure there exist an NDK global variable set to the directory where you extracted the NDK.
-
Change directory to usbuart/libusb
-
Configure libusb
./android/autogen.sh --enable-system_log
-
Change directory to usbuart
cd ..
-
Build libary and modules
ant debug