如何在Java中使用Xon-Xoff协议?

时间:2014-10-14 09:01:24

标签: java

我尝试开发一个用于打印财务凭证的代码。 我使用Java(编程语言)和XON-XOFF协议来打印票证。

所以我写了这段代码:

    public SerialPortHandler(String portaCOM){
        log = new MyLog4J();
        try {
            this.connect(portaCOM);
            this.provaScontrino();
        } catch (IOException 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");
            //System.out.println("apro porta seriale");
            // Get 
            serialPort =
                (SerialPort) portId.open("Demo application", 5000);
            // Set the parameters of the connection.
            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;
        }
    }

   public void provaScontrino(){
        try {
            outStream = serialPort.getOutputStream();
            //inStream = serialPort.getInputStream();
            log.information("output acquisito ora provo a stampare uno scontrino");
            String messageString = "j";
            outStream.write(messageString.getBytes()); 
            //messageString = "'PANTALONE'10H1R";
            //outStream.write(messageString.getBytes()); 
            messageString = "\"MAGLIA\"3*100H1P";
            outStream.write(messageString.getBytes()); 
            messageString = "1T";
            outStream.write(messageString.getBytes());
            messageString = "J";
            outStream.write(messageString.getBytes());
            log.information("scontrino stampato ora apro il cassetto");
            apriCassetto();
        } catch (Exception e) {
            log.logStackTrace(e);
        }
    }

因此,我希望这个代码可以使用#34; 3 MAGLIE"但是我收到了这样一张票:

DITTA S.R.L.
  NAME OF CITY
   P.IVA

                 EURO
Operatore 12
Reparto 1         0,00
TOTALE EURO       0,00
CONTANTI          0,00

14/10/14 10:55     SF.4

我们可以帮助我吗?

Reguards

0 个答案:

没有答案