escpos

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Interval = time.Second / 2
	ErrDelay = 5 * time.Second
	Timeout  = time.Second
	Wait     = 100 * time.Millisecond

	ReadTimeout  = 50 * time.Millisecond
	WriteTimeout = 50 * time.Millisecond

	Baudrate = 9600
	DataBits = 8
)
View Source
const (
	NUL = 0
	SOH = 1
	STX = 2
	ETX = 3
	EOT = 4
	ENQ = 5
	ACK = 6
	BEL = 7
	BS  = 8
	HT  = 9
	LF  = 10
	VT  = 11
	FF  = 12
	CR  = 13
	SO  = 14
	SI  = 15

	DLE = 16
	DC1 = 17
	DC2 = 18
	DC3 = 19
	DC4 = 20
	NAK = 21
	SYN = 22
	ETB = 23
	CAN = 24
	EM  = 25
	SUB = 26
	ESC = 27
	FS  = 28
	GS  = 29
	RS  = 30
	US  = 31

	SP  = ' '
	DQ  = '"'
	SQ  = '\''
	DEL = 127

	XON  = DC1
	XOFF = DC3
)
View Source
const (
	MODEL_MASK  = 0b1001_0000
	MODEL_FIXED = 0b0000_0000

	TYPE_MASK       = 0b1001_0000
	TYPE_FIXED      = 0b0000_0000
	TYPE_MULTI_BYTE = 0b0000_0001
	TYPE_AUTOCUTTER = 0b0000_0010
	TYPE_DISPLAY    = 0b0000_0100
)
View Source
const (
	NoParity   = Parity(serial.NoParity)
	OddParity  = Parity(serial.OddParity)
	EvenParity = Parity(serial.EvenParity)
)
View Source
const (
	PRN_MASK     = 0b1001_0011
	PRN_FIXED    = 0b0001_0010
	PRN_DRAWER   = 0b0000_0100
	PRN_OFFLINE  = 0b0000_1000
	PRN_RECOVERY = 0b0010_0000
	PRN_FEED_BTN = 0b0100_0000

	OFF_MASK  = 0b1001_0011
	OFF_FIXED = 0b0001_0010
	OFF_COVER = 0b0000_0100
	OFF_FED   = 0b0000_1000
	OFF_PAPER = 0b0010_0000
	OFF_ERROR = 0b0100_0000

	ERR_MASK         = 0b1001_0011
	ERR_FIXED        = 0b0001_0010
	ERR_RECOVER      = 0b0000_0100
	ERR_AUTOCUTTER   = 0b0000_1000
	ERR_UNRECOVER    = 0b0010_0000
	ERR_AUTO_RECOVER = 0b0100_0000

	ROLL_MASK     = 0b1001_0011
	ROLL_FIXED    = 0b0001_0010
	ROLL_NEAR_END = 0b0000_1100
	ROLL_NO_PAPER = 0b0110_0000

	PAPER_MASK     = 0b1001_0000
	PAPER_FIXED    = 0b0000_0000
	PAPER_NEAR_END = 0b0000_0011
	PAPER_END      = 0b0000_1100

	DRAWER_MASK  = 0b1001_0000
	DRAWER_FIXED = 0b0000_0000
	DRAWER_PIN_3 = 0b0000_0001
)
View Source
const (
	CHUNK = 40
)
View Source
const (
	INFO_HEADER = '_'
)

Variables

View Source
var (
	ErrorLogFunc func(string, ...interface{})
	InfoLogFunc  func(string, ...interface{})
	DebugLogFunc func(string, ...interface{})
)
View Source
var (
	DisconnectErr         = errors.New("Printer disconnected")
	PrintingInProgressErr = errors.New("Printing in progress")
)

Functions

func Parse

func Parse(data []byte) (out []byte, err error)

Types

type ClearAndRecoverCmd

type ClearAndRecoverCmd struct{}

func (ClearAndRecoverCmd) String

func (c ClearAndRecoverCmd) String() string

type Cmd

type Cmd interface {
	String() string
	// contains filtered or unexported methods
}

type CmdEvent

type CmdEvent struct {
	Time time.Time
	Cmd  Cmd
	Res  Res
}

func (CmdEvent) String

func (e CmdEvent) String() string

type CmdRes

