Nodejs 使用串口向 Arduino 发送数据

Nodejs send data to Arduino using serial

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

Node.js

serialport.write('3');

Arduino.ino

char rcved = Serial.read();

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

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.<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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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'.

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

谢谢

向 Arduino 发送串行消息并不像简单地传递一个字符串那么简单。不幸的是,您必须逐个字符发送字符串,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
        }
    }
}