title: JSSC Serial connection and its flow control
body: <p>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.</p>
  
  <p><strong>SOFTWARE FLOW CONTROL</strong></p>
  
  <p>My jssc code to open the port is this:</p>
  
  <pre><code>serialPort.openPort();
  serialPort.setParams(baud, data, stop, SerialPort.PARITY_NONE);
  serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT);
  </code></pre>
  
  <p>This works only for a small text output, for longer output the printer throw a full buffer error.</p>
  
  <p><strong>HARDWARE FLOW CONTROL</strong></p>
  
  <p>I also try to switch printer to cts/rts flow control and change my code to this:</p>
  
  <pre><code>serialPort.openPort();
  serialPort.setParams(baud, data, stop, SerialPort.PARITY_NONE, true, false);
  serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
  </code></pre>
  
  <p>But nothing changes. I still get the buffer error for long output.</p>
  
  <p>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!</p>
  
  <p>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?</p>
  
  <p><strong>MANUAL HARDWARE FLOW CONTROL TEST</strong></p>
  
  <p>This is my manual hardware flow control test. 
  It uses canSend boolean var to control the output stream.</p>
  
  <pre><code>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)) &gt;= 0) {
          if(byteread&gt;0){
              while(!canSend){
                  Thread.sleep(CTS_WAIT);
              }
              if(byteread&lt;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;
          }            
      }
  }
  </code></pre>
  
  <p>Seems that the flowcontrol settings is completely ignored.
  What can be? Is it a jssc error a windows driver error?</p>
  
  <p>I also tries to check it with getFlowControlMode() and I correctly get a values of 3 for the hardware flow control test.</p>