type CmdRes struct {
	Err error
}

func (CmdRes) Error

func (r CmdRes) Error() error

func (CmdRes) Res

func (r CmdRes) Res() any

func (CmdRes) String

func (r CmdRes) String() string

type ConnectionEvent

type ConnectionEvent struct {
	Time      time.Time
	Connected bool
}

func (ConnectionEvent) String

func (e ConnectionEvent) String() string

type Controller

type Controller struct {
	Dev    Dev
	Config *ControllerConfig
	// contains filtered or unexported fields
}

func (*Controller) ClearAndRecover

func (c *Controller) ClearAndRecover() error

Recover from recoverable error after clearing the receive and print buffers in real-time.

func (*Controller) Close

func (c *Controller) Close()

func (*Controller) DisablePulseLevel

func (c *Controller) DisablePulseLevel() error

Disable both real-time pulse command (DLE DC4 1 or 2).

func (*Controller) DrawerStatus

func (c *Controller) DrawerStatus() (*DrawerStatus, error)

func (*Controller) EnablePulseLevel

func (c *Controller) EnablePulseLevel() error

Enable both real-time pulse command (DLE DC4 1 or 2).

func (*Controller) ErrorStatus

func (c *Controller) ErrorStatus() (*ErrorStatus, error)

Get real-time error status.

func (*Controller) Firmware

func (c *Controller) Firmware() (Info, error)

func (*Controller) IsClosed

func (c *Controller) IsClosed() bool

func (*Controller) Maker

func (c *Controller) Maker() (Info, error)

func (*Controller) Model

func (c *Controller) Model() (Info, error)

func (*Controller) ModelID

func (c *Controller) ModelID() (*ModelID, error)

func (*Controller) OfflineStatus

func (c *Controller) OfflineStatus() (*OfflineStatus, error)

Get real-time offline status.

func (*Controller) PaperStatus

func (c *Controller) PaperStatus() (*PaperStatus, error)

func (*Controller) PressFeedButton

func (c *Controller) PressFeedButton() error

Press Feed Button in real-time. Note: support depend on printer type.

func (*Controller) PrinterStatus

func (c *Controller) PrinterStatus() (*PrinterStatus, error)

Get real-time printer status.

func (*Controller) PulseDrawerPin2

func (c *Controller) PulseDrawerPin2(tOn, tOff byte) error

Pulse the pin 2 of drawer connector for (tOn * 2ms) ON and (tOff * 2ms) OFF. Most printer force tOff >= tOn and some has minimal tOn and/or tOff. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level1

func (c *Controller) PulseDrawerPin2Level1() error

Pulse the pin 2 of drawer connector for 100ms ON and 100ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level2

func (c *Controller) PulseDrawerPin2Level2() error

Pulse the pin 2 of drawer connector for 200ms ON and 200ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level3

func (c *Controller) PulseDrawerPin2Level3() error

Pulse the pin 2 of drawer connector for 300ms ON and 300ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level4

func (c *Controller) PulseDrawerPin2Level4() error

Pulse the pin 2 of drawer connector for 400ms ON and 400ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level5

func (c *Controller) PulseDrawerPin2Level5() error

Pulse the pin 2 of drawer connector for 500ms ON and 500ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level6

func (c *Controller) PulseDrawerPin2Level6() error

Pulse the pin 2 of drawer connector for 600ms ON and 600ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level7

func (c *Controller) PulseDrawerPin2Level7() error

Pulse the pin 2 of drawer connector for 700ms ON and 700ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin2Level8

func (c *Controller) PulseDrawerPin2Level8() error

Pulse the pin 2 of drawer connector for 800ms ON and 800ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5

func (c *Controller) PulseDrawerPin5(tOn, tOff byte) error

Pulse the pin 5 of drawer connector for (tOn * 2ms) ON and (tOff * 2ms) OFF. Most printer force tOff >= tOn and some has minimal tOn and/or tOff. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level1

func (c *Controller) PulseDrawerPin5Level1() error

Pulse the pin 5 of drawer connector for 100ms ON and 100ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level2

func (c *Controller) PulseDrawerPin5Level2() error

Pulse the pin 5 of drawer connector for 200ms ON and 200ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level3

func (c *Controller) PulseDrawerPin5Level3() error

Pulse the pin 5 of drawer connector for 300ms ON and 300ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level4

