1

We are going to develop an app that prints invoice from Bluetooth printer. But we have to send TSPL commands like;

    String bytes =
    "SIZE 3,2"
    "DIRECTION 1,0"
    "GAP 0,0\n"
    "REFERENCE 0,0"
    "OFFSET 0mm"
    "SET PEEL OFF"
    "SET CUTTER OFF"
    "SET PARTIAL_CUTTER OFF"
    "SET TEAR ON"
    "CLS"
    "TEXT 10,100, \"ROMAN.TTF\",0,1,1,\"        MALINCINSI      \""
    "TEXT 10,120, \"ROMAN.TTF\",0,1,1,\"        MALINCINSI      \""
    "TEXT 10,150, \"ROMAN.TTF\",0,1,1,\"     KDV: %18    \""
    "TEXT 10,200, \"ROMAN.TTF\",0,3,2,\"     12.79    \""
    "BARCODE 328,386,\"128M\",102,0,180,3,6,\"!10512345678\""
    "TEXT 328, 250, \"ROMAN.TTF\",0,1,1,\"12345678\""
    "PRINT 1,1"
    ;

I have used bluetooth_thermal_printer: ^0.0.6 and esc_pos_utils and I have sent that commands like this;

final result = await BluetoothThermalPrinter.writeText(bytes);

but bluetooth printer never print this commands in TSPL mode.

According to debug console result says true. But printer not printing anything. Is there any way or package to send and print TSPL commands from bluetooth printer.

  • First of all, you need to convert String to Uint8List by writing `var list = Uint8List.fromList(utf8.encode(bytes))` then send it to the thermal printer. Let me know if that works. – Zahid Tekbaş Mar 29 '23 at 07:30

1 Answers1

1

Luckily, I'm working on thermal printers nowadays and I have seen this question while looking for answers for my own questions. I'm using flutter_blue_plus and esc_pos_utils libraries. I'll share a code piece to help you send TSPL commands to thermal printer over Bluetooth connection.

printPriceChange(BluetoothDevice connectedDevice, double price){
    final gen = Generator(PaperSize.mm58, await CapabilityProfile.load());
    final printer = BluePrint();

// ...
// ... some codes to create tarih and fiyat variables
// ...

String bytes =
        "SIZE 40 mm, 20 mm\nGAP 2 mm\nSET CUTTER 1\nCLS\nCODEPAGE 857\nTEXT 25, 100, \"1\", 0, 1, 1, \"F.T.T. $tarih\"\nTEXT 25, 50, \"2\", 0, 1, 1, \"$fiyat\"\nPRINT 1\n";

    printer.add(gen.rawBytes(bytes.codeUnits));

    await printer.printData(connectedDevice);

}

To explain what I wrote:

  • tarih variable stands for DateTime.now().toString() but in short
  • fiyat variable stands for price, it is a double value with toStringAsFixed(2)
  • At the end I have created List to send bytes to thermal printer

connectedDevice variable is the device I have connected before executing this function.

Finally, I suggest you to search for "THERMAL BARCODE PRINTER Programming Manual" in Google to create your own TSPL codes for different scenarios.

Zahid Tekbaş
  • 809
  • 2
  • 12
  • 27
  • Thanks for showing blue plus now I can connect IOS devices with printer too – Alperen Aygün Mar 30 '23 at 08:07
  • Do you know how to encode Turkish characters in esc-pos utils package. I have tried codepage 857 and 1252 but it didn't work. https://stackoverflow.com/questions/75578567/flutter-esc-pos-utils-encode-turkish-charactersinvalid-character-problem – Alperen Aygün Mar 30 '23 at 08:38
  • @AlperenAygün trigger your thermal printer’s selftest. It will print codetables. For my printer, Turkish characters table’s number is 857 or 1254. – Zahid Tekbaş Mar 30 '23 at 09:41