ESC/POS receipt printing, drawer kick and printer status
README.md

escpos #

Receipts, cash drawers and status on thermal receipt printers, in Epson's
ESC/POS command language, which most receipt printers implement.

A point of sale prints its receipts and closure reports on a receipt printer
and opens its cash drawer through the printer's drawer kick-out connector. A
receipt here is a list of commands; the encoder writes it as the ESC/POS bytes
a printer reads on its raw TCP port 9100 or its USB printer class device, and
the same receipt encodes as ePOS-Print XML for Epson's HTTP service. The
printer's real-time status reports its paper, its cover, its errors and the
drawer's switch.

Contents #

Installation #

$ opam install escpos

The library escpos (module Escpos) encodes and decodes and does no I/O.
The library escpos.eio (module Escpos_eio) connects to a printer over TCP,
USB or ePOS-Print. The command escpos prints a receipt written as JSON.

Usage #

open Escpos

let kick = Result.get_ok (Pulse.v ~on_ms:100 ~off_ms:100 ())

let receipt =
  Command.
    [
      Initialise;
      Code_page Pc858;
      Align Center;
      Bold true;
      Text "Caf\u{00E9}\n";
      Bold false;
      Align Left;
      Text "Espresso   \u{20AC}2.10\n";
      Cut { cut = Partial; feed = Some 0 };
      Pulse kick;
    ]

let hex = String.iter (fun c -> Fmt.pr "%02X " (Char.code c))

encode writes the bytes: ESC @, ESC t 19, ESC a 1, ESC E 1, the text, and
so on to GS V 66 0 and ESC p 0 50 50:

# encode receipt |> Result.iter hex;;
1B 40 1B 74 13 1B 61 01 1B 45 01 43 61 66 82 0A 1B 45 00 1B 61 00 45 73 70 72 65 73 73 6F 20 20 20 D5 32 2E 31 30 0A 1D 56 42 00 1B 70 00 32 32
- : unit = ()

Text is UTF-8 and is written in the character code table the last
Code_page selected: é is byte 0x82 and € byte 0xD5 in PC858. Text a table
cannot print is refused, and so is a control character, which a printer would
read as a command:

# encode [ Text "\u{20AC}" ] |> Result.iter_error (Fmt.pr "%a@." pp_error);;
pc437 has no glyph for U+20AC
- : unit = ()

The real-time status is one byte the printer answers to DLE EOT:

# Status.request Paper;;
- : string = "\016\004\004"
# Status.decode Paper '\x1e' |> Result.iter (Fmt.pr "%a@." Status.pp);;
roll paper sensor:
  paper near end: yes
  paper end: no
- : unit = ()

Escpos_eio sends a receipt and reads the status on a connection; given
Eio_main.run's environment, this prints the receipt on the network printer
at 192.168.1.50 and reports its paper:

let print_and_check env =
  Eio.Switch.run @@ fun sw ->
  let printer = Escpos_eio.tcp ~sw (Eio.Stdenv.net env) "192.168.1.50" in
  match Escpos_eio.print printer receipt with
  | Error e -> Fmt.epr "%a@." pp_error e
  | Ok () -> (
      match Escpos_eio.status ~clock:(Eio.Stdenv.clock env) printer Paper with
      | Ok s -> Fmt.pr "%a@." Status.pp s
      | Error e -> Fmt.epr "%a@." Escpos_eio.pp_error e)

The command line #

A receipt is a JSON array of commands, each an object whose member type
names it:

$ cat receipt.json
[{"type": "initialise"},
 {"type": "code_page", "code_page": "pc858"},
 {"type": "align", "align": "center"},
 {"type": "text", "text": "Café\n"},
 {"type": "qr", "data": "https://example.com/r/42", "level": "m"},
 {"type": "cut", "cut": "partial", "feed": 0}]
$ escpos print --tcp 192.168.1.50 receipt.json
$ escpos print --usb /dev/usb/lp0 receipt.json
$ escpos print --epos 'http://192.168.1.60/cgi-bin/epos/service.cgi?devid=local_printer&timeout=10000' receipt.json
$ escpos kick --tcp 192.168.1.50 --pin 2 --on 100 --off 100
$ escpos status --tcp 192.168.1.50 --kind paper
roll paper sensor:
  paper near end: no
  paper end: no
$ escpos encode receipt.json > receipt.bin
$ escpos epos receipt.json > receipt.xml

escpos --help lists every command's members.

What it covers #

  • ESC @, ESC d, ESC a, ESC M, ESC E, ESC -, GS B and GS ! for the text's
    modes; ESC t for the character code tables PC437, PC850, PC852, PC858,
    PC860, PC863, PC865, PC866 and WPC1252.
  • GS v 0 for a monochrome raster image, which a binary portable bitmap (P4)
    gives as is.
  • GS k function B for UPC-A, UPC-E, EAN13, EAN8, CODE39, ITF, CODABAR,
    CODE93 and CODE128, with GS h, GS w and GS H; their data is checked
    against what each symbology encodes, and a given EAN or UPC-A check digit
    against the one the data computes.
  • GS ( k for QR Code models 1 and 2 and Micro QR Code.
  • GS V for full and partial cuts, with or without feeding to the cutting
    position.
  • ESC p for the drawer kick, pin 2 or pin 5, with its on and off times.
  • DLE EOT for the printer, offline cause, error cause and roll paper sensor
    statuses.
  • ePOS-Print XML: the text, feed, image, barcode, symbol, cut and pulse
    elements in a SOAP envelope, and the response with its ASB status. A
    command ePOS-Print cannot express, a two-dot underline or a pulse time it
    has no value for, is refused.

Which pin of the drawer kick-out connector a drawer is wired to, and which
level of pin 3 means its drawer is open, are the drawer's wiring.

References #

  • Epson, ESC/POS Command Reference for TM printers.
  • Epson, ePOS-Print XML User's Manual.
  • Netpbm, pbm: the binary portable bitmap format.

License #

ISC. See LICENSE.md.