2013-02-22 22 views
11

Tôi đang cố gắng nói chuyện với Arduino của mình từ tập lệnh máy chủ node.js.Raspberry Pi, Arduino, Node.js và cổng nối tiếp

Dưới đây là mã của tôi:

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 khá chắc chắn điện thoại của tôi là on/dev/ttyACM0 vì:

[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 $ 

Khi tôi cố gắng và chạy kịch bản máy chủ của tôi tôi nhận được lỗi:

[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) 

Tôi chắc chắn rằng chỉ thiếu một thứ đơn giản nhưng tôi không biết đủ về Linux hoặc Nút để biết nó là gì. Tôi có cần cài đặt IDE arduino cho trình điều khiển không? Có phải vì tôi đang sshing vào pi mâm xôi của tôi, tôi biết điều này sử dụng cổng nối tiếp nhưng tôi muốn giao tiếp qua USB. Điều này có thể hoặc làm tôi chỉ có 1 cổng nối tiếp bất kể USB hoặc serial của nó?

EDIT Tôi đã cài đặt IDE và tôi có thể nói chuyện với Arduino thông qua IDE. Vì vậy, nó không phải là trình điều khiển hoặc thiếu cổng.

Cảm ơn bạn đã được trợ giúp.

Joe

+0

Thử sử dụng bài đăng ** không ** 80? – Neal

+0

Ý của bạn là gì? –

Trả lời

8

Tôi nghĩ rằng đó là vì đối số trống để SerialPort, mà sau này bạn chỉ định trong mở

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

Từ trang dự án 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. 

Vì vậy, bạn nên xác định tất cả các đối số trong SerialPort thay vì mở

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

Đó là nó, cảm ơn bạn rất nhiều –

0

Tôi có một nút làm việc JS/arduino/Serialport robot.

tôi đã sử dụng (bạn cần thay đổi cổng nối tiếp của bạn để riêng bạn)

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

Ghi Khi bạn viết thư cho Arduino của bạn để "thoát" đầu ra. Mã làm việc của tôi, nơi tôi bảo robot đi theo một hướng cụ thể.

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