You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Johnny-Five与Arduino串口通信:板载LED闪烁报错求助

解决Johnny-Five中Serialport Bindings文件缺失的问题

嘿,这个错误我刚入门Johnny-Five的时候也踩过坑!本质是Johnny-Five依赖的serialport模块需要编译原生组件,而你的环境里要么没装构建工具,要么依赖安装过程出了问题。下面是我亲测有效的解决步骤:

1. 彻底清理依赖,重新安装

有时候旧的依赖缓存或lock文件会导致版本不兼容,先清掉重来:

rm -rf node_modules package-lock.json
npm install johnny-five

2. 安装必要的构建工具

serialport是原生模块,需要编译环境才能正常安装,不同系统的安装命令如下:

  • Ubuntu/Debian:
    sudo apt-get install build-essential libudev-dev
    
  • macOS:
    先安装Xcode命令行工具:
    xcode-select --install
    
    再安装依赖库(需要Homebrew):
    brew install libusb
    
  • Windows:
    安装Windows构建工具(需要管理员权限打开命令提示符):
    npm install --global windows-build-tools
    

装完构建工具后,再重新安装serialport:

npm install serialport

3. 检查Arduino的串口权限与连接

  • 确保Arduino已经正确连接到电脑,没有被其他程序占用(比如Arduino IDE的串口监视器要关掉)
  • Linux用户需要给当前用户串口访问权限:
    sudo usermod -aG dialout $USER
    
    执行完后注销再重新登录,权限才会生效。
  • 可以通过命令确认串口设备:Linux用ls /dev/tty*找/dev/ttyACM0或/dev/ttyUSB0;macOS用ls /dev/tty.usb*;Windows看设备管理器的“端口”选项。

4. 尝试指定兼容的Serialport版本

如果最新版serialport和Johnny-Five有兼容问题,可以试试稳定的旧版本,比如我之前常用的9.x版本:

npm install serialport@9.2.8

最后附上正确的LED闪烁测试代码

确保你的代码没有问题,这是标准的引脚13闪烁示例:

const five = require("johnny-five");
const board = new five.Board();

board.on("ready", function() {
  // 初始化引脚13的LED
  const led = new five.Led(13);
  // 每500毫秒闪烁一次
  led.blink(500);
});

运行代码前记得插好Arduino,并且关闭所有占用串口的程序哦!

内容的提问来源于stack exchange,提问作者Pedro Juan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:19:37