Go SDK
Go SDK 适用于 Go 服务端、桌面工具和跨平台命令构建场景。Go 实现已经从主仓库拆出到独立仓库 prtmax/psdk-go。
Go SDK 使用 Go module 管理依赖,可以直接通过 go get 获取:
go get github.com/prtmax/psdk-go| 模块 | 说明 |
|---|---|
github.com/prtmax/psdk-go/frame/father | 核心命令与 PSDK 基础接口 |
github.com/prtmax/psdk-go/frame/device/adapter | 设备适配器基础接口 |
github.com/prtmax/psdk-go/frame/device/bluetooth | 蓝牙设备管理 |
github.com/prtmax/psdk-go/fruits/cpcl | CPCL 指令 |
github.com/prtmax/psdk-go/fruits/esc | ESC 指令 |
github.com/prtmax/psdk-go/fruits/tspl | TSPL 指令 |
github.com/prtmax/psdk-go/fruits/pos | POS 指令 |
github.com/prtmax/psdk-go/fruits/wifi | Wi-Fi / 蓝牙配置指令 |
下面示例使用一个最小 mock 设备展示 CPCL 命令写入。真实项目中把 Write / Read 接到蓝牙、USB、串口或网络连接即可。
package main
import ( "log"
adapterTypes "github.com/prtmax/psdk-go/frame/device/adapter/types" "github.com/prtmax/psdk-go/fruits/cpcl" cpclArgs "github.com/prtmax/psdk-go/fruits/cpcl/args" cpclTypes "github.com/prtmax/psdk-go/fruits/cpcl/types")
type Device struct{}
func (d *Device) Write(data []byte) (adapterTypes.WriteReporter, error) { return adapterTypes.WriteReporter{ BytesWritten: len(data), Success: true, }, nil}
func (d *Device) Read() ([]byte, error) { return nil, nil}
func (d *Device) DeviceName() string { return "Printer"}
func (d *Device) ConnectionState() string { return "CONNECTED"}
func main() { printer := cpcl.Generic(&Device{})
reporter, err := printer. Page(cpclArgs.NewCPage().SetSize(576, 400)). Text(cpclArgs.NewCText(). SetPosition(50, 50). SetFont(cpclTypes.TSS24). SetContent("商品标签")). Bar(cpclArgs.NewCBar(). SetPosition(50, 150). SetCodeType(cpclTypes.CODE128). SetLineWidth(2). SetHeight(50). SetContent("1234567890")). Print(). Write()
if err != nil { log.Fatal(err) }
log.Printf("write ok: %v, bytes: %d", reporter.Success, reporter.BytesWritten)}CPCL 用于便携式标签打印机。详细指令说明请参考 CPCL 指令文档。
printer := cpcl.Generic(device)
_, err := printer. Page(cpclArgs.NewCPage().SetSize(576, 400)). Bold(true). Text(cpclArgs.NewCText(). SetPosition(30, 40). SetFont(cpclTypes.TSS24). SetContent("商品标签")). Bold(false). Bar(cpclArgs.NewCBar(). SetPosition(30, 120). SetCodeType(cpclTypes.CODE128). SetLineWidth(2). SetHeight(60). SetContent("6901234567890")). QRCode(cpclArgs.NewCQRCode(). SetPosition(380, 80). SetWidth(5). SetCorrectLevel(cpclTypes.CorrectLevelM). SetContent("https://example.com")). Print(). Write()常用方法包括:
| 方法 | 说明 |
|---|---|
Page / PageHeight / PageWidth | 页面尺寸 |
Text | 文本 |
Bar | 一维条码 |
QRCode | 二维码 |
Line / Box / Inverse | 图形与反白 |
Image | 图片指令 |
StatusQuery / SNQuery / ModelQuery / VersionQueryShort / BatteryVolumeQuery | 查询指令 |
Print | 打印 |
TSPL 用于标签条码打印机。详细指令说明请参考 TSPL 指令文档。
printer := tspl.Generic(device)
_, err := printer. Size(tsplArgs.NewTSize().SetSize(60, 40)). Gap(tsplArgs.NewTGap().SetGap(3, 0)). Direction(tsplArgs.NewTDirection().SetDirection(0)). CLS(). Text(tsplArgs.NewTText(). SetPosition(50, 50). SetFont(tsplTypes.TSS24). SetContent("标签内容")). Bar(tsplArgs.NewTBar().SetBar(30, 90, 300, 2)). QRCode(tsplArgs.NewTQRCode(). SetPosition(320, 90). SetCellWidth(4). SetECCLevel("M"). SetContent("https://example.com")). Print(tsplArgs.NewTPrint().SetCopies(1)). Write()常用方法包括 Size、Gap、Direction、CLS、Text、Bar、Line、Box、Circle、BarCode、QRCode、Print、StatusQuery、SNQuery、VersionQueryShort、BatteryVolumeQuery。
ESC 用于热敏小票和标签打印设备。详细指令说明请参考 ESC 指令文档。
printer := esc.Generic(device)
_, err := printer. Wakeup(). Enable(). LineDot(16). StopJob(). Write()常用方法包括 Wakeup、Enable、Image、Position、StopJob、LineDot、PaperType、SetCurrentTime、SetShutdownTime、State、SN、Model、PrinterVersion、BatteryVolume。
Go SDK 的命令层依赖 ConnectedDevice 接口:
type ConnectedDevice interface { Write(data []byte) (types.WriteReporter, error) Read() ([]byte, error) DeviceName() string ConnectionState() string}蓝牙相关包依赖平台能力。Linux 环境下直接 go test ./... 可能因为 Windows 蓝牙依赖的 build constraints 失败;验证命令层时可以优先运行:
go test ./frame/father/... ./frame/toolkit/... ./fruits/...