コード例 #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
ファイル: SerialDriverAdapter.java プロジェクト: KenC57/JMRI
  public String openPort(String portName, String appName) {
    // open the port, check ability to set moderators
    try {
      // get and open the primary port
      CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
      try {
        activeSerialPort = (SerialPort) portID.open(appName, 2000); // name of program, msec to wait
      } catch (PortInUseException p) {
        return handlePortBusy(p, portName, log);
      }

      // try to set it for communication via SerialDriver
      try {
        activeSerialPort.setSerialPortParams(
            9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
      } catch (gnu.io.UnsupportedCommOperationException e) {
        log.error("Cannot set serial parameters on port " + portName + ": " + e.getMessage());
        return "Cannot set serial parameters on port " + portName + ": " + e.getMessage();
      }

      // set RTS high, DTR high
      activeSerialPort.setRTS(true); // not connected in some serial ports and adapters
      activeSerialPort.setDTR(true); // pin 1 in DIN8; on main connector, this is DTR

      // disable flow control; hardware lines used for signaling, XON/XOFF might appear in data
      activeSerialPort.setFlowControlMode(0);
      activeSerialPort.enableReceiveTimeout(50); // 50 mSec timeout before sending chars

      // set timeout
      // activeSerialPort.enableReceiveTimeout(1000);
      log.debug(
          "Serial timeout was observed as: "
              + activeSerialPort.getReceiveTimeout()
              + " "
              + activeSerialPort.isReceiveTimeoutEnabled());

      // get and save stream
      serialStream = activeSerialPort.getInputStream();

      // purge contents, if any
      purgeStream(serialStream);

      // report status
      if (log.isInfoEnabled()) {
        log.info(
            "Wangrow " + portName + " port opened at " + activeSerialPort.getBaudRate() + " baud");
      }
      opened = true;

    } catch (gnu.io.NoSuchPortException p) {
      return handlePortNotFound(p, portName, log);
    } catch (Exception ex) {
      log.error("Unexpected exception while opening port " + portName + " trace follows: " + ex);
      ex.printStackTrace();
      return "Unexpected error while opening port " + portName + ": " + ex;
    }

    return null; // indicates OK return
  }
コード例 #3
0
 public SerialSettings getSettings() {
   return new SerialSettings(
       mSerialPort.getBaudRate(),
       mSerialPort.getDataBits(),
       mSerialPort.getStopBits(),
       mSerialPort.getParity(),
       mSerialPort.getFlowControlMode());
 }
コード例 #4
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
コード例 #5
0
  public synchronized String openPort(String portName, String appName) {
    // open the port, check ability to set moderators
    try {
      // get and open the primary port
      CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
      try {
        activeSerialPort = (SerialPort) portID.open(appName, 2000); // name of program, msec to wait
      } catch (PortInUseException p) {
        handlePortBusy(p, portName);
        return "Port " + p + " in use already";
      }

      // try to set it for communication via SerialDriver
      try {
        // Doc says 7 bits, but 8 seems needed
        activeSerialPort.setSerialPortParams(
            38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
      } catch (gnu.io.UnsupportedCommOperationException e) {
        log.error("Cannot set serial parameters on port " + portName + ": " + e.getMessage());
        return "Cannot set serial parameters on port " + portName + ": " + e.getMessage();
      }

      // set RTS high, DTR high
      activeSerialPort.setRTS(true); // not connected in some serial ports and adapters
      activeSerialPort.setDTR(true); // pin 1 in DIN8; on main connector, this is DTR

      // disable flow control; hardware lines used for signaling, XON/XOFF might appear in data
      activeSerialPort.setFlowControlMode(0);

      // set timeout
      log.debug(
          "Serial timeout was observed as: "
              + activeSerialPort.getReceiveTimeout()
              + " "
              + activeSerialPort.isReceiveTimeoutEnabled());

      // get and save stream
      serialStream = new DataInputStream(activeSerialPort.getInputStream());
      ostream = activeSerialPort.getOutputStream();

      // make less verbose
      sendBytes(new byte[] {(byte) 'L', (byte) '-', 10, 13});
      // purge contents, if any
      int count = serialStream.available();
      log.debug("input stream shows " + count + " bytes available");
      while (count > 0) {
        serialStream.skip(count);
        count = serialStream.available();
      }

      // report status?
      if (log.isInfoEnabled()) {
        log.info(
            portName
                + " port opened at "
                + activeSerialPort.getBaudRate()
                + " baud, sees "
                + " DTR: "
                + activeSerialPort.isDTR()
                + " RTS: "
                + activeSerialPort.isRTS()
                + " DSR: "
                + activeSerialPort.isDSR()
                + " CTS: "
                + activeSerialPort.isCTS()
                + "  CD: "
                + activeSerialPort.isCD());
      }

    } catch (java.io.IOException ex) {
      log.error("IO error while opening port " + portName, ex);
      return "IO error while opening port " + portName + ": " + ex;
    } catch (gnu.io.UnsupportedCommOperationException ex) {
      log.error("Unsupported communications operation while opening port " + portName, ex);
      return "Unsupported communications operation while opening port " + portName + ": " + ex;
    } catch (gnu.io.NoSuchPortException ex) {
      log.error("No such port: " + portName, ex);
      return "No such port: " + portName + ": " + ex;
    }
    return null; // indicates OK return
  }