Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement serial reading. Documentation to come.
  • Loading branch information
elafargue committed Mar 20, 2014
commit aa07dc0338b2678b7715dc9b18bf02ddc2b28151
75 changes: 5 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,11 @@
## Cordovarduino

Cordovarduino is a Cordova/Phonegap plugin that enable you to use serial communication from an Android device to a serial over USB capable one.
This is my own branch of Cordovarduino. This one actually works... for me at least.

It's a **work in progress** : Android to Arduino works, Arduino to Android is not yet supported
Differences with the official branch:

### Context
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).
- Implemented reading from the serial port
- Bugfixed usb serial lib that works with FTDI devices

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.
Changes by Edouard Lafargue ([email protected])

### Install it
From the root folder of your cordova project, run :
```
cordova plugin add https://github.com/stereolux/cordovarduino.git
cp plugins/org.stereolux.cordova.serial/lib/usbseriallibrary.jar platforms/android/libs
```

### How to use it
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.

Because you're polite, first request the permission to use the serial port to the system:
```js
serial.requestPermission(function success(), function error());
```
You can now open the serial port:
```js
serial.open(opts, function success(), function error());
```
`opts` is a JSON object with the following properties:

- baudRate: defaults to 9600
- dataBits: defaults to 8
- stopBits: defaults to 1
- parity: defaults to 0

You're now able to read and write:
```js
serial.write(data, function success(), function error());
serial.read(function success(), function error());
```
`data` is the string representation to be written to the serial port.

And finally close the port:
```js
serial.close(function success(), function error())
```

### Example

A callback-ish example.

```js
var errorCallback = function(message) {
alert('Error: ' + message);
};

serial.requestPermission(
function(successMessage) {
serial.open(
{baudRate: 9600},
function(successMessage) {
serial.write(
'1',
function(successMessage) {
alert(successMessage);
},
errorCallback
);
},
errorCallback
);
},
errorCallback
);
```
Binary file modified lib/usbseriallibrary.jar
Binary file not shown.
29 changes: 25 additions & 4 deletions src/android/org/stereolux/cordova/serial/Serial.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.stereolux.cordova.serial;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -21,6 +23,7 @@
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;
import com.hoho.android.usbserial.util.SerialInputOutputManager.Listener;

/**
* Cordova plugin to communicate with the android serial port
Expand All @@ -42,6 +45,13 @@ public class Serial extends CordovaPlugin {
// The serial port that will be used in this plugin
private UsbSerialPort port;

private static final int READ_WAIT_MILLIS = 200;
private static final int BUFSIZ = 4096;

private final ByteBuffer mReadBuffer = ByteBuffer.allocate(BUFSIZ);
private final ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ);


/**
* Overridden execute method
* @param action the string representation of the action to execute
Expand Down Expand Up @@ -191,10 +201,21 @@ public void run() {
private void readSerial(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
byte buffer[] = new byte[16];
int numBytesRead = port.read(buffer, 1000);
callbackContext.success(numBytesRead);
try {
int len = port.read(mReadBuffer.array(), READ_WAIT_MILLIS);
// Whatever happens, we send an "OK" result, up to the
// receiver to check that len > 0
PluginResult.Status status = PluginResult.Status.OK;
if (len > 0) {
Log.d(TAG, "Read data len=" + len);
final byte[] data = new byte[len];
mReadBuffer.get(data, 0, len);
mReadBuffer.clear();
callbackContext.sendPluginResult(new PluginResult(status,data));
} else {
final byte[] data = new byte[0];
callbackContext.sendPluginResult(new PluginResult(status, data));
}
}
catch (IOException e) {
// deal with error
Expand Down