escpos/printer

Functions for connecting to and communicating with ESC/POS printers.

Supports both USB (device file) and network (TCP socket) connections.

Example

// USB printer
let assert Ok(printer) = printer.device("/dev/usb/lp0")

// Network printer
let assert Ok(printer) = printer.connect("192.168.1.100", 9100)

escpos.new()
|> escpos.writeln("Hello!")
|> escpos.cut()
|> printer.print(printer)

// Close network printer socket
printer.disconnect(printer)

Types

pub opaque type CommandBuffer

A handle to a connected printer, either over USB or TCP.

pub opaque type Printer

Errors that can occur when connecting to or printing with a printer.

pub type PrinterError {
  ConnectionFailed(mug.ConnectError)
  DisconnectionFailed(mug.Error)
  TransmissionError(mug.Error)
  NetworkPrintError(mug.Error)
  UsbPrintError(simplifile.FileError)
  UsbDeviceError(simplifile.FileError)
}

Constructors

Values

pub fn append_to_buffer(
  buffer: CommandBuffer,
  data: BitArray,
) -> CommandBuffer
pub fn buffer_to_bits(buffer: CommandBuffer) -> BitArray

Returns the raw BitArray from CommandBuffer

pub fn connect(
  ip: String,
  port: Int,
) -> Result(Printer, PrinterError)

Connects to a network printer over TCP and sends the initialization command.

pub fn device(path: String) -> Result(Printer, PrinterError)

Opens a USB printer by its device file path (e.g. /dev/usb/lp0) and writes the initialization command.

pub fn disconnect(printer: Printer) -> Result(Nil, PrinterError)

Closes the connection to a network printer. For USB printers this is a no-op.

pub fn new_buffer() -> CommandBuffer
pub fn print(
  cb: CommandBuffer,
  printer: Printer,
) -> Result(Nil, PrinterError)

Sends a command buffer to the printer.

For network printers this writes to the TCP socket. For USB printers this writes directly to the device file.

Search Document