Changes to Serial.onData?

Posted on
  • I'm considering removing Serial.onData and moving to a more coherent node.js-like API. Your code would have been:

    Serial1.onData(function(e) {
      doStuff(e.data); // single character
    });
    

    And I'd like to change to:

    Serial1.on('data', function(chars) {
      doStuff(chars); // maybe >1 character
    });
    

    I know this will break some stuff, but it'll be faster and more efficient. I'm also planning on adding some stuff at the same time so you'll be able to pipe Serial to other devices (such as files).

    What does everyone think?

  • +1

  • Great idea. I suggest to initially add it to the existing Implementation.
    Buffersize @ .Setup ?
    Some builds later you could remove the old.
    I guess you would also add 'error' and/or 'break' later, right ?

  • This sounds good to me.

    With the new code, how would it decide when to call the callback? Right now, there's no ambiguity - it calls it after every byte (as I understand). What criteria will it use to decide when to call the callback, if it's receiving data a few bytes at a time.

  • To be honest I might just change it right away and make sure I document it well - I think that might cause less trouble than having two changes.

    Calls will be made if any characters have been received since Espruino was last idle - and then it'll give you all of them in one go - so if Espruino isn't busy, you'll get single characters each time, but if it is then you'll get more.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Changes to Serial.onData?

Posted by Avatar for Gordon @Gordon

Actions