Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial port list throttling and repl error with multiple boards #1456

Merged
merged 3 commits into from
Apr 5, 2018
Merged

Serial port list throttling and repl error with multiple boards #1456

merged 3 commits into from
Apr 5, 2018

Conversation

dnlup
Copy link
Contributor

@dnlup dnlup commented Mar 23, 2018

Reading issue #1406 I started playing with Board.detect and indeed in case
of no connection the function gets called with no throttling.

I found a couple of other things too:

  • if a connected board(s) is(are) disconnected while running, when terminating the process the SIGINT event gets sometimes ignored (it has happened running a board with repl: true and repl: false)

  • if multiple boards are connected with repl: true, when exiting the process an error is thrown because state.board.io.pending is not defined

In this PR a simple solution to the first problem follows what @dtex suggests in the issue discussion, while for the other two I add a fail safe timer of 1 second to force exit and fix the code in the repl.js module to handle the case of multiple boards.

This is not definitive and I don't know how much of this is needed, I would like to know what do you think (@rwaldron @dtex).

When listing available serial connections, a maximum number of 10
attempts every 400 milliseconds are allowed, then the `board.fail`
method is called.
When disconnecting on SIGINT, add a 1000 millisecond timeout before
forcing the exit. This has been added because if one disconnects a
succesfully connected board the process could never exit on SIGINT.
When connecting multiple boards, an exception is thrown when exiting the
process
because `state.board.io.pending` is not defined. This commit fixes the
problem by checking the `length` property of `state.board` and looping
through the board list to check the `board.io.pending` property.
Futhermore, it add a timeout of 1000 milliseconds to force the process
exit in case the `board.io.pending` property never becomes `false` (it
has happened disconnetting a succesfully connected board).
Since `board.fail` is called when connection fails, it might be useful
to echo the event in the Boards class.
@@ -88,7 +91,14 @@ var Serial = {
Serial.attempts[Serial.used.length]++;

// Retry Serial connection
Serial.detect.call(this, callback);
if (Serial.attempts[Serial.used.length] > maxAttempts) {
this.fail("Board", "No connected device found");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I would have logged the id of the board but I haven't because when initializing multiple boards without specifing ids they all end up having the same generated uid. Is this how it is supposed to be?

@rwaldron
Copy link
Owner

I'm going to need a bit of time to test and fully understand these changes. I appreciate your patience.

@dnlup
Copy link
Contributor Author

dnlup commented Mar 27, 2018

@rwaldron Absolutely, thanks for reviewing this PR. If you need anything (a basic code sample that triggers the issue or anything else) I am here to help.

@rwaldron
Copy link
Owner

rwaldron commented Apr 3, 2018

@dtex @reconbot @monteslu Any thoughts on this?

Copy link
Collaborator

@reconbot reconbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true anymore, list will report an error if there's an error. Also port.list() returns a promise if there isn't a callback.

All in all a good approach.

// Max number of times listing serial conntections can fail
var maxAttempts = 10;
// Delay (ms) before trying again to list serial connections
var retryDelay = 400;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gut says this is a fine timeout.

@rwaldron
Copy link
Owner

rwaldron commented Apr 3, 2018

I've been in the middle of moving my office for about a month now :( All of my hardware is in boxes and I'm slowing unpacking things. I'd like to try this with real hardware before I land it—I appreciate your patience

@dtex
Copy link
Collaborator

dtex commented Apr 3, 2018

Is anybody here able to test with X-Bee's? It's probably fine, but that's a special case that may be affected.

@rwaldron
Copy link
Owner

rwaldron commented Apr 5, 2018

I've tested this against hardware and I'm satisfied with the changes—thanks and great work!!

  • w/ 1 UNO (unaffected)
  • w/ 2 UNOs
  • w/ 1 UNO + 1 Leonardo
  • w/ 1 UNO + 1 Mega
  • w/ 2 Megas
  • w/ 1 UNOs via XBee
  • w/ 2 UNOs via XBee

@rwaldron rwaldron merged commit 9ca2f5b into rwaldron:master Apr 5, 2018
@dnlup dnlup deleted the serial-port-detect branch April 6, 2018 07:44
@dnlup
Copy link
Contributor Author

dnlup commented Apr 6, 2018

@rwaldron Happy this PR was useful :-). I have one question if you don't mind.
While testing this code I saw that if I initialize multiple boards
without passing ids

var boards = new five.Boards([
   { port: '/dev/ttyACM0' },
   { port: '/dev/ttyACM1' },
   { port: '/dev/ttyACM2' }
]);

all the boards will have the same generated id. Is this how it is supposed to be
or the ids should be different?
(See comment https://github.com/rwaldron/johnny-five/pull/1456/files/0c70eee531d70bd90924d77d77c6b5feced29f7c#diff-54ffaa39b9e68a102065e0d7a79b6ea8R94, there I was trying to log the id of the board(s) and saw that the ids were the same when not explicitly passed during initializiation)

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.

None yet

4 participants