Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created December 2, 2012 02:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dulichan/4186610 to your computer and use it in GitHub Desktop.
Save dulichan/4186610 to your computer and use it in GitHub Desktop.
COM port printer writing java example
package app.posmachine.machine;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class PosPrinter {
public static Enumeration printport = CommPortIdentifier
.getPortIdentifiers();
/**
* This method is used to create the stream to the printer.
*/
public static OutputStream getStream(String port) throws Exception {
while (printport.hasMoreElements()) {
CommPortIdentifier portid = (CommPortIdentifier) printport
.nextElement();
if (portid.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portid.getName().equals(port)) {
SerialPort com = (SerialPort) portid.open("ListPortClass",
300);
com.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream output = com.getOutputStream();
return output;
}
}
}
return null;
}
/**
* This method can be used to create a line
*/
public void printLine(String line, OutputStream outputStream)
throws IOException {
final byte[] bytes = line.getBytes("UTF-8");
outputStream.write(bytes);
newLine(outputStream);
}
/**
* Writes a new line in the printer
*/
public void newLine(OutputStream outputStream) throws IOException {
outputStream.write(new byte[] { 10, 13 });
}
private static final byte PRINTER = 1;
private static final byte DRAWER = 1;
private static final byte DISPLAY = 2;
/**
* Since the printer is connected to the customer display we need to switch
* between. below code is necessary for that purpose.
*/
public void prepareResource(OutputStream outputStream) {
try {
outputStream.write(new byte[] { 27, 61, PRINTER });
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@AustineGwa
Copy link

hey does this work with a fiscal printer or a thermal printer . plus what is the replacement for the javax.com API in 2021 most of this are depracated and removed from the jdk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment