Nodejs使用serial将数据发送到Arduino

时间:2015-06-07 17:48:26

标签: node.js arduino serial-port

我有一些通过串口发送数据的nodejs代码。我的问题是我正在编码被蒙蔽的arduino部分。因为我看不到我的arduino从串口读取的数据。

Node.js的

serialport.write('3');

Arduino.ino

char rcved = Serial.read();

我需要看看我得到了什么。但是当我尝试时:

Serial.println(rcved);

然后打开串行监视器我收到串口忙的错误。我知道Node.js正在使用它来发送数据..但是我怎么能看到我的arduino代码正在读取的内容呢?

错误:

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'.

请帮忙!我需要这个来调试

谢谢

1 个答案:

答案 0 :(得分:4)

向Arduino发送串行消息并不像简单地传入String那么容易。不幸的是,您必须按字符发送字符串,Arduino将接收该字符并将其连接回字符串。在发送完最后一个字符后,您需要发送一个最后一个新行字符(/ n),这是Arduino停止连接和评估消息的信号。

这是您需要在Node.js服务器中执行的操作:

// 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);
    });
});

这是收到此内容的Arduino代码:

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
        }
    }
}