Skip to content

Conversation

@elafargue
Copy link
Contributor

This PR implement reading from the serial port. This is a simple (unique) callback driven mechanism, so the javascript app will have to continuously re-poll the serial port after getting data or a timeout (buffer with length = 0). A better mechanism would be to trigger "onData" events.

Also, added more checks on the state of the serial port, so that in case a Cordova app attempts read/write/close operations on ports that were not previously opened, the app does not crash but calls the error callback.

xseignard added a commit that referenced this pull request Mar 23, 2014
Implement reading from the serial port. Better error checking
@xseignard xseignard merged commit 54309fb into xseignard:master Mar 23, 2014
@xseignard
Copy link
Owner

So I merged and tried your code, but I can't read from the Arduino.
Do you have some simple snippet I can test against?

Regards,

Xavier

@elafargue
Copy link
Contributor Author

Hello Xavier - je pense qu'on peut parler français :)

J'ai uniquement testé avec un FTDI jusqu'ici, voici le code que
j'utilise:

  • Ouverture du port d'abord
  • Callback "onOpen": serial.read(onRead)
  • Callback onRead : pour mon utilisation, je fais un split sur ";", si tu veux lire ligne par ligne, fais un split sur '\n' ou '\r'...

function onRead(readInfo) {
// readInfo is an ArrayBuffer
if (readInfo.byteLength == 0) {
// Delay next attempt to read in order to avoid a fast loop
setTimeout(function() {
serial.read(onRead);
}, 100);
return;
}
// Inspired by Node's serialport library, why reinvent the wheel:
data += ab2str(readInfo);
// Split collected data by delimiter
var parts = data.split(';')
data = parts.pop();
parts.forEach(function (part, i, array) {
serialEvent(part);
});
// Keep on reading.
serial.read(onRead);
};

Hoping this helps! Very stable for me, I tested this yesterday for more
than 30 minutes of intense serial exchanges without any issue,

Ed

On Sun, Mar 23, 2014 at 10:09 AM, Xavier Seignard
[email protected]:

So I merged and tried your code, but I can't read from the Arduino.
Do you have some simple snippet I can test against?

Regards,

Xavier

Reply to this email directly or view it on GitHubhttps://github.com//pull/3#issuecomment-38388623
.

@xseignard
Copy link
Owner

Salut,

I found my problem, it doesn't work with my Arduino Leonardo, but I tested with an Arduino Pro Mini and it works, weird.

Here is the snippet I used

var data;
var paragraph = document.getElementById('test');
var ab2str = function(buf) {
    return String.fromCharCode.apply(null, new Uint8Array(buf));
};
var errorCallback = function(message) {
    alert('Error: ' + message);
};
var onRead = function (readInfo) {
    // readInfo is an ArrayBuffer
    if (readInfo.byteLength == 0) {
        console.log('no data');
        // Delay next attempt to read in order to avoid a fast loop
        setTimeout(function() {
            serial.read(onRead);
        }, 100);
        return;
    }
    // Inspired by Node's serialport library, why reinvent the wheel:
    data += ab2str(readInfo);
    var parts = data.split('\n');
    data = parts.pop();
    console.log(data);
    paragraph.textContent = data;
    // Keep on reading.
    serial.read(onRead);
};

serial.requestPermission(
    function(successMessage) {
        serial.open(
            {baudRate: 9600},
            function(successMessage) {
                serial.read(onRead);
            },
            errorCallback
        );
    },
    errorCallback
);

Merci de ton aide!

a+

@elafargue
Copy link
Contributor Author

Perfect ! Glad it worked for you too.

I'm using your plugin for a custom amateur radio transceiver controller,
I might release it publicly one of these days, but ironically, my visa
status in the US makes it difficult to do, because I am only allowed to
work for my current employer, and this is done outside of them...

Ed

On Sun, Mar 23, 2014 at 12:07 PM, Xavier Seignard
[email protected]:

Salut,

I found my problem, it doesn't work with my Arduino Leonardo, but I tested
with an Arduino Pro Mini and it works, weird.

Here is the snippet I used

var data;
var paragraph = document.getElementById('test');
var ab2str = function(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
};
var errorCallback = function(message) {
alert('Error: ' + message);
};
var onRead = function (readInfo) {
// readInfo is an ArrayBuffer
if (readInfo.byteLength == 0) {
console.log('no data');
// Delay next attempt to read in order to avoid a fast loop
setTimeout(function() {
serial.read(onRead);
}, 100);
return;
}
// Inspired by Node's serialport library, why reinvent the wheel:
data += ab2str(readInfo);
var parts = data.split('\n');
data = parts.pop();
console.log(data);
paragraph.textContent = data;
// Keep on reading.
serial.read(onRead);
};

serial.requestPermission(
function(successMessage) {
serial.open(
{baudRate: 9600},
function(successMessage) {
serial.read(onRead);
},
errorCallback
);
},
errorCallback
);

Merci de ton aide!

a+

Reply to this email directly or view it on GitHubhttps://github.com//pull/3#issuecomment-38393402
.

@xseignard
Copy link
Owner

Even if it is open source?

Btw, I still have some troubles with the decoding of the ArrayBuffer, I get some unreadable characters, I don't know if it comes from the cheap usb/serial converter I use, or from the way I translate the buffer to a string, hre is what I do:

var ab2str = function(buf) {
    return String.fromCharCode.apply(null, new Uint8Array(buf));
};

What's your technique? I tried to search on the source of node-serialport, but with no luck.

And how do you send data from your device to Android?

In Arduino I do a simple thing like this:

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
}

Any idea?

Regards,

Xavier

@elafargue
Copy link
Contributor Author

Open Source might be an option, but then I will get hate mail from quite
a few people who are selling the same sort of apps as their main activity -
or selling online services, what I did is close to www.remotehamradio.com,
only nicer ;-) Anyway, I'll work it out eventually, no worries.

Here is what I do in my code: same ab2str technique as yours, no issue
with undreadable characters.

My device is a Ham radio (elecraft KX3, look it up), which sends data on
the serial port using a fairly straightforward ASCII protocol - with a bit
of binary here and there. I have not tried with Arduinos yet, will come
later I guess!

Ed

On Sun, Mar 23, 2014 at 3:00 PM, Xavier Seignard
[email protected]:

Even if it is open source?

Btw, I still have some troubles with the decoding of the ArrayBuffer, I
get some unreadable characters, I don't know if it comes from the cheap
usb/serial converter I use, or from the way I translate the buffer to a
string, hre is what I do:

var ab2str = function(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
};

What's your technique? I tried to search on the source of node-serialport,
but with no luck.

And how do you send data from your device to Android?

In Arduino I do a simple thing like this:

void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
}

Any idea?

Regards,

Xavier

Reply to this email directly or view it on GitHubhttps://github.com//pull/3#issuecomment-38398942
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants