View Single Post
  #3 (permalink)  
Old 05-14-2007, 09:48 PM
bkirt bkirt is offline
Member
 
Join Date: May 2007
Posts: 5
bkirt is on a distinguished road
POS printer test
Hi Levent,

Here is the test program followed by my ReceiptPrinter class. From the current symptoms, I am beginning to suspect that my problem is not knowing how to address the USB port that the printer is attached to. Windows does let me send characters directly to LPT1 (one of the test cases in the following) but I cannot seem to do the same to either of my USB printers.

Thanks very much for your help.

Bayless

Code:
/* * PrintTest.java * * Created on April 24, 2007, 12:19 PM */ package com.sailok.util; import java.awt.Dimension; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JFrame; public class PrintTest extends javax.swing.JFrame { private ReceiptPrinter receiptPrinter = new ReceiptPrinter(); private FileOutputStream fos; public PrintTest() { initComponents(); setPreferredSize(new Dimension(300, 200)); pack(); try { /* * Note: only one of the following should be used at any time * although both should be attempted separately * attempt to open printer by its Windows printer name */ // fos = new FileOutputStream("Star TSP100 Cutter (TSP143)"); // attempt to open printer by its USB port name fos = new FileOutputStream("USB002"); // attempt to open LPT1 as a test of procedures only // fos = new FileOutputStream("LPT1"); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(this, "Cannot open file\n" + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE); } } private void initComponents() { openButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); display = new javax.swing.JTextArea(); exitButton = new javax.swing.JButton(); getContentPane().setLayout(null); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); openButton.setFont(new java.awt.Font("Tahoma", 1, 11)); openButton.setText("Open"); openButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { openButtonActionPerformed(evt); } }); getContentPane().add(openButton); openButton.setBounds(151, 120, 80, 23); display.setColumns(20); display.setRows(5); jScrollPane1.setViewportView(display); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(10, 10, 220, 92); exitButton.setFont(new java.awt.Font("Tahoma", 1, 11)); exitButton.setText("Exit"); exitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitButtonActionPerformed(evt); } }); getContentPane().add(exitButton); exitButton.setBounds(10, 120, 70, 23); pack(); } private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) { try { fos.close(); } catch (IOException e) { JOptionPane.showMessageDialog(this, "Unable to close printer port", "Warning", JOptionPane.WARNING_MESSAGE); } dispose(); } private void openButtonActionPerformed(java.awt.event.ActionEvent evt) { // this section attempts to send the BEL character to the printer port byte bel = 0x07; try { fos.write(bel); fos.flush(); } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error trying to write\n" + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE); } // this section appends the BEL to the printed message and sends it to the Windows printer String msg = "test"; msg += ((char) 0x07); receiptPrinter.printIt(msg); // this section displays a hex dump of the printed message // note that the BEL is being converted to a box and that // is what actually prints on the printer instead of the beep for (int i = 0; i < msg.length(); i += 5) { for (int j = 0; j < 5; j++) { if ((i + j) < msg.length()) { int x = msg.charAt(i + j); display.append(String.format("%02x ", x)); } } display.append(" "); for (int j = 0; j < 5; j++) { if (i + j < msg.length()) { char c = msg.charAt(i + j); display.append(String.format(" %c", c)); } } display.append("\n"); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new PrintTest().setVisible(true); } }); } private javax.swing.JTextArea display; private javax.swing.JButton exitButton; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton openButton; } /** * com.sailok.util.ReceiptPrinter.java * <p> * This class provides a means to print a text or graphics message to * a receipt printer. It includes an option to pop up a JOptionPane * with a configurable message that will cause the program to wait for * the print to finish before continuing. It is fully copyrighted and * owned by the developer, SailOK Software, Oklahoma City, OK. * <p> * A license to use, executed by SailOK, is required to use the program * in any way. A source code license is also available. If you have a * source code license then you may modify the program in any way you * desire. Any such modifications, however, will not be covered under * any SailOK warranty. The program may not be resold, even if you * have made modifications to it. * <p> * Any other use of this program or any of its parts is unauthorized * and is punishable by law. * <p> * Created on May 2, 2007, 11:58 AM */ package com.sailok.util; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.print.PrinterJob; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.Color; import java.awt.Graphics; /** * This class provides a means to print a text or graphics message to * a receipt printer. It includes an option to pop up a JOptionPane * with a configurable message that will cause the program to wait for * the print to finish before continuing. * <p> * @author Bayless Kirtley */ public class ReceiptPrinter implements Printable { private JFrame printFrame; private String waitMsg; /** Inner class for the actual printed object */ class PrintFrame extends JFrame { PrintFrame(String msg) { setBackground(new Color(255, 255, 255, 0)); add(new JLabel(msg)); pack(); setVisible(true); } } /** Creates a new instance of ReceiptPrinter with a default wait message */ public ReceiptPrinter() { waitMsg = "Wait for the printer to finish\nClick the OK button when done"; } /** * Creates a new instance of ReceiptPrinter with a wait message. * * @param msg the wait message */ public ReceiptPrinter(String msg) { waitMsg = msg; } /** * Sends the actual message to the receipt printer - does not wait. * * @param msg the actual message to be printed */ public void printIt(String msg) { printIt(msg, false); } /** * Sends the actual message to the receipt printer. * * @param msg the actual message to be printed * @param wait show JOptionPane to wait for print to finish */ public void printIt(String msg, boolean wait) { printFrame = new PrintFrame(msg); PrinterJob job = PrinterJob.getPrinterJob(); PageFormat format = job.defaultPage(); format.setOrientation(PageFormat.PORTRAIT); job.setPrintable(this, format); try { job.print(); if (wait) JOptionPane.showMessageDialog(null, waitMsg, "Information", JOptionPane.INFORMATION_MESSAGE); } catch (PrinterException e) { e.printStackTrace(); } printFrame.dispose(); } /** * Print method required by Printable interface. * * @param g the graphics context * @param format the page format * @param pagenum the page number requested to print * @return int flag indicating page existance */ public int print(Graphics g, PageFormat format, int pagenum) { if (pagenum > 0) return Printable.NO_SUCH_PAGE; g.translate((int) format.getImageableX(), (int) format.getImageableY()); printFrame.paint(g); return Printable.PAGE_EXISTS; } }

Last edited by levent : 05-14-2007 at 09:51 PM. Reason: You seem to forget closing your [code] tag :)
Reply With Quote