2.0.0 • Published 2 months ago

@smart-cabinet-packages/rfid v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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

1.0.2

8 months ago

1.0.1

8 months ago

1.0.9

6 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.11

6 months ago

1.0.10

6 months ago

1.0.15

3 months ago

1.0.14

3 months ago

1.0.13

4 months ago

2.0.0

2 months ago

1.0.12

4 months ago

1.0.0

9 months ago