以下是环境详细信息:
package.json "serialport": "^11.0.0"
node -v v18.16.0
npm -v 9.5.1
index.js
const {SerialPort} = require('serialport');
const port = new SerialPort({
path: '/dev/ttyUSB0',
baudRate: 57600,
});
console(非sudo用户)
$ node index
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
Error: Error: Permission denied, cannot open /dev/ttyUSB0
Emitted 'error' event on SerialPort instance at:
at SerialPort._error (/var/www/node-serialport/node_modules/@serialport/stream/dist/index.js:78:18)
at /var/www/node-serialport/node_modules/@serialport/stream/dist/index.js:111:18
Node.js v18.16.0
console(with sudo user):
$ sudo node index
/var/www/node-serialport/node_modules/@serialport/stream/dist/index.js:71
return (this.port?.isOpen ?? false) && !this.closing;
^
SyntaxError: Unexpected token .
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/var/www/node-serialport/node_modules/serialport/dist/serialport-mock.js:4:18)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
1条答案
按热度按时间t2a7ltrp1#
您需要使用提升的特权运行节点,或者给予当前用户访问串行端口。
How do I allow a non-default user to use serial device ttyUSB0?
Read/Write to a Serial Port Without Root?