我编写了一个程序,将数据发送到arduino,该程序检测发送的内容,然后根据所按下的键打开正确的引脚。
在Windows计算机上使用arduino软件时,arduino草图可以正常工作,我可以通过发送WAS或D来打开和关闭每个引脚。
通过节点发送时,arduino上的RX指示灯闪烁,但没有其他反应。
有人可以帮忙吗?
Node.js程序:
var httpServer = require('http').createServer(function(req, response){ /* Serve your static files */ }) httpServer.listen(8080); var nowjs = require("now"); var everyone = nowjs.initialize(httpServer); everyone.now.logStuff = function(msg){ console.log(msg); } var SerialPort = require('serialport2').SerialPort; var assert = require('assert'); var portName; if (process.platform == 'win32') { portName = 'COM4'; } else if (process.platform == 'darwin') { portName = '/dev/cu.usbserial-A800eFN5'; } else { portName = '/dev/ttyUSB0'; } var readData = ''; var sp = new SerialPort(); sp.on('close', function (err) { console.log('port closed'); }); sp.on('error', function (err) { console.error("error", err); }); sp.on('open', function () { console.log('port opened... Press reset on the Arduino.'); }); sp.open(portName, { baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1, flowControl: false }); everyone.now.forward = function() { sp.write("w"); } everyone.now.back = function() { sp.write("s"); } everyone.now.left = function() { sp.write("a"); } everyone.now.right = function() { sp.write("d"); } sp.on('data', function(data) { console.log(data.toString()); });
Arduino程序:
void setup(){ Serial.begin(9600); Serial.write("READY"); //Set all the pins we need to output pins pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); } void loop (){ if (Serial.available() > 0) { //read serial as a character char ser = Serial.read(); Serial.write(ser); //NOTE because the serial is read as "char" and not "int", the read value must be compared to character numbers //hence the quotes around the numbers in the case statement switch (ser) { case 'w': move(8); break; case 's': move(9); break; case 'a': move(10); break; case 'q': move(10); move(8); break; case 'd': move(11); break; case 'e': move(11); move(8); break; } } } void move(int pin){ Serial.print(pin); digitalWrite(pin, HIGH); delay(1); digitalWrite(pin, LOW); }
我最近涉足这一领域。当Arduino从Arduino IDE以外的其他设备接收到串行通信时,它会自动重置。这就是为什么您可以从IDE发送但不能从node.js发送的原因。
我有一个Uno,并在Reset和Ground之间放置了一个电容器。这是有关此主题的一些信息的页面。 祝好运。 http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection