2

I have some nodejs code that sends data through serial port. My problem is I'm coding the arduino part blinded. Since I can't see the data that my arduino reads from serial.

Node.js

serialport.write('3');

Arduino.ino

char rcved = Serial.read();

I need to see what I get in rcved. But when I tried:

Serial.println(rcved);

and then opening the Serial Monitor I get the error that the Serial port is busy. I understand that it is being used by Node.js to send the data.. But how can I see what my arduino code is reading then!?

The error:

processing.app.SerialException: Error opening serial port 'COM4'.
at processing.app.Serial.<init>(Unknown Source)
at processing.app.Serial.<init>(Unknown Source)
at processing.app.SerialMonitor$3.<init>(SerialMonitor.java:94)
at processing.app.SerialMonitor.open(SerialMonitor.java:94)
at processing.app.Editor.handleSerial(Editor.java:2536)
at processing.app.EditorToolbar.mousePressed(EditorToolbar.java:357)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Caused by: jssc.SerialPortException: Port name - COM4; Method name -     openPort(); Exception type - Port busy.
at jssc.SerialPort.openPort(SerialPort.java:164)
... 37 more
Error opening serial port 'COM4'.

Please help! I need this to debug

Thank you

1 Answer 1

6

Sending serial messages to the Arduino is not as easy as simply passing in a String. Unfortunately you have to send the String character by character which the Arduino will receive and concatenate back to a String. After you sent the last character you need to send one final new line character (/n) which is a signal for the Arduino to stop concatenating and evaluate the message.

This is what you need to do in your Node.js server:

// Socket.IO message from the browser
socket.on('serialEvent', function (data) {

    // The message received as a String
    console.log(data);

    // Sending String character by character
    for(var i=0; i<data.length; i++){
        myPort.write(new Buffer(data[i], 'ascii'), function(err, results) {
            // console.log('Error: ' + err);
            // console.log('Results ' + results);
        });
    }

    // Sending the terminate character
    myPort.write(new Buffer('\n', 'ascii'), function(err, results) {
        // console.log('err ' + err);
        // console.log('results ' + results);
    });
});

And this is the Arduino code that receives this:

String inData = "";

void loop(){
    while (Serial.available() > 0) {
        char received = Serial.read();
        inData.concat(received);

        // Process message when new line character is received
        if (received == '\n') {
            // Message is ready in inDate
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.