senddat

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2025 License: BSD-2-Clause Imports: 19 Imported by: 0

README

Open Send Data Tool for ESC/POS

This is an open-source extended implementation of the EPSON Send Data Tool (or, "senddat") for ESC/POS printers. It is designed to send raw data to printers using the ESC/POS command set.

It supports parsing *.dat files, and a subset of Senddat commands discovered in the documentation:

  • '// ... - comment
  • *N - delay N milliseconds
  • .text - output text and wait for "any key" press. In this implementation, it waits for the user to press Enter.
  • !text - output text without waiting for a key press.

Senddat commands are read till the end of line. Maximum line length is 250 chars.

Extensions

In addition to the standard set of senddat functions, this version is extended to support the following:

  • Go templating language preprocessor (template files up to 1MB)
  • File inclusion via @file.txt directive
  • TODO: Bitmap data inclusion via #image.png (must be monochrome)
Templating

Following functions are predefined:

  • Range "helpers":
    • count m n - returns an iterator that returns all numbers in range $[m..n]$
    • count_step m n s - self-explanatory
  • String functions:
    • strcat s1 s2 - concatenates strings "s1" and "s2"
    • strlen s - returns a length of a unicode string "s"

Installation

go install github.com/rusq/senddat/cmd/senddat@latest

Examples

Simple example
'// Initialise printer
    ESC "@"

'// Print Printer intitialised and wait for key press
.Printer initialised, press Enter to continue

`// Prints hello world on the printer.
    "Hello world!" LF

`// Wait for 2 seconds
*2000

