Question

I need to send a long text to serial printer using Jssc library. After a few I get a full buffer error, so I thought it can be the flow control settings, because for smaller transmission everything works. The printer can use XON/XOFF flow control configuration or CTS/RTS hardware signals.

SOFTWARE FLOW CONTROL

My jssc code to open the port is this:

serialPort.openPort();
serialPort.setParams(baud, data, stop, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT);

This works only for a small text output, for longer output the printer throw a full buffer error.

HARDWARE FLOW CONTROL

I also try to switch printer to cts/rts flow control and change my code to this:

serialPort.openPort();
serialPort.setParams(baud, data, stop, SerialPort.PARITY_NONE, true, false);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);

But nothing changes. I still get the buffer error for long output.

Than I check with a COM monitor if my printer correctly sends XOFF chars or CTS signal before filling its buffer and it well works!

At the end I tried to manually manage the output stream stopping it while CTS signal is off (using an event handler) in this way everything works also with long output.... but this is not what the flowcontrol should do automatically?

MANUAL HARDWARE FLOW CONTROL TEST

This is my manual hardware flow control test. It uses canSend boolean var to control the output stream.

private boolean canSend=true;

public void openPort(
    serialPort.openPort();
    serialPort.setParams(baud, data, stop, SerialPort.PARITY_NONE, true, false);
    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
    serialPort.addEventListener(new SerialPortReader(), SerialPort.MASK_CTS);
    canSend=true;
}

public void write(byte[] content) throws Exception {
    ByteArrayInputStream  b= new  ByteArrayInputStream (content);
    byte[] bytes =new byte[OUT_BUFFER_SIZE];
    int byteread=0;
    while ((byteread=b.read(bytes, 0, bytes.length)) >= 0) {
        if(byteread>0){
            while(!canSend){
                Thread.sleep(CTS_WAIT);
            }
            if(byteread<bytes.length){
                byte[] tocopy=new byte[byteread];
                System.arraycopy(bytes, 0, tocopy, 0, byteread);
                serialPort.writeBytes(tocopy);
            }else{
                serialPort.writeBytes(bytes);
            }
        }
    }
}

private class SerialPortReader implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        if(event.isCTS()){
            canSend=event.getEventValue() == 1;
        }            
    }
}

Seems that the flowcontrol settings is completely ignored. What can be? Is it a jssc error a windows driver error?

I also tries to check it with getFlowControlMode() and I correctly get a values of 3 for the hardware flow control test.

Was it helpful?

Solution

At the end the problems was not the jssc library but usb to serial adapter. Changing it with a FTDI chip based adapter it works. (Moreover seems that also the last driver's release of Prolific adapter works).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top