func (c *Controller) PulseDrawerPin5Level4() error

Pulse the pin 5 of drawer connector for 400ms ON and 400ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level5

func (c *Controller) PulseDrawerPin5Level5() error

Pulse the pin 5 of drawer connector for 500ms ON and 500ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level6

func (c *Controller) PulseDrawerPin5Level6() error

Pulse the pin 5 of drawer connector for 600ms ON and 600ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level7

func (c *Controller) PulseDrawerPin5Level7() error

Pulse the pin 5 of drawer connector for 700ms ON and 700ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) PulseDrawerPin5Level8

func (c *Controller) PulseDrawerPin5Level8() error

Pulse the pin 5 of drawer connector for 800ms ON and 800ms OFF in real-time. From Epson docs it's not possible to pulse both pin at the same time.

func (*Controller) Recover

func (c *Controller) Recover() error

Recover from recoverable error and restart printing from the line where the error occured in real-time. Note: support depend on printer type.

func (*Controller) Reset

func (c *Controller) Reset() error

func (*Controller) RollStatus

func (c *Controller) RollStatus() (*RollStatus, error)

Get real-time paper roll status.

func (*Controller) Send

func (c *Controller) Send(b []byte) error

func (*Controller) SerialNo

func (c *Controller) SerialNo() (Info, error)

func (*Controller) TypeID

func (c *Controller) TypeID() (*TypeID, error)

type ControllerConfig

type ControllerConfig struct {
	Timeout time.Duration
	Wait    time.Duration
}

func DefaultControllerConfig

func DefaultControllerConfig() *ControllerConfig

type Dev

type Dev interface {
	UseXonXoff() bool
	Open() (io.ReadWriteCloser, error)
}

type DevConfig

type DevConfig struct {
	ReadTimeout  time.Duration // This is per byte timeout
	WriteTimeout time.Duration // This is per byte timeout
}

func DefaultDevConfig

func DefaultDevConfig() *DevConfig

type DrawerEvent

type DrawerEvent struct {
	Time time.Time
	On   bool
}

func (DrawerEvent) String

func (e DrawerEvent) String() string

type DrawerStatus

type DrawerStatus byte

func (DrawerStatus) IsPin3

func (s DrawerStatus) IsPin3() bool

func (DrawerStatus) IsValid

func (s DrawerStatus) IsValid() bool

func (DrawerStatus) String

func (s DrawerStatus) String() string

type ErrorStatus

type ErrorStatus byte

func (ErrorStatus) IsAutoRecoverable

func (s ErrorStatus) IsAutoRecoverable() bool

func (ErrorStatus) IsAutocutter

func (s ErrorStatus) IsAutocutter() bool

func (ErrorStatus) IsRecoverable

func (s ErrorStatus) IsRecoverable() bool

func (ErrorStatus) IsUnrecoverable

func (s ErrorStatus) IsUnrecoverable() bool

func (ErrorStatus) IsValid

func (s ErrorStatus) IsValid() bool

func (ErrorStatus) String

func (s ErrorStatus) String() (str string)

type Event

type Event interface {
	String() string
	// contains filtered or unexported methods
}

type FeedButtonEvent

type FeedButtonEvent struct {
	Time time.Time
	On   bool
}

func (FeedButtonEvent) String

func (e FeedButtonEvent) String() string

type FileDev

type FileDev struct {
	Device string
	Config *DevConfig
}

func (*FileDev) Open

func (d *FileDev) Open() (io.ReadWriteCloser, error)

func (*FileDev) UseXonXoff

func (d *FileDev) UseXonXoff() bool

type FinishPrintEvent

type FinishPrintEvent struct {
	Time  time.Time
	Print PrintCmd
	Ok    bool
}

func (FinishPrintEvent) String

func (e FinishPrintEvent) String() string

type FirmwareCmd

type FirmwareCmd struct{}

func (FirmwareCmd) Result

func (c FirmwareCmd) Result(r Res) string

func (FirmwareCmd) String

func (c FirmwareCmd) String() string

type Info

type Info []byte

func MakeInfo

func MakeInfo(b []byte) Info

func (Info) IsValid

func (i Info) IsValid() bool

func (Info) String

func (i Info) String() string

type InfoRes

type InfoRes struct {
	Info string
	Err  error
}