`// Print some graphics
    ESC "*" 0 8 0

    0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08
ESC J interval tester for ESC/POS printer:
'// Line feed test, output a line in the ESC * mode and see if it connects
    ESC "@"
'// Unidirectional
    ESC "U" 1

'// open source senddat extension, ranges through counter from 15 to 16.
{{ range $n := count 15 16 }}
	"Spacing: {{$n}}" LF
	ESC "*" 0 5 0
	0xFF 0x00 0xFF 0x00 0xFF
	ESC "J" {{ $n }}

	ESC "*" 0 4 0
	0x00 0xFF 0x00 0xFF
	ESC "J" {{ $n }}

	LF
{{end}}

    ESC "@"
    ESC "E" 1 "END OF TEST" LF ESC "E" 0

For more examples, check the "examples" directory.

Motivation

This project was created to provide an open-source alternative to the EPSON Send Data Tool, which is not available for all platforms and has limited functionality. The goal is to provide a flexible and extensible tool for sending raw data to ESC/POS printers, with support for templating and other features that make it easier to work with printers in various environments.

Installation

go install github.com/rusq/senddat/cmd/senddat@latest
  • dotprint - A command line tool to render ESC/P files.
  • ESCParser - Another great command line tool to convert ESC/P files to postscript or pdf.
  • Send Data Tool - The original EPSON Send Data tool.

Senddat-OS is BSD licensed.

ESC/POS is a registered trademark of Seiko Epson Corporation.

The following examples in testdata/POS are taken from ESC/POS manual, and are (c) Seiko Epson Corp.:

  • label.dat
  • page_mode.dat
  • graphics.dat
  • receipt.dat

they are used for unit testing only.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSpec              = errors.New("no command spec found")
	ErrArgCountMismatch    = errors.New("argument count mismatch")
	ErrPayloadSizeMismatch = errors.New("payload size mismatch")
	ErrEmptyData           = errors.New("empty data entry")
	ErrInvalidCommand      = errors.New("invalid command")
)
View Source
var GenericCommandSpecs []CommandSpec
View Source
var (
	// SenddatOutput is the default senddat output stream
	SenddatOutput = os.Stderr
)

Functions

func Parse

func Parse(w io.Writer, r io.Reader) error

func ParseFromTemplate

func ParseFromTemplate(w io.Writer, r io.Reader) error

func ParseHexBytes added in v0.0.2

func ParseHexBytes(s string) ([]byte, error)

ParseHexBytes is an alternative parser for command prefixes, which expects a string of hex bytes separated by spaces, e.g. "1B 40" for ESC @. TODO: wire it to CLI flags.

func ParseString added in v0.0.2

func ParseString(s string) ([]byte, error)

ParseString parses the string, like 'ESC "@"' and returns bytes.

Types

type CommandSpec added in v0.0.2

type CommandSpec struct {
	Prefix   []byte
	Name     string
	ArgCount int
	ArgNames []string
	// Ignore indicates that this command should be ignored during processing.
	// Possibly, read to the next known command.
	Ignore bool
	// contains filtered or unexported fields
}

func LoadCommandSpecsWithSubcommands added in v0.0.2

func LoadCommandSpecsWithSubcommands(cmdCSV, subCSV string) ([]CommandSpec, error)

func (CommandSpec) ArgValues added in v0.0.2

func (cs CommandSpec) ArgValues(args []byte) (map[string]uint8, error)

ArgValues returns a map of argument names to their values based on the provided args.

func (CommandSpec) String added in v0.0.2

func (cs CommandSpec) String() string

type ControlCode

type ControlCode byte

ControlCode represents a control code used in the ESC/P, ESC/POS, and ESC/P2 protocols.

func (ControlCode) String

func (i ControlCode) String() string

type DecodeError added in v0.0.2

type DecodeError struct {
	Message string
	Offset  int
	Err     error
}

func (*DecodeError) Error added in v0.0.2

func (e *DecodeError) Error() string

func (*DecodeError) Unwrap added in v0.0.2

func (e *DecodeError) Unwrap() error

type Entry added in v0.0.2

type Entry struct {
	// Position in the input stream where the command or data starts.
	Offset int
	// Spec is the command specification.
	Spec *CommandSpec
	// Data is the raw bytes read from the stream. If the command is known,
	// it will be empty.
	Data []byte
	// Args is the optional arguments for commands that have them. i.e. for ESC
	// J n, will contain the value of n
	Args []byte
	// Payload is the optional payload data for commands that have it, like:
	// ESC * m nL nH data
	Payload []byte
}

Entry represents a single command entry in the PRN stream.

func Decode added in v0.0.2

func Decode(r io.Reader, spec []CommandSpec) ([]Entry, error)

Decode decodes a stream of PRN commands from the provided reader using the provided command specifications. It returns a slice of Command structs or an error if decoding fails.

func (Entry) IsCommand added in v0.0.2

func (e Entry) IsCommand() bool

func (Entry) IsData added in v0.0.2

func (e Entry) IsData() bool

func (Entry) IsEmpty added in v0.0.2

func (e Entry) IsEmpty() bool

func (Entry) IsValid added in v0.0.2

func (e Entry) IsValid() bool

func (Entry) Name added in v0.0.2

func (e Entry) Name() string

func (Entry) String added in v0.0.2

func (e Entry) String() string

func (Entry) Validate added in v0.0.2

func (e Entry) Validate() error

type Interpreter added in v0.0.2

type Interpreter struct {
	// contains filtered or unexported fields
}

func NewInterpreter added in v0.0.2

func NewInterpreter(r io.Reader, spec []CommandSpec) (*Interpreter, error)

func (*Interpreter) Next added in v0.0.2

func (p *Interpreter) Next(ignoreUnknown bool) (*Entry, error)

Next reads the next command or raw data entry from the input stream. It returns an Entry containing the command specification, arguments, and payload if applicable, or raw data if no command is recognized. If ignoreUnknown is true, it will skip unknown commands and continue reading.

func (*Interpreter) ReadByte added in v0.0.2

func (p *Interpreter) ReadByte() (byte, error)

func (*Interpreter) UnreadByte added in v0.0.2

func (p *Interpreter) UnreadByte() error

type ParseFunc added in v0.0.2

type ParseFunc func(s string) ([]byte, error)
var ParseFn ParseFunc = ParseString

ParseFn is the default parsing function for commands. There are two currently to choose from:

  1. ParseString - parses expressions like `ESC "@"'
  2. parseHexBytes - parses hex bytes, i.e. `1B 40'

type SubCommands added in v0.0.2

type SubCommands map[string]Subcommand

type Subcommand added in v0.0.2

type Subcommand struct {
	Prefix   string
	Cn       byte
	Fn       byte
	Argcount string
}

Directories

Path Synopsis
cmd
senddat command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL