NodeJS serial-port bug

Discussion in 'UDOO NEO' started by Nico, Feb 3, 2016.

  1. Nico

    Nico New Member

    Joined:
    Jan 19, 2016
    Messages:
    27
    Likes Received:
    5
    Hi guys,

    I have got a problem while using nodejs serial-port library.
    I can receive some datas, but the serial communication hangs up after 4 or 5 messages ...
    After that, my arduino code seems to be freezed (my code just light 3 different leds based on an input duration), the only to get it works correctly is to press the RST button ...

    I have noticed sometimes that the arduino seems to slow down, and when I connect to /dev/ttyMCC (using "screen /dev/ttyMCC 9600") it go back normally ...

    I can post my sketch here if it could help to debug, and my nodejs code, they are both ultra basic (just print data to serial, and get it with nodejs should reproduce this bug).

    Thanks,
    Nico

    Edit : I have found this, I don't understand all, but it seems to be related to this bug, I don't know if the commit added on December 22th is on Udubuntu 2 RC1 (released on December 23th)
     
    Last edited: Feb 3, 2016
  2. Nico

    Nico New Member

    Joined:
    Jan 19, 2016
    Messages:
    27
    Likes Received:
    5
    I have just tested same code on an UDOO Quad, and it is working well ... I can't freeze the Arduino by reading serial through nodejs
     
  3. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
  4. Nico

    Nico New Member

    Joined:
    Jan 19, 2016
    Messages:
    27
    Likes Received:
    5
    Thanks Andrea, I will check this tomorrow, and let you know if it works ;)
     
    Andrea Rovai likes this.
  5. Nico

    Nico New Member

    Joined:
    Jan 19, 2016
    Messages:
    27
    Likes Received:
    5
    I have checked out your solution, and it does not seem to resolve the problem ...
    Here is my simple arduino code (I have added a delay to slow down serial printing, but it does not change the problem)
    Code:
    #define BUTTON_PIN 2
    
    int buttonState = 0;
    int i = 0;
    
    void setup() {
      Serial.begin(115200);
    
      pinMode(BUTTON_PIN, INPUT);
    }
    
    void loop() {
        buttonState = digitalRead(BUTTON_PIN);
      
        if(buttonState == HIGH){
          Serial.print("Test : ");
          Serial.println(i++);
          delay(100);
        }
    }
    
    And here is my nodejs code :
    Code:
    var SerialPort = require("serialport").SerialPort
    
    var serialPort = new SerialPort("/dev/ttyMCC", {
        baudrate: 115200
    });
    
    serialPort.on("open", function(){
        console.log("open");
        serialPort.on("data", function(data){
            if(!data){
                return;          
            }
            console.log("data received : " + data);
        });
    });
    
    There is no problem using this command "screen /dev/ttyMCC 115200", all output a printed without any error.
    But while using nodejs after some ouptut reading the serial port seems to hang up ...
    Here is the output I get using my nodejs code :
    Edit :
    Here is the output I get using the same code on a UDOO Quad :
     
    Last edited: Feb 4, 2016
  6. jrullan

    jrullan New Member

    Joined:
    Dec 11, 2014
    Messages:
    28
    Likes Received:
    4
    @Nico

    Try to add a Serial.flush(); at the end of your loop(). I faced a similar problem in my Neo while exchanging messages with a C program in the Linux side. I found out that by using flush and avoiding the use of Serial.println() my code stabilized.

    Sent from my SAMSUNG-SM-G920A using Tapatalk
     
    Nico likes this.
  7. Nico

    Nico New Member

    Joined:
    Jan 19, 2016
    Messages:
    27
    Likes Received:
    5
    @jrullan
    Thanks for this hack! It works like a charm!! :)

    Edit : Here is the working arduino code :
    Code:
    #define BUTTON_PIN 2
    
    int buttonState = 0;
    int i = 0;
    
    void setup() {
        Serial.begin(115200);
    
        pinMode(BUTTON_PIN, INPUT);
    }
    
    void loop() {
        buttonState = digitalRead(BUTTON_PIN);
    
        if(buttonState == HIGH){
          Serial.print("Test : ");
          Serial.print(i++);
          delay(100);
       }
       Serial.flush();
    }
    
     
    Andrea Rovai and jrullan like this.
  8. jrullan

    jrullan New Member

    Joined:
    Dec 11, 2014
    Messages:
    28
    Likes Received:
    4
    @Nico,

    Great! It feels good to be able to help a fellow Neo user after struggling myself for over two weeks with this problem.

    I imagine, the problem lies in the implementation of the Serial object that the Neo uses.

    @Andrea,
    Maybe the Udoo team could fix it in the near future.

    Sent from my SAMSUNG-SM-G920A using Tapatalk
     
    Nico likes this.

Share This Page