Skip to content

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.

The Go SDK uses Go modules. Install it with go get:

Terminal window
go get github.com/prtmax/psdk-go
ModuleDescription
github.com/prtmax/psdk-go/frame/fatherCore commands and PSDK base interfaces
github.com/prtmax/psdk-go/frame/device/adapterBase device adapter interfaces
github.com/prtmax/psdk-go/frame/device/bluetoothBluetooth device management
github.com/prtmax/psdk-go/fruits/cpclCPCL commands
github.com/prtmax/psdk-go/fruits/escESC commands
github.com/prtmax/psdk-go/fruits/tsplTSPL commands
github.com/prtmax/psdk-go/fruits/posPOS commands
github.com/prtmax/psdk-go/fruits/wifiWi-Fi / Bluetooth configuration commands

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:

MethodDescription
Page / PageHeight / PageWidthPage dimensions
TextText
Bar1D barcode
QRCodeQR code
Line / Box / InverseGraphics and reverse printing
ImageImage commands
StatusQuery / SNQuery / ModelQuery / VersionQueryShort / BatteryVolumeQueryQuery commands
PrintPrint

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.

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:

Terminal window
go test ./frame/father/... ./frame/toolkit/... ./fruits/...