如何使用java在termic printer POS上打印

如何使用java在termic printer POS上打印,java,printing,Java,Printing,我想写一个程序,在终端打印机(POS)上打印。这台打印机通过COM8连接到我的电脑。我已在windows中将此打印机添加为通用打印机。我已经找到了一个演示程序来做这件事。这个程序可以在这个打印机上运行。现在我想在这台打印机上打印代码。所以我有这个 package prove; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import com.mcsolution.co

我想写一个程序,在终端打印机(POS)上打印。这台打印机通过COM8连接到我的电脑。我已在windows中将此打印机添加为通用打印机。我已经找到了一个演示程序来做这件事。这个程序可以在这个打印机上运行。现在我想在这台打印机上打印代码。所以我有这个

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);
但是,当我尝试运行此代码时,我看到打印机上的指示灯“接头”已亮起一秒钟,但无法打印。 错误在哪里