コード例 #1
0
 public synchronized void sendEvent(RobotEvent ev) {
   try {
     output.write(ev.toStringSend().getBytes()); // write needs a byte array instead of a string
     long milli = (long) (1.0 / ((float) serialPort.getBaudRate() / (8.0 * 16.0)) * 1000.0);
     long nano =
         (long) (1.0 / ((float) serialPort.getBaudRate() / (8.0 * 16.0)) * 1000000000.0)
             - milli * 1000000;
     Thread.sleep((int) milli, (int) nano);
   } catch (Exception e) {
   }
 }
コード例 #2
0
ファイル: SerialConnection.java プロジェクト: sdhibit/openhab
  /**
   * 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
コード例 #3
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());
 }