コード例 #1
0
 /**
  * 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);
     }
   }
 }
コード例 #2
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
  }
コード例 #3
0
ファイル: TanitaInstrument.java プロジェクト: emorency/onyx
  /** Reestablish a lost connection. */
  protected void reestablishConnection() {

    String[] options = {
      tanitaResourceBundle.getString("OK"),
      tanitaResourceBundle.getString("Cancel"),
      tanitaResourceBundle.getString("Settings")
    };

    // Loop until connection is reestablished.
    int selectedOption;
    while (serialPort == null || !serialPort.isCTS()) {

      selectedOption =
          JOptionPane.showOptionDialog(
              appWindow,
              tanitaResourceBundle.getString("Err.No_communication"),
              tanitaResourceBundle.getString("Title.Communication_problem"),
              JOptionPane.OK_CANCEL_OPTION,
              JOptionPane.ERROR_MESSAGE,
              null,
              options,
              tanitaResourceBundle.getString("OK"));

      // OK option selected.
      if (selectedOption == 0) {

        // Try to reestablish connection.
        setupSerialPort();

        // Cancel option selected.
      } else if (selectedOption == 1) {

        exitUI();
        break;

        // Configuration option selected.
      } else if (selectedOption == 2) {

        // List all serial port in a drop down list, so a new one can be
        // selected.
        refreshSerialPortList();
        String selectedPort =
            (String)
                JOptionPane.showInputDialog(
                    appWindow,
                    tanitaResourceBundle.getString("Instruction.Choose_port"),
                    tanitaResourceBundle.getString("Title.Settings"),
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    availablePortNames.toArray(),
                    getTanitaCommPort());

        if (selectedPort != null) {
          setTanitaCommPort(selectedPort);

          try {
            settingsHelper.saveSettings(tanitaLocalSettings);
          } catch (CouldNotSaveSettingsException e) {
            log.error("Local settings could not be persisted.", e);
          }

          setupSerialPort();
        } else {
          exitUI();
        }
      }
    }
  }