npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@smart-cabinet-packages/rfid

v2.0.0

Published

智能柜 RFID 模块,提供RFID读写器控制功能

Readme

RFID设备通信库

这个库提供了与RFID读写器设备通信的高级API。支持通过串口和TCP连接与设备进行通信。

特点

  • 支持多种连接方式(串口、TCP)
  • 简单易用的API接口
  • 基于RxJS的响应式架构
  • 完整的TypeScript类型支持
  • 易于扩展的适配器架构

安装

pnpm add @smart-cabinet/rfid-new

快速开始

基本用法

使用通用工厂函数创建RFID设备实例:

import { createRfidDevice } from '@smart-cabinet/rfid-new'

async function main() {
  // 创建串口连接的RFID设备
  const rfidDevice = await createRfidDevice({
    type: 'serial',
    options: {
      path: '/dev/ttyS0',
      baudRate: 115200
    }
  })

  // 获取读写器信息
  const readerInfo = await rfidDevice.getReaderInfo()
  console.log('读写器信息:', readerInfo)

  // 清理资源
  rfidDevice.dispose()
}

main().catch(console.error)

特定连接类型

也可以使用专用工厂函数直接创建特定类型的连接:

import { createSerialRfidDevice, createTcpRfidDevice } from '@smart-cabinet/rfid-new'

// 串口连接
const serialDevice = await createSerialRfidDevice({
  path: '/dev/ttyS0',
  baudRate: 115200
})

// TCP连接
const tcpDevice = await createTcpRfidDevice({
  host: '192.168.1.100',
  port: 8080
})

读取标签数据

// 开始读取EPC标签
await rfidDevice.startReadEpcTags({
  antennaId: 1,
  power: 20,
  readTimes: 0, // 0表示持续读取,直到调用stopOperation
})

// 订阅标签数据
rfidDevice.epcTagDataUpload$.subscribe((tagData) => {
  console.log('读取到标签:', tagData)
})

// 订阅读取完成事件
rfidDevice.epcReadCompleted$.subscribe(() => {
  console.log('标签读取已完成')
})

// 3秒后停止读取
setTimeout(async () => {
  await rfidDevice.stopOperation()
}, 3000)

API参考

工厂函数

  • createRfidDevice(options): 根据连接类型创建RFID设备
  • createSerialRfidDevice(options): 创建使用串口连接的RFID设备
  • createTcpRfidDevice(options): 创建使用TCP连接的RFID设备

连接选项

串口连接选项

interface SerialConnectionOptions {
  path: string // 串口路径
  baudRate: number // 波特率
  dataBits?: 5 | 6 | 7 | 8 // 数据位
  stopBits?: 1 | 2 // 停止位
  parity?: 'none' | 'even' | 'odd' | 'mark' | 'space' // 校验位
  flowControl?: boolean // 流控制
  autoOpen?: boolean // 自动打开
}

TCP连接选项

interface TcpConnectionOptions {
  host: string // 主机地址
  port: number // 端口号
  timeout?: number // 连接超时时间(毫秒)
  keepAlive?: boolean // 是否保持连接
}

RfidDevice类

RfidDevice类提供与RFID读写器交互的高级API,主要方法包括:

  • 读写器管理:getReaderInfo(), getBasebandVersion(), getMacAddress()
  • RFID操作:startReadEpcTags(), stopOperation(), writeEpcTag()
  • 电源管理:configPower(), getPower()
  • 系统管理:rebootReader(), configSystemTime(), restoreDefaultConfig()

高级用法

自定义适配器

如果需要支持其他类型的连接方式,可以实现RfidCommunicationAdapter接口:

import type { RfidCommunicationAdapter } from '@smart-cabinet/rfid-new/adapters'
import { RfidDevice } from '@smart-cabinet/rfid-new'

class CustomAdapter implements RfidCommunicationAdapter {
  // 实现接口方法...
}

// 使用自定义适配器
const adapter = new CustomAdapter()
await adapter.connect()

const rfidDevice = new RfidDevice(
  adapter.getWriter(),
  adapter.getDataStream()
)

示例

更多示例可以在examples目录中找到:

  • basic-connection.ts: 基本连接示例
  • read-tags.ts: 读取标签示例
  • write-tags.ts: 写入标签示例

错误处理

库提供了专用的错误类型:

  • RfidDeviceError: 基础错误类型
  • RfidCommunicationError: 通信相关错误
  • RfidOperationError: 操作相关错误
try {
  await rfidDevice.startReadEpcTags(params)
}
catch (error) {
  if (error instanceof RfidCommunicationError) {
    console.error('通信错误:', error.message)
  }
  else if (error instanceof RfidOperationError) {
    console.error(`操作错误 (代码 ${error.errorCode}):`, error.message)
  }
}

许可证

MIT