未加星标

Raspberry Pi, Arduino, Node.js and serial port

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Im trying to talk to my arduino from a node.js server script.

Here's my code:

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 pretty sure my device is on /dev/ttyACM0 because:

pi@raspberrypi ~/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 pi@raspberrypi ~/Programming/node $

When i try and run my server script i get the error:

pi@raspberrypi ~/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 (<strong>node.js</strong>:244:9)

Im sure im just missing something simple but i don't know enough about linux or Node to know what it is. Do i need to install the arduino IDE for drivers? Is it because i am sshing into my raspberry pi, i know this uses the serial port but i want to communicate over USB. Is this possible or do i only have 1 serial port regardless of whether its USB or serial?

EDIT I have installed the IDE and I can talk to the arduino through the IDE. So it's not drivers or lack of ports.

Thanks for any help.

Joe

Problem courtesy of: Joseph Roberts

Solution

I think it is because empty arguments to SerialPort , which you later specify in open

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

From SerialPort npm project page

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.

So you should specify all the arguments in SerialPort instead of open

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

Solution courtesy of: user568109

Discussion

I have a working nodeJS / arduino / Serialport robot.

I used (you need to change your serial port to your own)

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

RememberWhen you write to your Arduino to "drain" the output. My working code where I tell the robot to go in a specific direction.

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

Discussion courtesy of: alexmac

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Raspberry Pi, Arduino, Node.js and serial port
本站链接:https://www.codesec.net/view/621251.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(23)