我想写一个程序在Termic打印机(POS)上打印。该打印机通过COM8连接到我的PC。我在Windows中将此打印机添加为通用打印机。我找到了一个演示程序来完成它。这个程序可以在这个priner打印机。现在我想构建要在此打印机上打印的代码。所以我有这个
package prove;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.mcsolution.common.LoggerFactory.MyLog4J;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
public class SerialPortHandler {
private SerialPort serialPort;
private OutputStream outStream;
private InputStream inStream;
public static MyLog4J log;
public SerialPortHandler(String portaCOM){
log = new MyLog4J();
try {
this.connect(portaCOM);
} catch (IOException e) {
log.logStackTrace(e);
}
}
public static void main(String[] args){
SerialPortHandler s = new SerialPortHandler("COM8");
try {
s.connect("COM8");
} catch (IOException e) {
log.logStackTrace(e);
}
}
public void provaScontrino(String codiceDaInviare){
try {
outStream = serialPort.getOutputStream();
//inStream = serialPort.getInputStream();
log.information("output acquisito ora provo a stampare uno scontrino");
outStream.write(codiceDaInviare.getBytes());
log.information("scontrino stampato ora apro il cassetto");
} catch (Exception e) {
log.logStackTrace(e);
}
}
public void connect(String portName) throws IOException {
try {
// Obtain a CommPortIdentifier object for the port you want to open
CommPortIdentifier portId =
CommPortIdentifier.getPortIdentifier(portName);
log.information("apro porta seriale");
serialPort =
(SerialPort) portId.open("Demo application", 5000);
setSerialPortParameters();
log.information("settaggio porta terminato");
} catch (NoSuchPortException e) {
log.logStackTrace(e);
throw new IOException(e.getMessage());
} catch (PortInUseException e) {
log.logStackTrace(e);
throw new IOException(e.getMessage());
} catch (IOException e) {
log.logStackTrace(e);
serialPort.close();
throw e;
}
}
/**
* Get the serial port input stream
* @return The serial port input stream
*/
public InputStream getSerialInputStream() {
return inStream;
}
/**
* Get the serial port output stream
* @return The serial port output stream
*/
public OutputStream getSerialOutputStream() {
return outStream;
}
/**
* Sets the serial port parameters
*/
private void setSerialPortParameters() throws IOException {
int baudRate = 9600; // 57600bps
//int baudRate = 38400; // 57600bps
try {
// Set serial port to 57600bps-8N1..my favourite
serialPort.setSerialPortParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE
);
log.information("settaggio porta iniziato");
serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_NONE);
log.information("settaggio porta eseguito");
} catch (UnsupportedCommOperationException ex) {
log.logStackTrace(ex);
throw new IOException("Unsupported serial port parameter");
}
}
}
在主要课程中我这样做:
serialPort = new SerialPortHandler("COM8");
String rigaDaStampare ="pippo";
serialPort.provaScontrino(rigaDaStampare);
但是当我尝试运行此代码时,我看到打印机潜水的led连接打开了一秒但是没有打印。 哪里有错误?