Giter Site home page Giter Site logo

usbserialforandroid's Introduction

UsbSerialForAndroid

Build Status

This is a driver library to allow your Xamarin Android app to communicate with many common USB serial hardware. It uses the Android USB Host API available on Android 3.1+.

No root access, ADK, or special kernel drivers are required; all drivers are implemented in c#. You get a raw serial port with Read(), Write(), and other basic functions for use with your own protocols. The appropriate driver is picked based on the device's Vendor ID and Product ID.

This is a Xamarin C# port of Mike Wakerly's Java usb-serial-for-android library. It followed that library very closely when it was ported. The main changes were to make the method names follow C# standard naming conventions. Some Java specific data types were replaced with .NET types and the reflection code is .NET specific. Code examples written for the Java version of the library should translate more or less faithfully to C#.

It also includes code derived from a portion of LusoVU's XamarinUsbSerial library. XamarinUsbSerial was a C# wrapper for the Java usb-serial-for-android. It used an older version of the usb-serial-for-android .jar file. Only the the C# code was used, the Java library is not referenced.

The default branch has been renamed from master to main. if you have a local clone, you can run the following commands to update the name of the default branch

git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a

Structure

This solution contains two projects.

  • UsbSerialForAndroid - A port of the Java library usb-serial-for-android
  • UsbSerialExampleApp - A Xamarin version of the example app that comes with usb-serial-for-android

Getting Started

1. Reference the library to your project

2. Copy the device_filter.axml from the example app to your Resources/xml folder. Make sure that the Build Action is set to AndroidResource

3. Add the following attribute to the main activity to enable the USB Host

[assembly: UsesFeature("android.hardware.usb.host")]

4. Add the following IntentFilter to the main activity to receive USB device attached notifications

[IntentFilter(new[] { UsbManager.ActionUsbDeviceAttached })]

5. Add the MetaData attribute to associate the device_filter with the USB attached event to only see the devices that we are looking for

[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]

6. Refer to MainActivity.cs in the example app to see how connect to a serial device and read data from it.

Working with unrecognized devices

The UsbSerialForAndroid has been compiled with the Vendor ID/Product ID pairs for many common serial devices. If you have a device that is not defined by the library, but will work with one of the drivers, you can manually add the VID/PID pair.

UsbSerialProber is a class to help you find and instantiate compatible UsbSerialDrivers from the tree of connected UsbDevices. Normally, you will use the default prober returned by UsbSerialProber.getDefaultProber(), which uses the built-in list of well-known VIDs and PIDs that are supported by our drivers.

To use your own set of rules, create and use a custom prober:

// Probe for our custom CDC devices, which use VID 0x1234
// and PIDS 0x0001 and 0x0002.
var table = UsbSerialProber.DefaultProbeTable;
table.AddProduct(0x1b4f, 0x0008, typeof(CdcAcmSerialDriver)); // IOIO OTG

table.AddProduct(0x09D8, 0x0420, typeof(CdcAcmSerialDriver)); // Elatec TWN4

var prober = new UsbSerialProber(table);
List<UsbSerialDriver> drivers = prober.FindAllDrivers(usbManager);
// ...

Of course, nothing requires you to use UsbSerialProber at all: you can instantiate driver classes directly if you know what you're doing; just supply a compatible UsbDevice.

Compatible Devices

Additional information

This is a port of the usb-serial-for-android library and code examples written for it can be adapted to C# without much effort.

For common problems, see the Troubleshooting wiki page for usb-serial-for-android library.

For other help and discussion, please join the usb-serial-for-android Google Group, usb-serial-for-android.

Pull Requests are welcome, but please include what hardware was used for testing. I do not have the hardware or the bandwidth to test the various chipsets supported by the library.

Author, License, and Copyright

This library is licensed under LGPL Version 2.1. Please see LICENSE.txt for the complete license.

Copyright 2017, Tyler Technologies. All Rights Reserved. Portions of this library are based on the usb-serial-for-android and XamarinUsbSerial libraries. Their rights remain intact.

usbserialforandroid's People

Contributors

anotherlab avatar ihornehrutsa avatar mattjeanes avatar ygoe avatar nwestfall avatar alexmedia avatar andrewhaighcell avatar wim-dekker avatar pravincar avatar tiorac avatar

Watchers

James Cloos avatar  avatar

Forkers

huafangyun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.