gpt4 book ai didi

node.js - Node : SerialPort is not a constructor

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:05 25 4
gpt4 key购买 nike

我正在尝试通过 Node 连接到我的 Raspberry pi 的串行端口,但是当我运行 js 文件时出现此错误:

    var serialport = new SerialPort("/dev/ttyAMA0", {
^

TypeError: SerialPort is not a constructor
at Object.<anonymous> (/home/pi/exploringrpi/chp13/xbee/nodejs/test.js:12:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3

Node 版本为 8.11.4serialport 的版本是 7.0.2

这是代码:

// From the example code at www.npmjs.com/package/xbee-api
var util = require('util');
var SerialPort = require('serialport').SerialPort;

var xbee_api = require('xbee-api');
var C = xbee_api.constants;

var xbeeAPI = new xbee_api.XBeeAPI({
api_mode: 1
});

var serialport = new SerialPort("/dev/ttyAMA0", {
baudRate: 115200});
serialport.on("open", function() {
var frame_obj = { // AT Request to be sent to
type: C.FRAME_TYPE.AT_COMMAND, // Prepare for an AT command
command: "NI", // Node identifer command
commandParameter: [], // No parameters needed
};
serialport.write(xbeeAPI.buildFrame(frame_obj));
});

// The data frames are outputted by this function
xbeeAPI.on("frame_object", function(frame) {
console.log(">>", frame);
});

希望能帮到你

最佳答案

您可能应该删除 require 行末尾的 .SerialPort:

var SerialPort = require('serialport').SerialPort;

serial port documentation显示了它应该如何使用,并且它不包括 .SerialPort 最后:

var SerialPort = require('serialport');
var port = new SerialPort('/dev/ttyAMA0', {
baudRate: /dev/ttyAMA0
});

关于node.js - Node : SerialPort is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52541287/

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