树莓派、Arduino、Node.js和串口通信技术

10
我是一个有用的助手,可以为您翻译文本。
我正在尝试从Node.js服务器脚本与我的Arduino通信。
以下是我的代码:
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);
    });
});

我相信我的设备在/dev/ttyACM0上,因为:

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 $

当我尝试运行我的服务器脚本时,出现了以下错误:
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 (node.js:244:9)

我确定我只是错过了一些简单的东西,但我对Linux或Node的了解不足,不知道是什么。我需要安装Arduino IDE来获取驱动程序吗?因为我正在ssh连接我的树莓派,我知道这使用串行端口,但我想通过USB通信。这可能吗,还是我只有一个串行端口,无论是USB还是串行?
编辑:我已经安装了IDE,并且可以通过IDE与arduino进行通信。所以这不是驱动程序或端口不足的问题。
谢谢任何帮助。

尝试使用非80的端口进行POST请求? - Naftali
3个回答

10
我认为这是因为空参数传递给了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
也许这段代码可以帮助到某些人。我在树莓派 Zero W 上运行它。
它使用解析器获取固定长度的字节(例如此例中的 16 个字节),并检查第一个字符是否为冒号。
var SerialPort = require('serialport');
var port = new SerialPort('/dev/ttyAMA0', {
   baudRate: 57600,
   dataBits: 8,
   parity: 'none',
   stopBits: 1,
   flowControl: false
});

var ByteLength = require('@serialport/parser-byte-length');
var parser = port.pipe(new ByteLength({length: 16}));


parser.on('data', function (data) {
    var dataUTF8 = data.toString('utf-8');
    if (dataUTF8.substring(0, 1) === ":") {
        console.log('Data: ' + data);
    }
});

注意:这个页面可能对像我一样在RPI串口上遇到问题的人有用。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接