andrey-ushakov / esc_pos_printer

ESC/POS (thermal, receipt) printing for Flutter & Dart
BSD 3-Clause "New" or "Revised" License
351 stars 300 forks source link

Windows Release Printing Bug #147

Open DroneMesh opened 2 years ago

DroneMesh commented 2 years ago

Hi Everyone,

Info To reproduce Problem. Windows 7 , 10, 11 Ethernet and Wifi Flutter Version 2.8 Stable RELEASE MODE Compile

The problem is network printing sometimes it prints the whole receipt and sometimes it just stops in random places. This was also reproduced with the default examples and my custom functions. Tried multiple windows devices all have the same issue in release Mode. IOS , MacOS, and Android all work perfectly the issue is windows related only.

Meanwhile, I will be going over the source of the library and see if I can spot the issue. However, if anyone has any valuable information that would be great.

Will keep you updated.

Thank you for your time.

RizaldiWiratama commented 2 years ago

Hey @DroneMesh i appear to have same issue with you, and then when i change the code to use generator. from esc_pos_utils (instead using printer. every command) and then after that call printer.rawBytes(bytes) like this example

List<int> bytes = [];
bytes += generator.text("Test", styles: const PosStyles(align: PosAlign.center), linesAfter: 2);
bytes += generator.text("Test 2", styles: const PosStyles(align: PosAlign.center), linesAfter: 2);
bytes += generator.cut();
printer.rawBytes(bytes);

there is no random stop again after using this workaround

hugorosario commented 2 years ago

This also happens in debug mode, not just in release. I believe it is something to do with the disconnection of the underlying IOSink implementation for windows. If I just add a small delay of 100ms before disconnecting, everything seems to be printing fine.

  void testTicket() async {
    const PaperSize paper = PaperSize.mm80;
    final profile = await CapabilityProfile.load();
    final printer = NetworkPrinter(paper, profile);

    final PosPrintResult res =
        await printer.connect('192.168.0.151', port: 9100);

    if (res == PosPrintResult.success) {
      testReceipt(printer);
      //add a small delay before disconnecting
      await Future.delayed(const Duration(milliseconds: 100));
      printer.disconnect();
    }
  }
victorshx commented 2 years ago

@hugorosario

It's because data sent to the socket is not flushed before destroying the socket.

printer.disconnect();

You can look at the implementation of printer.disconnect().

ztalha6 commented 1 year ago

This also happens in debug mode, not just in release. I believe it is something to do with the disconnection of the underlying IOSink implementation for windows. If I just add a small delay of 100ms before disconnecting, everything seems to be printing fine.

  void testTicket() async {
    const PaperSize paper = PaperSize.mm80;
    final profile = await CapabilityProfile.load();
    final printer = NetworkPrinter(paper, profile);

    final PosPrintResult res =
        await printer.connect('192.168.0.151', port: 9100);

    if (res == PosPrintResult.success) {
      testReceipt(printer);
      //add a small delay before disconnecting
      await Future.delayed(const Duration(milliseconds: 100));
      printer.disconnect();
    }
  }

This fixes my problem too on windows.