我有一些通过串行端口发送数据的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$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'.
请帮忙!我需要这个来调试
谢谢
向Arduino发送串行消息并不像简单地传递一个String那样简单。不幸的是,您必须按字符发送String字符,Arduino会接收该字符并将其连接回String。发送完最后一个字符后,您需要发送一个最后的换行符(/ 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
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句