Java and XON XOFF serial print

Viewed 225

I m build a java code to use xon xoff protocol to print a fiscal document with cash register Custom K3. This is the setting of this cash register. enter image description here

This is the code about DriverPrinterSeriale.java

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 DriverPrinterSeriale {
    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;
    public static MyLog4J log;

    public DriverPrinterSeriale(String portaCOM){
        log = new MyLog4J();
        try {
            this.connect(portaCOM);
        } catch (IOException e) {
            log.logStackTrace(e);
        }
    }

    public void apriCassetto(){
        try {
            outStream = serialPort.getOutputStream();
            
            //inStream = serialPort.getInputStream();
            //log.information("output acquisito ");
            String messageString = "a";
            outStream.write(messageString.getBytes()); 
            log.information("cassetto aperto");
        } catch (Exception 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");
            //String messageString = "j";
            //outStream.write(messageString.getBytes()); 
            //messageString = "'PANTALONE'10H1R";
            //outStream.write(messageString.getBytes()); 
            //String messageString = "\"MAGLIA. 1\"100H1R";
            outStream.write(codiceDaInviare.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);
        }
    }

    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;
        }
    }

    /**
     * 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 = 19200; // 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");
        }
    }
}

This is the code to print a fiscal document:

driverSeriale = new DriverPrinterSeriale("COM3");
String rigaDaStampare = "\"Pippo"+
                        "\"10"+"*1"+"H1R";
                driverSeriale.provaScontrino(rigaDaStampare);

I have no error but I m not able to print fiscal document. nothing comes out of the cash register

1 Answers

I found a copy of your printer manual located here. Page 40 lists the printer parameters supported. From your description, it seems like there is a mismatch of parameters. First, I would try setting it to something other than CUSTOM XON-XOFF and see if the printer will work with hardware flow control. That shouldn't be a problem with today's equipment and may make this work out of the box.

If you can't change to hardware flow control, then I think you need to set flowcontrol in your serial port. Here is a link to Java documentation on setFlowControlMode. From the current configuration, it appears you might want to logical or include both FLOWCONTROL_XONXOFF_IN and FLOWCONTROL_XONXOFF_OUT.

Related