How to send native printer commands (escape codes) directly to theprinter ?

Discussion in 'Java' started by Krist, Nov 25, 2009.

  1. Krist

    Krist Guest

    Hi all,

    How can I do it in Java , access printer port and send native printer
    commands (escape codes) directly to the printer ?

    Thank you very much,
    Krist
     
    Krist, Nov 25, 2009
    #1
    1. Advertising

  2. Re: How to send native printer commands (escape codes) directly to the printer ?

    In article
    <>,
    Krist <> wrote:

    > How can I do it in Java , access printer port and send native printer
    > commands (escape codes) directly to the printer ?


    You might look at RxTx: <http://rxtx.qbang.org/wiki/index.php/FAQ>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Nov 25, 2009
    #2
    1. Advertising

  3. Re: How to send native printer commands (escape codes) directly totheprinter ?

    On Wed, 25 Nov 2009 03:50:26 -0800, Krist wrote:

    > Hi all,
    >
    > How can I do it in Java , access printer port and send native printer
    > commands (escape codes) directly to the printer ?
    >

    (a) Use a socket connection to send the escape codes to a tiny C
    program that copies bytes from the socket to the parallel port.

    (b) Use a socket to talk directly to a network capable (IPP) printer.

    (c) Similar to (b) but talk to an IPP-aware print spooler, e.g. CUPS.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Nov 25, 2009
    #3
  4. Re: How to send native printer commands (escape codes) directly tothe printer ?

    Krist wrote:
    > Hi all,
    >
    > How can I do it in Java , access printer port and send native printer
    > commands (escape codes) directly to the printer ?


    1)

    It is possible to break this problem into two parts:

    You can use Java to write a "text" file that contains text with
    printer-specific escape codes (I guess you mean HP's PCL, Epson's ESC/P
    or similar).

    You can use Java to execute an operating-system dependent command, such
    as "copy /b filename.pcl lpt1:" or "copy /b filename.pcl
    \\server\printer" using something like
    "Runtime.getRuntime().exec(commands);"

    Clumsy but practical.


    --
    RGB
     
    RedGrittyBrick, Nov 25, 2009
    #4
  5. Re: How to send native printer commands (escape codes) directly tothe printer ?

    Krist wrote:
    > Hi all,
    >
    > How can I do it in Java , access printer port and send native printer
    > commands (escape codes) directly to the printer ?
    >
    > Thank you very much,
    > Krist


    You should be able to open a stream to the printer port and write your
    commands. I don't have a parallel printer to try it with but I try a
    test with the serial port and get back to you.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Nov 25, 2009
    #5
  6. Re: How to send native printer commands (escape codes) directly tothe printer ?

    Knute Johnson wrote:
    > Krist wrote:
    >> Hi all,
    >>
    >> How can I do it in Java , access printer port and send native printer
    >> commands (escape codes) directly to the printer ?
    >>
    >> Thank you very much,
    >> Krist

    >
    > You should be able to open a stream to the printer port and write your
    > commands. I don't have a parallel printer to try it with but I try a
    > test with the serial port and get back to you.
    >


    It works fine with the serial port on Windows XP so it should work with
    the parallel printer port as well. I think Linux will also work the same.

    import java.io.*;

    public class test {
    public static void main(String[] args) throws Exception {
    FileWriter fw = new FileWriter("COM2");
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("Hello World!\r\n");
    bw.close();
    }
    }

    I connected my COM1 to my COM2 through a null modem and read the data
    with HyperTerminal. To configure the port settings you need to use the
    MODE command. I believe that Linux has a similar command but you won't
    need this for the parallel port as there is nothing to configure.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Nov 25, 2009
    #6
  7. Krist

    Roedy Green Guest

    Re: How to send native printer commands (escape codes) directly to the printer ?

    On Wed, 25 Nov 2009 03:50:26 -0800 (PST), Krist <>
    wrote, quoted or indirectly quoted someone who said :

    >How can I do it in Java , access printer port and send native printer
    >commands (escape codes) directly to the printer ?


    see http://mindprod.com/jgloss/printing.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I mean the word proof not in the sense of the lawyers, who set two half proofs equal to a whole one, but in the sense of a mathematician, where half proof = 0, and it is demanded for proof that every doubt becomes impossible.
    ~ Carl Friedrich Gauss
     
    Roedy Green, Nov 25, 2009
    #7
  8. Krist

    Krist Guest

    Re: How to send native printer commands (escape codes) directly tothe printer ?

    Hi All,
    Thanks all for all your kind advice.

    After many test, I found that easiest is using the codes below can do
    what I need to do :

    public void print() {
    PrintService ps =
    PrintServiceLookup.lookupDefaultPrintService();
    try {
    String line;
    String s ="";

    int iCHR27Val = 27;
    char cCHR27 = (char)iCHR27Val;
    int iCHR4Val = 4;
    char cCHR4 = (char)iCHR4Val;
    s += cCHR27 + "c0" + cCHR4 ;
    s += "\n"+"\n"+"\n"+"\n"+"\n";
    s += " PRINTTING FROM JAPPLET PAGE : 1 v4" +
    "\n";
    s += " This is FIRST LINE " + "\n";
    s += " This is SECOND LINE " + "\n";
    s += " This is THIRD LINE " + "\n";
    s += " This is ** LAST ** LINE " + "\f";

    // now print!!
    if (1 > 0) {
    // PrintService ps =
    PrintServiceLookup.lookupDefaultPrintService();
    if (ps!=null) {
    //JOptionPane.showMessageDialog(null,"selected
    printer " +ps.getName());
    //System.out.println("selected printer " +
    ps.getName());

    PrintRequestAttributeSet aset= new
    HashPrintRequestAttributeSet();
    DocFlavor flavor =
    DocFlavor.INPUT_STREAM.AUTOSENSE;
    //DocFlavor flavor =
    DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    aset.add(new MediaPrintableArea
    (100,400,210,160,Size2DSyntax.MM));
    DocPrintJob pj = ps.createPrintJob();
    try {

    ByteArrayInputStream b =new
    ByteArrayInputStream(s.getBytes());

    Doc doc = new SimpleDoc(b, flavor, null);
    pj.print(doc, aset);

    JOptionPane.showMessageDialog(null,"end
    printing");
    } catch (PrintException e) {
    JOptionPane.showMessageDialog
    (null,e.getMessage());


    } catch(Exception e1) {
    JOptionPane.showMessageDialog
    (null,e1.getMessage());
    }

    } else {

    JOptionPane.showMessageDialog(null,"no
    Printer");
    }
    }

    } catch (Exception e) {
    } finally {
    if (br != null)
    try { br.close(); } catch (IOException ignore) {}
    if (in != null)
    try { in.close(); } catch (IOException ignore) {}
    }

    }

    Thank you, if you have further advise, pls welcome..

    best regards,
    Krist
     
    Krist, Dec 1, 2009
    #8
  9. Krist

    lightst4r

    Joined:
    Jul 23, 2012
    Messages:
    1
    Hi Kris,

    Please help me. When i try your source code, why printer just print:
    unrecoverable error: rangecheck in setpagedevice
    Operand stack .........................


    How i can print text or string like your code? Thanks before Kris.
     
    lightst4r, Jul 23, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ellie
    Replies:
    1
    Views:
    531
    Andrew Thompson
    Jul 20, 2004
  2. Martin Vogler
    Replies:
    4
    Views:
    524
    Martin Vogler
    May 2, 2005
  3. Karlheinz Klingbeil

    [reportlab] print directly to printer

    Karlheinz Klingbeil, Dec 18, 2003, in forum: Python
    Replies:
    1
    Views:
    413
    Derrick 'dman' Hudson
    Dec 18, 2003
  4. pavan
    Replies:
    3
    Views:
    8,687
    venkat.sadasivam
    Nov 5, 2008
  5. Replies:
    2
    Views:
    2,892
    Malcolm
    Aug 20, 2005
Loading...

Share This Page