gpt4 book ai didi

node.js - 树莓派、Arduino、Node.js 和串口

转载 作者:IT老高 更新时间:2023-10-28 23:16:42 25 4
gpt4 key购买 nike

我正在尝试通过 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 吗?是不是因为我正在连接我的树莓派,我知道这使用串行端口,但我想通过 USB 进行通信。这是可能的还是我只有 1 个串口,不管它是 USB 还是串口?

编辑我已经安装了 IDE,我可以通过 IDE 与 arduino 对话。所以这不是驱动程序或缺乏端口。

感谢您的帮助。

最佳答案

我认为这是因为 SerialPort 的参数为空,您稍后会在 open 中指定

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 中指定所有参数而不是 open

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

关于node.js - 树莓派、Arduino、Node.js 和串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15028240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com