Search code examples
flutterdartprintingbluetoothtspl

Bluetooth Printer with TSPL commands


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.


Solution

  • 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.