Skip to content

Commit 54309fb

Browse files
committed
Merge pull request #3 from elafargue/master
Implement reading from the serial port. Better error checking
2 parents 80995be + 4aecc4a commit 54309fb

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
Cordovarduino is a Cordova/Phonegap plugin that enable you to use serial communication from an Android device to a serial over USB capable one.
44

5-
It's a **work in progress** : Android to Arduino works, Arduino to Android is not yet supported
5+
It's a **work in progress** : Android to Arduino works, Arduino to Android now works.
6+
7+
8+
## Change log
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.
613

714
### Context
815
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).
@@ -37,9 +44,10 @@ serial.open(opts, function success(), function error());
3744
You're now able to read and write:
3845
```js
3946
serial.write(data, function success(), function error());
40-
serial.read(function success(), function error());
47+
serial.read(function success(buffer), function error());
4148
```
4249
`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.
4351

4452
And finally close the port:
4553
```js

lib/usbseriallibrary.jar

496 Bytes
Binary file not shown.

src/android/org/stereolux/cordova/serial/Serial.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.stereolux.cordova.serial;
22

33
import java.io.IOException;
4+
import java.nio.ByteBuffer;
45
import java.util.List;
56

67
import org.apache.cordova.CallbackContext;
8+
import org.apache.cordova.PluginResult;
79
import org.apache.cordova.CordovaPlugin;
810
import org.json.JSONArray;
911
import org.json.JSONException;
@@ -21,6 +23,7 @@
2123
import com.hoho.android.usbserial.driver.UsbSerialDriver;
2224
import com.hoho.android.usbserial.driver.UsbSerialPort;
2325
import com.hoho.android.usbserial.driver.UsbSerialProber;
26+
import com.hoho.android.usbserial.util.SerialInputOutputManager.Listener;
2427

2528
/**
2629
* Cordova plugin to communicate with the android serial port
@@ -42,6 +45,12 @@ public class Serial extends CordovaPlugin {
4245
// The serial port that will be used in this plugin
4346
private UsbSerialPort port;
4447

48+
private static final int READ_WAIT_MILLIS = 200;
49+
private static final int BUFSIZ = 4096;
50+
51+
private final ByteBuffer mReadBuffer = ByteBuffer.allocate(BUFSIZ);
52+
private final ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ);
53+
4554
/**
4655
* Overridden execute method
4756
* @param action the string representation of the action to execute
@@ -170,8 +179,11 @@ public void run() {
170179
private void writeSerial(final String data, final CallbackContext callbackContext) {
171180
cordova.getThreadPool().execute(new Runnable() {
172181
public void run() {
173-
Log.d(TAG, data);
182+
if (port == null) {
183+
callbackContext.error("writing a closed port");
184+
} else
174185
try {
186+
Log.d(TAG, data);
175187
byte[] buffer = data.getBytes();
176188
port.write(buffer, 1000);
177189
callbackContext.success();
@@ -191,10 +203,24 @@ public void run() {
191203
private void readSerial(final CallbackContext callbackContext) {
192204
cordova.getThreadPool().execute(new Runnable() {
193205
public void run() {
206+
if (port == null) {
207+
callbackContext.error("reading a closed port");
208+
} else
194209
try {
195-
byte buffer[] = new byte[16];
196-
int numBytesRead = port.read(buffer, 1000);
197-
callbackContext.success(numBytesRead);
210+
int len = port.read(mReadBuffer.array(), READ_WAIT_MILLIS);
211+
// Whatever happens, we send an "OK" result, up to the
212+
// receiver to check that len > 0
213+
PluginResult.Status status = PluginResult.Status.OK;
214+
if (len > 0) {
215+
Log.d(TAG, "Read data len=" + len);
216+
final byte[] data = new byte[len];
217+
mReadBuffer.get(data, 0, len);
218+
mReadBuffer.clear();
219+
callbackContext.sendPluginResult(new PluginResult(status,data));
220+
} else {
221+
final byte[] data = new byte[0];
222+
callbackContext.sendPluginResult(new PluginResult(status, data));
223+
}
198224
}
199225
catch (IOException e) {
200226
// deal with error
@@ -213,7 +239,10 @@ private void closeSerial(final CallbackContext callbackContext) {
213239
cordova.getThreadPool().execute(new Runnable() {
214240
public void run() {
215241
try {
216-
port.close();
242+
// Make sure we don't die if we try to close an non-existing port!
243+
if (port != null)
244+
port.close();
245+
port = null;
217246
callbackContext.success();
218247
} catch (IOException e) {
219248
// deal with error

0 commit comments

Comments
 (0)