Rust SDK
Rust SDK 适用于需要类型安全、低运行时开销和跨平台命令构建能力的场景。当前 Rust 实现位于 prtmax/psdk 仓库的 rust/ workspace 中,SDK crate 已发布到 crates.io。
Rust SDK 使用 Cargo 管理依赖,可以按需添加对应 crate:
[dependencies]psdk-father = "0.0.1"psdk-fruit-cpcl = "0.0.1"psdk-fruit-tspl = "0.0.1"psdk-fruit-esc = "0.0.1"例如只使用 CPCL 指令时,通常添加核心写入流程和 CPCL 指令 crate 即可:
[dependencies]psdk-father = "0.0.1"psdk-fruit-cpcl = "0.0.1"可用 Crate
Section titled “可用 Crate”| Crate | 说明 |
|---|---|
psdk-father | 核心命令、设备、写入流程 |
psdk-toolkit | 工具函数 |
psdk-extension | 图片转换与扩展能力 |
psdk-frame-ota | OTA 数据包与升级流程 |
psdk-fruit-cpcl | CPCL 指令 |
psdk-fruit-tspl | TSPL 指令 |
psdk-fruit-esc | ESC 指令 |
psdk-fruit-rgb | RGB 系列指令 |
psdk-fruit-wifi | Wi-Fi / 蓝牙配置指令 |
psdk-fruit-emapi | EMAPI 协议指令 |
psdk-extension-export-c、psdk-extension-export-jni、psdk-compatible-hjinfo 属于 C/JNI/兼容层构建包,不作为普通 crates.io SDK 包发布。
下面示例使用内置的 FakeConnectedDevice 展示命令构建与写入流程。真实项目中需要实现 ConnectedDevice,把 write / read 接到蓝牙、USB、串口或网络连接。
use std::sync::{Arc, Mutex};
use psdk_father::{device::FakeConnectedDevice, psdk::Psdk};use psdk_fruit_cpcl::{args::CPrint, BaseCpcl};
fn main() -> Result<(), Box<dyn std::error::Error>> { let device = Arc::new(Mutex::new(FakeConnectedDevice::new())); let mut cpcl = BaseCpcl::with_connected_device(device);
let reporter = cpcl .page((400, 576))? .text((50, 50, "商品标签"))? .bar((50, 150, "1234567890", 2, 50))? .print(CPrint::default())? .write()?;
println!("write ok: {}, bytes: {}", reporter.ok, reporter.binary.len()); Ok(())}自定义设备需要实现 ConnectedDevice:
use psdk_father::{ device::{ConnectedDevice, ConnectionState, DeviceWriteOptions, ReadOptions}, errors::PsdkFatherResult,};
struct MyDevice;
impl ConnectedDevice for MyDevice { fn device_name(&self) -> String { "Printer".to_string() }
fn connection_state(&self) -> ConnectionState { ConnectionState::Connected }
fn disconnect(&self) -> PsdkFatherResult<()> { Ok(()) }
fn can_read(&self) -> bool { true }
fn write(&self, binary: &[u8]) -> PsdkFatherResult<()> { // Send bytes to printer transport. Ok(()) }
fn write_with_options( &self, binary: &[u8], options: DeviceWriteOptions, ) -> PsdkFatherResult<()> { self.write(binary) }
fn read(&self, _options: Option<ReadOptions>) -> PsdkFatherResult<Vec<u8>> { Ok(Vec::new()) }
fn flush(&self) -> PsdkFatherResult<()> { Ok(()) }}CPCL 用于便携式标签打印机。详细指令说明请参考 CPCL 指令文档。
use psdk_father::psdk::Psdk;use psdk_fruit_cpcl::{ args::{CPrint, CQRCode}, BaseCpcl,};
cpcl .page((400, 576))? .text((30, 40, "商品标签"))? .bar((30, 120, "6901234567890", 2, 60))? .qr_code((380, 80, "https://example.com", 5))? .print(CPrint::default())? .write()?;常用方法包括:
| 方法 | 说明 |
|---|---|
page / page_height / page_width | 页面尺寸 |
text | 文本 |
bar | 一维条码 |
qr_code | 二维码 |
line / box_ / inverse | 图形与反白 |
image / image_pixels / image_bytes | 图片指令 |
status / sn / model / version / battery_volume | 查询指令 |
print | 打印 |
TSPL 用于标签条码打印机。详细指令说明请参考 TSPL 指令文档。
use psdk_father::psdk::Psdk;use psdk_fruit_tspl::BaseTspl;
tspl .page((60, 40))? .gap(true)? .direction(0)? .cls()? .text((50, 50, 1, 1, false, "标签内容"))? .bar((30, 90, 300, 2))? .qr_code((320, 90, 4, "https://example.com", "M"))? .print(1)? .write()?;常用方法包括 page、gap、direction、cls、text、bar、line、box_、circle、ellipse、bar_code、qr_code、image、put_image、print、status、sn、version、battery_volume。
ESC 用于热敏小票和标签打印设备。详细指令说明请参考 ESC 指令文档。
use psdk_father::psdk::Psdk;use psdk_fruit_esc::BaseEsc;
esc .wakeup()? .enable()? .line_dot(16)? .stop_job()? .write()?;常用方法包括 wakeup、enable、image、image_bytes、position、stop_job、line_dot、paper_type、set_current_time、set_shutdown_time、state、sn、model、printer_version、battery_volume。
Feature
Section titled “Feature”psdk-fruit-cpcl、psdk-fruit-tspl、psdk-fruit-esc 默认启用 image-plus,可以直接使用图片 byte helper。需要最小依赖时可关闭默认 feature:
[dependencies]psdk-fruit-cpcl = { version = "0.0.1", default-features = false }- Rust 方法名遵循 Rust 命名习惯,例如
box_避开关键字,qr_code/bar_code使用 snake case。 write()会生成当前缓冲区指令并写入设备,成功后清空命令缓冲。rust/Cargo.lock当前不作为仓库内容提交,应用项目可自行管理自己的 lockfile。