Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to send native printer commands (escape codes) directly to theprinter ?

Reply
Thread Tools

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

 
 
Krist
Guest
Posts: n/a
 
      11-25-2009
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
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      11-25-2009
In article
<(E-Mail Removed)>,
Krist <(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
 
Martin Gregorie
Guest
Posts: n/a
 
      11-25-2009
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 |
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      11-25-2009

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
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      11-25-2009
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
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      11-25-2009
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-25-2009
On Wed, 25 Nov 2009 03:50:26 -0800 (PST), Krist <(E-Mail Removed)>
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
 
Reply With Quote
 
Krist
Guest
Posts: n/a
 
      12-01-2009
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
 
Reply With Quote
 
lightst4r lightst4r is offline
Junior Member
Join Date: Jul 2012
Posts: 1
 
      07-23-2012
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Better performance than native unix commands? Ma Sa Ruby 15 03-06-2008 05:32 PM
Use java to grab PDF file from SQL Server and send directly to printer? Ellie Java 1 07-20-2004 08:50 PM
Preview image directly on PC, save directly to HD Patrick M. Digital Photography 3 01-07-2004 08:29 PM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments