|
1 | 1 | ## Cordovarduino |
2 | 2 |
|
3 | | - This is my own branch of Cordovarduino. This one actually works... for me at least. |
| 3 | +Cordovarduino is a Cordova/Phonegap plugin that enable you to use serial communication from an Android device to a serial over USB capable one. |
4 | 4 |
|
5 | | - Differences with the official branch: |
| 5 | +It's a **work in progress** : Android to Arduino works, Arduino to Android now works. |
6 | 6 |
|
7 | | - - Implemented reading from the serial port |
8 | | - - Bugfixed usb serial lib that works with FTDI devices |
9 | 7 |
|
10 | | -Changes by Edouard Lafargue ([email protected]) |
| 8 | +## Change log |
11 | 9 |
|
| 10 | +2014.03: Ed. Lafargue |
| 11 | + Implemented read(). The success callback returns a Javascript ArrayBuffer which is the best way to handle binary data |
| 12 | + in Javascript. It is straightforward to convert this to a string if required - a utility function could be implemented in this plugin. |
| 13 | + |
| 14 | +### Context |
| 15 | +This work was made during an art residency hosted at the [Stereolux, Laboratoire Arts et Technologies](http://www.stereolux.org/laboratoire-arts-et-technologies) with [Coup de foudre](https://www.facebook.com/coup.defoudre.716) and [Xavier Seignard](http://drangies.fr). |
| 16 | + |
| 17 | +The goal was to create a tablet app to control a [tesla coil](http://www.youtube.com/watch?v=X2elQ6RR7lw) with an [Arduino](http://arduino.cc). The chosen technology ([Cordova](http://cordova.io)) had no capabilities to handle such serial over usb communication. |
| 18 | + |
| 19 | +### Install it |
| 20 | +From the root folder of your cordova project, run : |
| 21 | +``` |
| 22 | +cordova plugin add https://github.com/stereolux/cordovarduino.git |
| 23 | +cp plugins/org.stereolux.cordova.serial/lib/usbseriallibrary.jar platforms/android/libs |
| 24 | +``` |
| 25 | + |
| 26 | +### How to use it |
| 27 | +Thanks to [usb-serial-for-android](https://github.com/mik3y/usb-serial-for-android) library, you can communicate with CDC, FTDI, Arduino and other devices. Here is the Cordova plugin API. |
| 28 | + |
| 29 | +Because you're polite, first request the permission to use the serial port to the system: |
| 30 | +```js |
| 31 | +serial.requestPermission(function success(), function error()); |
| 32 | +``` |
| 33 | +You can now open the serial port: |
| 34 | +```js |
| 35 | +serial.open(opts, function success(), function error()); |
| 36 | +``` |
| 37 | +`opts` is a JSON object with the following properties: |
| 38 | + |
| 39 | +- baudRate: defaults to 9600 |
| 40 | +- dataBits: defaults to 8 |
| 41 | +- stopBits: defaults to 1 |
| 42 | +- parity: defaults to 0 |
| 43 | + |
| 44 | +You're now able to read and write: |
| 45 | +```js |
| 46 | +serial.write(data, function success(), function error()); |
| 47 | +serial.read(function success(buffer), function error()); |
| 48 | +``` |
| 49 | +`data` is the string representation to be written to the serial port. |
| 50 | +`buffer` is a JavaScript ArrayBuffer containing the data that was just read. |
| 51 | + |
| 52 | +And finally close the port: |
| 53 | +```js |
| 54 | +serial.close(function success(), function error()) |
| 55 | +``` |
| 56 | + |
| 57 | +### Example |
| 58 | + |
| 59 | +A callback-ish example. |
| 60 | + |
| 61 | +```js |
| 62 | +var errorCallback = function(message) { |
| 63 | + alert('Error: ' + message); |
| 64 | +}; |
| 65 | + |
| 66 | +serial.requestPermission( |
| 67 | + function(successMessage) { |
| 68 | + serial.open( |
| 69 | + {baudRate: 9600}, |
| 70 | + function(successMessage) { |
| 71 | + serial.write( |
| 72 | + '1', |
| 73 | + function(successMessage) { |
| 74 | + alert(successMessage); |
| 75 | + }, |
| 76 | + errorCallback |
| 77 | + ); |
| 78 | + }, |
| 79 | + errorCallback |
| 80 | + ); |
| 81 | + }, |
| 82 | + errorCallback |
| 83 | +); |
| 84 | +``` |
0 commit comments