添加此语句
const port = new serialport('COM1',
{ autoOpen: true,
baudRate: 9600,
stopBits: 1},
((err) => {if (err){console.log('Error:', err)}
else {console.log('COM1 opened!')}})
)
到 Electron-serialport-quick-start ( https://github.com/serialport/electron-serialport-quick-start ) 导致 'npm run start' 失败(在 'rm -rf node_modules'、'rm package-lock. json', 'npm i' 命令序列)如下,如果推荐的 'postinstall' 脚本包含在 package.json 中:
Error: TypeError: Third argument must be a function
at internal/util.js:297:30
at new Promise (<anonymous>)
at open (internal/util.js:296:12)
at WindowsBinding.open (G:\CodeDev\quickStartSerialPort\node_modules\@serialport\bindings\lib\win32.js:56:22)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
有或没有可选的构造函数参数都会出现故障,同样,如果 autoOpen 设置为 false 并且随后调用 port.open (据我所知,port.open 的回调在串行端口文档中未被标识为选项)。
不知道这是串口还是安装后脚本问题,但如果从以下 package.json 中省略安装后脚本,可以确认端口打开时没有错误:
{
"name": "electron-serialport",
"version": "1.0.2",
"description": "A minimal Electron application with node serialport",
"main": "main.js",
"scripts": {
"start": "electron .",
"install": "electron-rebuild",
"postinstall": "electron-builder install-app-deps",
"build": "electron-builder -w nsis"
},
"repository": {
"type": "git",
"url": "git@github.com:serialport/electron-serialport.git"
},
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^11.2.0",
"electron-rebuild": "^2.3.4"
},
"dependencies": {
"serialport": "^9.0.7",
"tableify": "^0.0.3"
}
}
该问题出现在 Windows VS Code git bash 环境中;它尚未在其他平台上进行过测试。