示例#1
0
  /**
   * 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
示例#2
0
 private void printPortStatus() {
   System.out.println("baud rate: " + port.getBaudRate());
   System.out.println("data bits: " + port.getDataBits());
   System.out.println("stop bits: " + port.getStopBits());
   System.out.println("parity:    " + port.getParity());
 }