2

I am using a thermal printer which kicks out the drawer before or after printing. But i want to stop that and i am sending a hex code to kickout the draw when needed. The sequence i am using is openDrawerCommand = "\u001B\u0070\u0030\u0042\u0045"; This is working fine in windows but if i run this on linux i am getting some error not in the application but from the printer driver saying that "There was a problem processing document 'Java Printing'". What is this all about ? I thing there is something to do with the code so tat it runs well on all platforms. The code i am using is shown below:

public OpenCashDrawer() {
    PrintService pservice = PrintServiceLookup.lookupDefaultPrintService();
    System.out.println("printservice"+pservice);
    try {
        DocPrintJob job = pservice.createPrintJob();
        String openDrawerCommand = ((char)0x1B70111)+"";
        //
        openDrawerCommand = "\u001B\u0070\u0030\u0042\u0045";
        byte by[] = openDrawerCommand.getBytes();
        //char[] printdata = "hello world\n".toCharArray();
        // System.out.println(by[1]);
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        //PrintService pservice = PrintServiceLookup.lookupDefaultPrintService();
        //DocPrintJob pjob = pservice.createPrintJob();
        //DocFlavor flavor = DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
        Doc doc = new SimpleDoc(by, flavor, null);
        job.print(doc, null);
    } catch (Exception e) {
        System.out.println("Whoa bro. The printer is balls. Check it:");
        e.printStackTrace();
    }
}
Deepak
  • 6,684
  • 18
  • 69
  • 121
  • Have your program log the bytes it sends to the printer, and see where they differ. It's possible the issue is in your printer drivers, or in the printer API you're using (`PrintServiceLookup`). – Gilles 'SO- stop being evil' Jun 03 '11 at 13:24
  • As asked, this is definitely a programming question (you need to read Java code and know the Java printing API to answer), so this is off-topic on Unix Stack Exchange. The question should be migrated to Stack Overflow shortly. – Gilles 'SO- stop being evil' Jun 03 '11 at 13:25
  • i am using CUPS driver. is there a way to trace this issue ? – Deepak Jun 03 '11 at 14:30
  • 3
    Did you setup the CUPS queue en RAW mode ? – PeterMmm Jun 03 '11 at 14:35
  • i dont know how to setup that. and when i look at error logs i am getting a error like "This document does not confirm to the Adobe Document Structuring Conventions and may not print correctly!" and lot more. – Deepak Jun 03 '11 at 14:43
  • do you want me to paste the error logs ? there is lot of lines for that one print job. – Deepak Jun 03 '11 at 14:44
  • 2
    "This document does not confirm to the Adobe Document ... that probably means the queue expect PS or PDF and you are sending some raw data. CUPS has a web interface on http: //localhost:631. Maybe you have to digg a little bit into CUPS setup. – PeterMmm Jun 03 '11 at 14:55
  • yes i am in that page and trying to find how to change to RAW mode. but no luck. u know any nice little tutorial where i can put my eyes in ? – Deepak Jun 03 '11 at 14:58
  • 1
    Maybe you can write directly into the printer by open a FileOutputStream on /dev/lp1 ? – PeterMmm Jun 03 '11 at 14:59
  • 1
    It is all here http://www.cups.org/ – PeterMmm Jun 03 '11 at 15:05
  • i am not using parallel port actually.. I am USB and serial.. – Deepak Jun 03 '11 at 15:07
  • 1
    If it is serial (even thru a USB adapter) then there is some /dev/tty* in your system. – PeterMmm Jun 03 '11 at 15:12
  • ok i will try tat but will RAW mode makes any difference ? – Deepak Jun 03 '11 at 15:17
  • 1
    Try openDrawerCommand = new String("\u001B\u0070\u0030\u0042\u0045","UTF-8"); byte by[] = openDrawerCommand.getBytes("UTF-8"); – Zecas May 24 '12 at 16:33
  • @Zecas Thanks for your response.. I asked this question 11 months ago and I got it solved already... cheers!! – Deepak May 24 '12 at 21:20
  • Cool! Can you please post an answer to the question yourself and then accept that answer so that we can close this question? Cheers. – Zecas May 24 '12 at 21:38
  • Shell commands are overkill. Just select the raw driver as previously mentioned. If the GUI hides this use CUPS web interface as suggested by @PeterMmm – tresf Sep 20 '16 at 07:16

1 Answers1

1

I got this working by the following code.

public OpenCashDrawer() {
    SwingWorker worker = new SwingWorker<String, Void>() {

        public String doInBackground() {
            try {
                String sql = "INSERT INTO pos_cashdraw SET user_id='"+global_variables.user_id+"', message='Opened For Sale'";
                mysql_query.update_mysql(variables.con.conn, sql);
                String out = "";
                do {
                    out = "";
                    //System.out.println("opened cashdraw");
                    //String openDrawerCommand = "\u001B\u0070\u0030\u0042\u0045";
                    String openDrawerCommand2 = "\u0017";
                    //String cmd = "echo -e " + openDrawerCommand2 + " > /dev/usblp0";
                    String cmd = "echo -e " + openDrawerCommand2 + " > /dev/ttyS0";
                    ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);

                    pb.redirectErrorStream(true);
                    if (pb != null) {
                        Process shell = pb.start();
                        InputStream shellin = shell.getInputStream();
                        int c;
                        while ((c = shellin.read()) != -1) {
                            System.out.write(c);
                            out += (char) c;
                        }
                        shellin.close();
                    }
                } while (out.contains("busy"));
            } catch (IOException ex) {
                System.err.println("Not supported in your operating system");
            }
            return "Opened";
        }
    };
    worker.execute();
}
Deepak
  • 6,684
  • 18
  • 69
  • 121