Go SDK
The Go SDK supports servers, desktop tools, and cross-platform command construction. The Go implementation has moved from the main repository to prtmax/psdk-go.
Installation
Section titled “Installation”The Go SDK uses Go modules. Install it with go get:
go get github.com/prtmax/psdk-goAvailable modules
Section titled “Available modules”| Module | Description |
|---|---|
github.com/prtmax/psdk-go/frame/father | Core commands and PSDK base interfaces |
github.com/prtmax/psdk-go/frame/device/adapter | Base device adapter interfaces |
github.com/prtmax/psdk-go/frame/device/bluetooth | Bluetooth device management |
github.com/prtmax/psdk-go/fruits/cpcl | CPCL commands |
github.com/prtmax/psdk-go/fruits/esc | ESC commands |
github.com/prtmax/psdk-go/fruits/tspl | TSPL commands |
github.com/prtmax/psdk-go/fruits/pos | POS commands |
github.com/prtmax/psdk-go/fruits/wifi | Wi-Fi / Bluetooth configuration commands |
Quick start
Section titled “Quick start”This example uses a minimal mock device to demonstrate writing CPCL commands. In a real project, connect Write / Read to a Bluetooth, USB, serial, or network transport.
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 is used by portable label printers. See the CPCL command reference for details.
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()Common methods include:
| Method | Description |
|---|---|
Page / PageHeight / PageWidth | Page dimensions |
Text | Text |
Bar | 1D barcode |
QRCode | QR code |
Line / Box / Inverse | Graphics and reverse printing |
Image | Image commands |
StatusQuery / SNQuery / ModelQuery / VersionQueryShort / BatteryVolumeQuery | Query commands |
Print |
TSPL is used by label and barcode printers. See the TSPL command reference for details.
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()Common methods include Size, Gap, Direction, CLS, Text, Bar, Line, Box, Circle, BarCode, QRCode, Print, StatusQuery, SNQuery, VersionQueryShort, and BatteryVolumeQuery.
ESC is used by thermal receipt and label printers. See the ESC command reference for details.
printer := esc.Generic(device)
_, err := printer. Wakeup(). Enable(). LineDot(16). StopJob(). Write()Common methods include Wakeup, Enable, Image, Position, StopJob, LineDot, PaperType, SetCurrentTime, SetShutdownTime, State, SN, Model, PrinterVersion, and BatteryVolume.
Device connection
Section titled “Device connection”The Go SDK command layer depends on the ConnectedDevice interface:
type ConnectedDevice interface { Write(data []byte) (types.WriteReporter, error) Read() ([]byte, error) DeviceName() string ConnectionState() string}Bluetooth packages depend on platform capabilities. On Linux, go test ./... may fail because of build constraints in the Windows Bluetooth dependency. To validate the command layer, start with:
go test ./frame/father/... ./frame/toolkit/... ./fruits/...