func (InfoRes) Error

func (r InfoRes) Error() error

func (InfoRes) Res

func (r InfoRes) Res() any

func (InfoRes) String

func (r InfoRes) String() string

type MakerCmd

type MakerCmd struct{}

func (MakerCmd) Result

func (c MakerCmd) Result(r Res) string

func (MakerCmd) String

func (c MakerCmd) String() string

type ModelCmd

type ModelCmd struct{}

func (ModelCmd) Result

func (c ModelCmd) Result(r Res) string

func (ModelCmd) String

func (c ModelCmd) String() string

type ModelID

type ModelID byte

func (ModelID) IsValid

func (id ModelID) IsValid() bool

func (ModelID) String

func (id ModelID) String() string

type ModelIDCmd

type ModelIDCmd struct{}

func (ModelIDCmd) Result

func (c ModelIDCmd) Result(r Res) *ModelID

func (ModelIDCmd) String

func (c ModelIDCmd) String() string

type ModelIDRes

type ModelIDRes struct {
	ID  *ModelID
	Err error
}

func (ModelIDRes) Error

func (r ModelIDRes) Error() error

func (ModelIDRes) Res

func (r ModelIDRes) Res() any

func (ModelIDRes) String

func (r ModelIDRes) String() string

type OfflineStatus

type OfflineStatus byte

func (OfflineStatus) IsCoverOpen

func (s OfflineStatus) IsCoverOpen() bool

func (OfflineStatus) IsError

func (s OfflineStatus) IsError() bool

func (OfflineStatus) IsFedByButton

func (s OfflineStatus) IsFedByButton() bool

Is paper being fed by the paper feed button

func (OfflineStatus) IsPaperEnd

func (s OfflineStatus) IsPaperEnd() bool

Is printing stops due to paper-end

func (OfflineStatus) IsValid

func (s OfflineStatus) IsValid() bool

func (OfflineStatus) String

func (s OfflineStatus) String() (str string)

type PaperNearEndEvent

type PaperNearEndEvent struct {
	Time time.Time
	On   bool
}

func (PaperNearEndEvent) String

func (e PaperNearEndEvent) String() string

type PaperStatus

type PaperStatus byte

func (PaperStatus) IsNearEnd

func (s PaperStatus) IsNearEnd() bool

func (PaperStatus) IsNoPaper

func (s PaperStatus) IsNoPaper() bool

func (PaperStatus) IsValid

func (s PaperStatus) IsValid() bool

func (PaperStatus) String

func (s PaperStatus) String() (str string)

type Parity

type Parity serial.Parity

func (Parity) IsValid

func (p Parity) IsValid() bool

func (Parity) MarshalText

func (p Parity) MarshalText() ([]byte, error)

func (Parity) String

func (p Parity) String() string

func (*Parity) UnmarshalText

func (p *Parity) UnmarshalText(b []byte) error

type ParseError

type ParseError struct {
	LineNo int
	Column int
	State  int
	Line   string
}

func (ParseError) Error

func (e ParseError) Error() string

type PressFeedButtonCmd

type PressFeedButtonCmd struct{}

func (PressFeedButtonCmd) String

func (c PressFeedButtonCmd) String() string

type PrintCmd

type PrintCmd []byte

func (PrintCmd) NeedDisablePulseLevel

func (c PrintCmd) NeedDisablePulseLevel() bool

func (PrintCmd) String

func (c PrintCmd) String() string

type PrinterStatus

type PrinterStatus byte

func (PrinterStatus) IsDrawerPin3

func (s PrinterStatus) IsDrawerPin3() bool

func (PrinterStatus) IsFeedButton

func (s PrinterStatus) IsFeedButton() bool

Is paper feed button being pressed

func (PrinterStatus) IsOffline

func (s PrinterStatus) IsOffline() bool

func (PrinterStatus) IsRecovery

func (s PrinterStatus) IsRecovery() bool

Is the printer waiting for online recovery?

func (PrinterStatus) IsValid

func (s PrinterStatus) IsValid() bool

func (PrinterStatus) String

func (s PrinterStatus) String() (str string)

type PulseCmd

type PulseCmd struct {
	Pin int8
	On  byte
	Off byte
}

func (PulseCmd) String

func (c PulseCmd) String() string

