gpt4 book ai didi

javascript - SerialPort 库 - "parsers.readline is not a function"错误 - NodeJS

转载 作者:搜寻专家 更新时间:2023-11-01 00:16:39 31 4
gpt4 key购买 nike

我已经复制了这段代码:

var serialport =require("serialport");
var SerialPort = serialport.SerialPort;
var portName = process.argv[2];

var myPort = new SerialPort(portName,{
baudRate: 9600,
parser:serialport.parsers.readline("\r\n")
})

myPort.on('open',onOpen);
myPort.on('data',onData);

function onOpen(){
console.log("Open connections!");
}

function onData(data){
console.log("on Data "+data);
}

来自此视频:https://www.youtube.com/watch?v=rhagmAv35Kk

我想将我电脑串口的数据传输到 NJS 代码中。当我运行代码时出现此错误:

c:\njs>node main.js
C:\njs\main.js:7
parser:serialport.parsers.readline("\r\n")
^

TypeError: serialport.parsers.readline is not a function
at Object.<anonymous> (C:\njs\main.js:7:28)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
at bootstrap_node.js:467:3

c:\njs>

有人知道为什么它不能识别该函数吗?

最佳答案

问题出在模块的版本上。根据最新版本,这段代码应该可以完成工作:

const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = new Readline();
port.pipe(parser);
parser.on('data', console.log);
port.write('ROBOT PLEASE RESPOND\n');

关于javascript - SerialPort 库 - "parsers.readline is not a function"错误 - NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46023151/

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