Node.js串行端口访问被拒绝
我正在尝试通过node.js中的serialport发送字符串,代码如下:Node.js串行端口访问被拒绝,node.js,serial-port,Node.js,Serial Port,我正在尝试通过node.js中的serialport发送字符串,代码如下: function createSerialPort() { myPort = new SerialPort('COM3', { baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: 0 }) myPort.write("135"); } 每次我运行它时都会出现以下错误: (node:846
function createSerialPort() {
myPort = new SerialPort('COM3', {
baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: 0
})
myPort.write("135");
}
每次我运行它时都会出现以下错误:
(node:8468) UnhandledPromiseRejectionWarning: Error: Opening COM3: Access denied
(node:8468) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8468) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code
我到处寻找解决方法(我不熟悉使用serialport),我找到的唯一解决方案是:
function createSerialPort() {
myPort = new SerialPort('COM3', {
baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: 0
}, true)
myPort.write("135");
}
然后,该解决方案给了我以下信息:
(node:18192) UnhandledPromiseRejectionWarning: TypeError: callback.call is not a function
at SerialPort._error (C:\examplepath\node_modules\@serialport\stream\lib\index.js:198:14)
at C:\examplepath\node_modules\@serialport\stream\lib\index.js:242:12
(node:18192) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:18192) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
添加错误处理程序
myPort.on('error',function(err){console.log('error:',err.message);})
应该修复它,因此SerialPort的第三个参数应该是函数而不是布尔值。所以这myPort=newserialport('COM3',…,function(){…})代码>也可以解决此问题。