anastaciocintra / escpos-coffee

Java library for ESC/POS printer
https://anastaciocintra.github.io/escpos-coffee
MIT License
277 stars 72 forks source link

it only prints the first time i execute the code #54

Open palanganero opened 3 years ago

palanganero commented 3 years ago

i have to power off and power on again to do a new print.

palanganero commented 3 years ago

once i do the escpos.close(); it does not print anymore.

anastaciocintra commented 3 years ago

Hi @palanganero Yes, PrinterOutputStream send data directing to the printer. The instance cannot be reused and the last command should be close(), after that, you need to create another instance to send data to the printer..

one simple way to code it is:

EscPos escpos = new EscPos(new PrinterOutputStream(printService));
// send data to the printer
escpos.close();

// again!
escpos = new EscPos(new PrinterOutputStream(printService));
// send data to the printer
escpos.close();

// and again!
escpos = new EscPos(new PrinterOutputStream(printService));
// send data to the printer
escpos.close();
palanganero commented 3 years ago

with this code it only prints "hola holita":

 PrintService printService = PrinterOutputStream.getPrintServiceByName("xpos58");
  PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
  EscPos escpos = new EscPos(printerOutputStream);
  escpos.writeLF("Hello Wold");
  //escpos.feed(5);
  escpos.close();

 escpos = new EscPos(new PrinterOutputStream(printService));
escpos.writeLF("Hola holita");
escpos.close();

escpos = new EscPos(new PrinterOutputStream(printService));
escpos.writeLF("Hola que tal");
escpos.close();
palanganero commented 3 years ago

the main problem i have is that when i print a ticket (after doing escpos.close()) i have to reboot the printer. It happens to me only in linux in usb mode (in ethernet mode works fine). In windows works fine. What can i try?

anastaciocintra commented 3 years ago

wow! have multiple configurations. we need to divide the problems by distinct implementations of OutputStream and distinct OS.

are you using implementations from the lib? try to make one table like this, and we can try to solve one by one ...

Platform OutputStream result
Windows PrinterOutputStream Not Ok
Windows TcpIpOutputStream Ok
Windows UsbStream Not Ok
Ubuntu PrinterOutputStream Ok
Ubuntu TcpIpOutputStream Not Ok
Ubuntu UsbStream Not Ok
palanganero commented 3 years ago

i have ubuntu with PrinterOutputStream. it should be ok.

anastaciocintra commented 3 years ago

@palanganero I runned your code, using windows and PrinterOutputStream and really, it doens't print correctly all lines.

I don't know why. but if you write a line with feed and cut. it work fine..

        escpos.writeLF("Hello Wold");
        escpos.feed(3).cut(EscPos.CutMode.FULL);
        escpos.close();

        escpos = new EscPos(new PrinterOutputStream(printService));
        escpos.writeLF("Hola holita");
        escpos.feed(3).cut(EscPos.CutMode.FULL);
        escpos.close();

        escpos = new EscPos(new PrinterOutputStream(printService));
        escpos.writeLF("Hola que tal");
        escpos.feed(7).cut(EscPos.CutMode.FULL);
        escpos.close();
palanganero commented 3 years ago

my problem is that only prints the first time i execute the program. If i run the program again it doesn't print. If i unplug the usb cable and plug it again it prints again. But only one time. I tryed with several drivers and keeps the same. It seems to be a usb problem because if i connect the printer by ethernet it doesn't have problem. When i reboot the system it prints again.

anastaciocintra commented 3 years ago

Maybe some kind of driver configuration But the problem is happening on linux only?

palanganero commented 3 years ago

yes only in linux and by ethernet is not problem.

anastaciocintra commented 3 years ago

Tomorrow I will test on my ubuntu

anastaciocintra commented 3 years ago

hi @palanganero

My environment is ubuntu/openjdk 11 with generic Text Only printer installed.

uname --all
Linux marcoubuntu 5.3.0-42-generic #34-Ubuntu SMP Fri Feb 28 05:49:40 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu219.10)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu219.10, mixed mode, sharing)

and, really, the code print somethig unexpected...

but if you print with one readline (enter keyboard) between print blocks it will be ok (I don't know why)

another thing is that I'm sending four lines with feed and cut (like a complete receipt) and not only one line..

                Scanner keyboard = new Scanner(System.in);
                PrintService printService = PrinterOutputStream.getPrintServiceByName("TM-T20");
                escpos = new EscPos(new PrinterOutputStream(printService));
                escpos.writeLF("Hello Wold");
                escpos.writeLF("Hello Wold");
                escpos.writeLF("Hello Wold");
                escpos.writeLF("Hello Wold");
                escpos.feed(5).cut(EscPos.CutMode.FULL);
                escpos.close();

                keyboard.nextLine();

                escpos = new EscPos(new PrinterOutputStream(printService));
                escpos.writeLF("2Hello Wold");
                escpos.writeLF("2Hello Wold");
                escpos.writeLF("2Hello Wold");
                escpos.writeLF("2Hello Wold");
                escpos.feed(5).cut(EscPos.CutMode.FULL);
                escpos.close();

                keyboard.nextLine();

                escpos = new EscPos(new PrinterOutputStream(printService));
                escpos.writeLF("3Hello Wold");
                escpos.writeLF("3Hello Wold");
                escpos.writeLF("3Hello Wold");
                escpos.writeLF("3Hello Wold");
                escpos.feed(5).cut(EscPos.CutMode.FULL);
                escpos.close();
//                keyboard.nextLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

On real world app, like server print or swing app it works well see https://github.com/anastaciocintra/escpos-coffee-samples/blob/master/miscellaneous/server-print/src/main/java/ServerPrint.java

see you

palanganero commented 3 years ago

ok, thank you, it should be some kind of incompatibility with the drivers.