func (PulseCmd) Validate

func (c PulseCmd) Validate() error

type PulseLevelCmd

type PulseLevelCmd struct {
	Pin   int8
	Level int8
}

func (PulseLevelCmd) String

func (c PulseLevelCmd) String() string

func (PulseLevelCmd) Validate

func (c PulseLevelCmd) Validate() error

type RawCmd

type RawCmd []byte

func (RawCmd) String

func (c RawCmd) String() string

type RecoverCmd

type RecoverCmd struct{}

func (RecoverCmd) String

func (c RecoverCmd) String() string

type Res

type Res interface {
	Error() error
	Res() any
	String() string
}

type RollStatus

type RollStatus byte

func (RollStatus) IsNearEnd

func (s RollStatus) IsNearEnd() bool

func (RollStatus) IsNoPaper

func (s RollStatus) IsNoPaper() bool

func (RollStatus) IsValid

func (s RollStatus) IsValid() bool

func (RollStatus) String

func (s RollStatus) String() (str string)

type Scanner

type Scanner struct {
	Controller *Controller
	Config     *ScannerConfig
	// contains filtered or unexported fields
}

func (*Scanner) Scan

func (s *Scanner) Scan(cmdCh <-chan []Cmd) <-chan []Event

type ScannerConfig

type ScannerConfig struct {
	Interval time.Duration
	ErrDelay time.Duration
}

func DefaultScannerConfig

func DefaultScannerConfig() *ScannerConfig

type SerialConfig

type SerialConfig struct {
	UseXonXoff bool
	Baudrate   int
	DataBits   int
	Parity     Parity
	DevConfig
}

func DefaultSerialConfig

func DefaultSerialConfig() *SerialConfig

type SerialDev

type SerialDev struct {
	Device string
	Config *SerialConfig
}

func (*SerialDev) Open

func (d *SerialDev) Open() (io.ReadWriteCloser, error)

func (*SerialDev) UseXonXoff

func (d *SerialDev) UseXonXoff() bool

type SerialNoCmd

type SerialNoCmd struct{}

func (SerialNoCmd) Result

func (c SerialNoCmd) Result(r Res) string

func (SerialNoCmd) String

func (c SerialNoCmd) String() string

type StartPrintEvent

type StartPrintEvent struct {
	Time  time.Time
	Print PrintCmd
}

func (StartPrintEvent) String

func (e StartPrintEvent) String() string

type State

type State byte
const (
	OnlineState State = iota
	FedByButtonState
	WaitRecoveryState
	OfflineState
	RecoverableErrState
	AutoRecoverErrState
	UnrecoverableErrState
	AutocutterErrState
	NoPaperState
	CoverOpenState
)

func (State) IsValid

func (s State) IsValid() bool

func (State) MarshalText

func (s State) MarshalText() ([]byte, error)

func (State) String

func (s State) String() string

func (*State) UnmarshalText

func (s *State) UnmarshalText(b []byte) error

type StateEvent

type StateEvent struct {
	Time  time.Time
	State State
}

func (StateEvent) String

func (e StateEvent) String() string

type TypeID

type TypeID byte

func (TypeID) HasAutocutter

func (id TypeID) HasAutocutter() bool

func (TypeID) HasDisplay

func (id TypeID) HasDisplay() bool

func (TypeID) HasMultiByte

func (id TypeID) HasMultiByte() bool

func (TypeID) IsValid

func (id TypeID) IsValid() bool

func (TypeID) MarshalText

func (id TypeID) MarshalText() ([]byte, error)

func (TypeID) String

func (id TypeID) String() (str string)

func (*TypeID) UnmarshalText

func (id *TypeID) UnmarshalText(b []byte) error

type TypeIDCmd

type TypeIDCmd struct{}

func (TypeIDCmd) Result

func (c TypeIDCmd) Result(r Res) *TypeID

func (TypeIDCmd) String

func (c TypeIDCmd) String() string

type TypeIDRes

type TypeIDRes struct {
	ID  *TypeID
	Err error
}

func (TypeIDRes) Error

func (r TypeIDRes) Error() error

func (TypeIDRes) Res

func (r TypeIDRes) Res() any

func (TypeIDRes) String

func (r TypeIDRes) String() string

Jump to

Keyboard shortcuts

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