GardenMobile & interfaces

Flutter receipt printing and an ESC/POS cash drawer

An older network-printer example, raw drawer commands, and a small printer-library extension.

Printing a receipt and opening a cash drawer are separate commands, even when both travel through the same network printer. This earlier Flutter experiment used esc_pos_printer to send text and ESC/POS bytes.

import 'package:esc_pos_printer/esc_pos_printer.dart';

Printer.connect('192.168.0.123', port: 9100).then((printer) {
    printer.println('Regular: aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ');
    printer.println('Special 1: àÀ èÈ éÉ ûÛ üÜ çÇ ôÔ',
        styles: PosStyles(codeTable: PosCodeTable.westEur));
    printer.println('Special 2: blåbærgrød',
        styles: PosStyles(codeTable: PosCodeTable.westEur));

    printer.println('Bold text', styles: PosStyles(bold: true));
    printer.println('Reverse text', styles: PosStyles(reverse: true));
    printer.println('Underlined text',
        styles: PosStyles(underline: true), linesAfter: 1);

    printer.println('Align left', styles: PosStyles(align: PosTextAlign.left));
    printer.println('Align center',
        styles: PosStyles(align: PosTextAlign.center));
    printer.println('Align right',
        styles: PosStyles(align: PosTextAlign.right), linesAfter: 1);
    
    printer.println('Text size 200%',
        styles: PosStyles(
          height: PosTextSize.size2,
          width: PosTextSize.size2,
        ));

    printer.cut();
    printer.disconnect();
  });

The address and port identify the example printer. Text styles and code tables depend on the device and library generation.

Read the drawer command

The original hardware reference described the command fields as follows:

The Epson ESC POS Drawer Kick cash drawer command enables the control of either 1 or 2 Cash drawers

ESC p m t1 t2

Where

ESC p is the drawer kick command

m is the DK connector pin to output control signals ( 0 for Pin2/drawer1  or 1 for Pin5/drawer2 )

t1 is the ON time of the drawer kick signal. The ON time is 2 mS x t1 value.

t2 is the OFF time of the drawer kick signal ( time before the next ON signal can be received ) The OFF time is 2 mS x t2 value.
ESC p m t1 t2

Its decimal example and Flutter byte sequence were:

MSComm1.Output = ESC + p + 0 + 25 + 250

MSComm1.Output = Chr(27) + Chr(112) + Chr(0) + Chr(25) + Chr(250)

Kick drawer 1 with the following 50mS on / 500mS off pulse
// nhancv: Flutter kick drawer 1 with the following 50mS on / 500mS off pulse
printer.sendRaw([27, 112, 0, 25, 250]);

The note described a 50 ms on / 500 ms off pulse for that example. Connector pinouts, voltage, lock position, and supported pulse behavior must match the actual drawer and printer; a similar-looking cable is not a compatibility check.

The earlier library extension

The source linked an owner fork adding QR-code printing and drawer opening:

  esc_pos_printer:
    git:
      url: git://github.com/nhancv/esc_pos_printer.git
Printer.connect('192.168.0.123', port: 9100).then((printer) async {
    await printer.printQRCode('nhancv.com');
    printer.println('');
    printer.cut();
    printer.openCashDrawer();
    printer.disconnect();
  });

The unpinned Git dependency and API calls are historical. No printer or drawer was connected or activated for this revision.

← Back to the garden