2013-02-22 134 views
11

我试图从node.js服务器脚本与我的arduino交谈。Raspberry Pi,Arduino,Node.js和串口

这里是我的代码:

var app = require('express')() 
, server = require('http').createServer(app) 
, io = require('socket.io').listen(server) 
, SerialPort = require('serialport').SerialPort; 

//SERIAL 
var portName = '/dev/ttyACM0'; 
var sp = new SerialPort(); // instantiate the serial port. 
sp.open(portName, { // portName is instatiated to be COM3, replace as necessary 
    baudRate: 115200, // this is synced to what was set for the Arduino Code 
    dataBits: 8, // this is the default for Arduino serial communication 
    parity: 'none', // this is the default for Arduino serial communication 
    stopBits: 1, // this is the default for Arduino serial communication 
    flowControl: false // this is the default for Arduino serial communication 
}); 

//SERVER 
server.listen(80, '127.0.0.5'); 

app.get('/', function (req, res){ 
    res.sendfile(__dirname + '/index.html'); 
}); 

io.sockets.on('connection', function (socket){ 
    socket.emit('test', { test: 'Its Working' }); 
    socket.on('value', function (data){ 
    console.log(data); 
    }); 
}); 

Im相当肯定我的设备是在/ dev/ttyACM0因为:

[email protected] ~/Programming/node $ dmesg|tail 
[91569.773042] cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device 
[91569.776338] usbcore: registered new interface driver cdc_acm 
[91569.776369] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters 
[92601.131298] usb 1-1.2: USB disconnect, device number 7 
[92609.044999] usb 1-1.2: new full-speed USB device number 8 using dwc_otg 
[92609.149759] usb 1-1.2: New USB device found, idVendor=2341, idProduct=0043 
[92609.149789] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=220 
[92609.149806] usb 1-1.2: Manufacturer: Arduino (www.arduino.cc) 
[92609.149820] usb 1-1.2: SerialNumber: 74132343430351705051 
[92609.156743] cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device 
[email protected] ~/Programming/node $ 

当我尝试运行我的服务器脚本中,我得到的错误:

[email protected] ~/Programming/node $ node server.js 
    info - socket.io started 

/home/pi/node_modules/serialport/serialport.js:72 
    throw new Error('Invalid port specified: ' + path); 
     ^
Error: Invalid port specified: undefined 
    at new SerialPort (/home/pi/node_modules/serialport/serialport.js:72:11) 
    at Object.<anonymous> (/home/pi/Programming/node/server.js:8:10) 
    at Module._compile (module.js:449:26) 
    at Object.Module._extensions..js (module.js:467:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.runMain (module.js:492:10) 
    at process.startup.processNextTick.process._tickCallback (node.js:244:9) 

我确定我只是缺少一些简单的东西,但我不知道Linux或节点知道它是什么。我是否需要安装驱动程序的arduino IDE? 是不是因为我对我的覆盆子pi sshing,我知道这使用串行端口,但我想通过USB通信。这是可能的还是我只有1串口,无论其USB或串行?

编辑 我已经安装了IDE,我可以通过IDE与arduino交谈。所以这不是司机或港口的缺乏。

感谢您的任何帮助。

+0

尝试使用不是** 80的帖子吗? – Neal 2013-02-22 17:03:44

+0

你是什么意思? – 2013-02-22 17:04:53

回答

8

我想这是因为空参数SerialPort,你后来在开放

var sp = new SerialPort(); // instantiate the serial port. 
//then you open 
sp.open(portName, { // portName is instatiated to be COM3, replace as necessary 

指定从SerialPort NPM项目页面

var SerialPort = require("serialport").SerialPort 
var serialPort = new SerialPort("/dev/tty-usbserial1"); 

When opening a serial port, you can specify (in this order). 
1. Path to Serial Port - required. 
2. Options - optional and described below. 

所以,你应该指定所有SerialPort中的参数而不是打开的

var portName = '/dev/ttyACM0'; 
var sp = new SerialPort(portName, { 
    baudRate: 115200, 
    dataBits: 8, 
    parity: 'none', 
    stopBits: 1, 
    flowControl: false 
}); 
+0

就是这样,非常感谢 – 2013-02-25 12:05:26

0

我有一个工作nodeJS/arduino/Serialport机器人。我用

(你需要改变你的串口你自己的)

var serialport = require("serialport"); 
    var SerialPort = serialport.SerialPort; // localize object constructor 

    var sp = new SerialPort(comPort, { 
    parser: serialport.parsers.readline("\r"), 
    baudrate: 9600 
    }); 

    sp.on("open", function() { 
    sp.write(0x80); 
    sp.write('123456\r'); 
    console.log ("comm port ready"); 
    }); 

记住当你写你的Arduino于“消耗”的输出。我的工作代码告诉机器人要朝特定的方向前进。

  robotData.setLastCommand(direction); 
    sp.write(direction , function(err, results) { 
     **sp.drain(function(err, result){** 
       //console.log ("drain"); 
        //console.log(err, result); 
     }); 
     //console.log ("results -> " + results); 
    });