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

K

Krist

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
 
M

Martin Gregorie

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.
 
R

RedGrittyBrick

Krist said:
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.
 
K

Knute Johnson

Krist said:
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.
 
K

Knute Johnson

Knute said:
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.
 
K

Krist

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
 
Joined
Jul 23, 2012
Messages
1
Reaction score
0
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top