【Iot】前端串口serialport.js串口通信库快速入门(附经验总结)

公司项目需要开发一个windows客户端,提供串口modbusRTU数据读取、处理、显示和控制功能。
项目使用了electron 31.0.2版本,串口通信库使用了serialport 12.0.0版本。

serialport.js

一、serialport简介

serialport.js 是一个用于 Node.js 的串口通信库,允许开发者通过 JavaScript 与串口设备(如 Arduino、传感器、GPS 模块等)进行通信。它提供了一个简单且强大的 API,用于打开、配置、读取和写入串口数据。serialport.js 是开源的,基于 MIT 许可证,广泛应用于物联网(IoT)、嵌入式系统和硬件开发项目中。

重点,serialport.js必须运行与Node.js环境,在浏览器环境是无法使用的,所以一般结合electron.js使用,serialport.js在electron的主进程中调用并使用。

1.1 安装

可以通过 npm 安装 serialport.js(cnpm、pnpm、yarn都是一样的)
现在默认安装的应该是12.0.0版本

npm i serialport

1.2 基本用法

import {
   
    SerialPort } from 'serialport';

// 打开串口
const port = new SerialPort('/dev/ttyUSB0', {
   
   
  baudRate: 9600, // 设置波特率
  dataBits: 8,    // 数据位
  parity: 'none', // 校验位
  stopBits: 1,    // 停止位
  autoOpen: false // 不自动打开
});

// 打开串口时触发
port.open((err) => {
   
   
  if (err) {
   
   
    return console.error('Error opening port:', err.message);
  }
  console.log('Port opened successfully');
});

// 监听数据事件
port.on('data', (data) => {
   
   
  console.log('Received data:', data.toString());
});

// 监听错误事件
port.on('error', (err) => {
   
   
  console.error('Error:', err.message);
});

// 关闭串口
setTimeout(() => {
   
   
  port.close((err) => {
   
   
    if (err) {
   
   
      return console.error('Error closing port:', err.message);
    }
    console.log
node-serialport 是一个 Node.js 的包,用来对串口数据进行读写操作。基本示例代码:var SerialPort = require("serialport").SerialPort var serialPort = new SerialPort("/dev/tty-usbserial1", {   baudrate: 57600 }, false); // this is the openImmediately flag [default is true] serialPort.open(function (error) {   if ( error ) {     console.log('failed to open: ' error);   } else {     console.log('open');     serialPort.on('data', function(data) {       console.log('data received: '   data);     });     serialPort.write("ls\n", function(err, results) {       console.log('err '   err);       console.log('results '   results);     });   } });罗列所有串口:var serialPort = require("serialport"); serialPort.list(function (err, ports) {   ports.forEach(function(port) {     console.log(port.comName);     console.log(port.pnpId);     console.log(port.manufacturer);   }); });串口配置:baudRatedataBitsstopBitsparityrtsctsxonxoffxanyflowControlbufferSizeparserencodingdataCallbackdisconnectedCallbackplatformOptions - sets platform specific options, see below.目前已有很多项目在使用这个包进行串口处理:Johnny-Five - Firmata based Arduino Framework.Cylon.js - JavaScript Robotics, By Your Command.node-l8smartlight (source) A node library to control the L8 Smartlight via Bluetooth or USB portfirmata Talk natively to Arduino using the firmata protocol.tmpad source - a DIY midi pad using infrared, arduino, and nodejs. Videoduino - A higher level framework for working with Arduinos in node.js.Arduino Drinking Game Extravaganza - AKA "The Russian" a hexidecimal drinking game for geeks by Uxebu presented at JSConf EU 2011.Arduino controlling popcorn.js - Controlling a popcorn.js video with an Arduino kit.Robotic JavaScript - The first live presentation of the node-serialport code set as presented at JSConf EU 2010.devicestack - This module helps you to represent a device and its protocol.reflecta A communication protocol that combines Arduino Libraries and NodeJS into an integrated system.rc4pt-node - Control Popcorntime with an
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卸载引擎

如果对您有帮助,请赏个饭吃

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值