lsongdev / node-escpos

🖨️ ESC/POS Printer driver for Node.js
https://npmjs.org/escpos
Other
1.35k stars 420 forks source link

Can't close device with a pending request #85

Closed bahrul-rasyid closed 6 years ago

bahrul-rasyid commented 6 years ago

my print function :

print() {
    const device = new escpos.USB(0x04B8, 0x0202);
    const printer = new escpos.Printer(device);
    device.open(function () {
      printer
      .font('a')
      .align('ct')
      .text('Hello World')
      .cut()
      .close(() => {
        console.log('close');
      });
    });
  }

close's callback never called.

.close(() => {
       console.log('close');
});

The first time calling this function, the printer works, and next calling throw this error

Error: Can't close device with a pending request
    at Device.usb.Device.close (E:\workspace\tester\electron\escpos\dist\node_modules\usb\usb.js:43:7)
    at USB.close (E:\workspace\tester\electron\escpos\dist\node_modules\escpos\adapter\usb.js:127:15)
    at OutEndpoint. (E:\workspace\tester\electron\escpos\dist\node_modules\escpos\printer.js:454:18)
    at callback (E:\workspace\tester\electron\escpos\dist\node_modules\usb\usb.js:362:14)
    at E:\workspace\tester\electron\escpos\dist\node_modules\usb\usb.js:368:33
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

Thanks

Psychopoulet commented 6 years ago

if you don't send endofline with "println" function, try some "flush" function, like

  .cut()
  .flush()
  .close(() => {
    console.log('close');
  });
bahrul-rasyid commented 6 years ago

I think it's because flush() was called twice from cut() and close(). So I modified this at printer.js, then it works

Printer.prototype.cut = function(part, feed){
  this.feed(feed || 3);
  this.buffer.write(_.PAPER[
    part ? 'PAPER_PART_CUT' : 'PAPER_FULL_CUT'
  ]);
  // return this.flush();
  return this;
};