/** Always use flow control, not considered a user-setable option */ protected void setSerialPort(SerialPort activeSerialPort) throws gnu.io.UnsupportedCommOperationException { // find the baud rate value, configure comm options int baud = 19200; // default, but also defaulted in the initial value of selectedSpeed for (int i = 0; i < validBaudNumber().length; i++) { if (validBaudRates()[i].equals(mBaudRate)) { baud = validBaudNumber()[i]; } } activeSerialPort.setSerialPortParams( baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // set RTS high, DTR high - done early, so flow control can be configured after activeSerialPort.setRTS(true); // not connected in some serial ports and adapters activeSerialPort.setDTR(true); // pin 1 in Mac DIN8; on main connector, this is DTR // configure flow control to always on int flow = SerialPort.FLOWCONTROL_RTSCTS_OUT; activeSerialPort.setFlowControlMode(flow); log.debug( "Found flow control " + activeSerialPort.getFlowControlMode() + " RTSCTS_OUT=" + SerialPort.FLOWCONTROL_RTSCTS_OUT + " RTSCTS_IN= " + SerialPort.FLOWCONTROL_RTSCTS_IN); }
public SerialSettings getSettings() { return new SerialSettings( mSerialPort.getBaudRate(), mSerialPort.getDataBits(), mSerialPort.getStopBits(), mSerialPort.getParity(), mSerialPort.getFlowControlMode()); }
/** * Sets the connection parameters to the setting in the parameters object. If set fails return the * parameters object to origional settings and throw exception. * * @throws Exception if the configured parameters cannot be set properly on the port. */ public void setConnectionParameters() throws Exception { // Save state of parameters before trying a set. int oldBaudRate = m_SerialPort.getBaudRate(); int oldDatabits = m_SerialPort.getDataBits(); int oldStopbits = m_SerialPort.getStopBits(); int oldParity = m_SerialPort.getParity(); int oldFlowControl = m_SerialPort.getFlowControlMode(); // Set connection parameters, if set fails return parameters object // to original state. try { m_SerialPort.setSerialPortParams( m_Parameters.getBaudRate(), m_Parameters.getDatabits(), m_Parameters.getStopbits(), m_Parameters.getParity()); } catch (UnsupportedCommOperationException e) { m_Parameters.setBaudRate(oldBaudRate); m_Parameters.setDatabits(oldDatabits); m_Parameters.setStopbits(oldStopbits); m_Parameters.setParity(oldParity); final String errMsg = "Unsupported parameter"; logger.debug( "{} failed to set up one of [baudRate, dataBits, stopBits, parity]: {}", errMsg, e.getMessage()); throw new Exception(errMsg); } // Set flow control. try { m_SerialPort.setFlowControlMode( m_Parameters.getFlowControlIn() | m_Parameters.getFlowControlOut()); } catch (UnsupportedCommOperationException e) { final String errMsg = "Unsupported flow control"; logger.debug("{}: {}", errMsg, e.getMessage()); throw new Exception(errMsg); } } // setConnectionParameters
/** * Can the port accept additional characters? The state of CTS determines this, as there seems to * be no way to check the number of queued bytes and buffer length. This might go false for short * intervals, but it might also stick off if something goes wrong. */ public boolean okToSend() { if ((activeSerialPort.getFlowControlMode() & SerialPort.FLOWCONTROL_RTSCTS_OUT) == SerialPort.FLOWCONTROL_RTSCTS_OUT) { if (checkBuffer) { log.debug("CTS: " + activeSerialPort.isCTS() + " Buffer Empty: " + OutputBufferEmpty); return (activeSerialPort.isCTS() && OutputBufferEmpty); } else { log.debug("CTS: " + activeSerialPort.isCTS()); return (activeSerialPort.isCTS()); } } else { if (checkBuffer) { log.debug("Buffer Empty: " + OutputBufferEmpty); return (OutputBufferEmpty); } else { log.debug("No Flow Control or Buffer Check"); return (true); } } }