跳转到内容

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说明
psdk-father核心命令、设备、写入流程
psdk-toolkit工具函数
psdk-extension图片转换与扩展能力
psdk-frame-otaOTA 数据包与升级流程
psdk-fruit-cpclCPCL 指令
psdk-fruit-tsplTSPL 指令
psdk-fruit-escESC 指令
psdk-fruit-rgbRGB 系列指令
psdk-fruit-wifiWi-Fi / 蓝牙配置指令
psdk-fruit-emapiEMAPI 协议指令

psdk-extension-export-cpsdk-extension-export-jnipsdk-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()?;

常用方法包括 pagegapdirectionclstextbarlinebox_circleellipsebar_codeqr_codeimageput_imageprintstatussnversionbattery_volume

ESC 用于热敏小票和标签打印设备。详细指令说明请参考 ESC 指令文档

use psdk_father::psdk::Psdk;
use psdk_fruit_esc::BaseEsc;
esc
.wakeup()?
.enable()?
.line_dot(16)?
.stop_job()?
.write()?;

常用方法包括 wakeupenableimageimage_bytespositionstop_jobline_dotpaper_typeset_current_timeset_shutdown_timestatesnmodelprinter_versionbattery_volume

psdk-fruit-cpclpsdk-fruit-tsplpsdk-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。