nodeJS 版本 18.15.0
目前寫的部分
const Readline = require('@serialport/parser-readline')
var SerialPort = require("serialport");
const parser = new Readline();
var COMPort = "Com3";
var port = new SerialPort(COMPort, {baudRate: 9600}).setEncoding('utf8');
port.on("open", (err) => {
console.log('serial port open'); //成功連接時印出port open
if(err){
console.log("no serial device found")//失敗時印出 device not found
}
},20);
port.pipe(parser)
parser.on('data', line =>{
console.log(line)
})
這兩行都會是 new的n 錯誤
const parser = new Readline();
var port = new SerialPort(COMPort, {baudRate: 9600}).setEncoding('utf8');
const parser = new Readline();
^
ReferenceError: Readline is not constructor
var port = new SerialPort(COMPort, {baudRate: 9600}).setEncoding('utf8');
^
ReferenceError: SerialPort is not constructor
npm install serialport
npm install express
目前只有用這兩個
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const port = new SerialPort({ path: '/dev/ROBOT', baudRate: 14400 })
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
官網範例...可能是你參考的教學有點舊。
你整個引用沒解構。