2.0.0 • Published 2 months ago
@smart-cabinet-packages/rfid v2.0.0
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