示例#1
0
  /**
   * This creates an mbed object for an mbed connected over Serial. <br>
   * Using this class requires the Sun Communications API to be installed
   *
   * @param PortName The Serial Port mbed is connected to eg "COM5" on Windows.
   * @param Baud The baud rate
   */
  public SerialRPC(String PortName, int Baud) {
    // open serial port
    try {
      mbedPortID = CommPortIdentifier.getPortIdentifier(PortName);

      mbedPort = mbedPortID.open("mbed", 100000);
      mbedSerialPort = (SerialPort) mbedPort;
      mbedSerialPort.setSerialPortParams(
          Baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

      inputStream = new DataInputStream(mbedPort.getInputStream());
      outputStream = new PrintStream(mbedPort.getOutputStream(), true);

      reader = new BufferedReader(new InputStreamReader(inputStream));
      to_mbed = new PrintWriter(outputStream, true);

      mbedSerialPort.addEventListener(this);
      // Important to set this regardless of whether interrupts are in use to keep the buffer clear
      mbedSerialPort.notifyOnDataAvailable(true);

    } catch (TooManyListenersException e) {
      // Error adding event listener
      System.out.println("Too many Event Listeners");
    } catch (NoSuchPortException e) {
      System.out.println("No Such Port");
    } catch (PortInUseException e) {
      System.out.println("Port Already In Use");
    } catch (UnsupportedCommOperationException e) {
      System.out.println("Unsupported Comm Operation");
    } catch (IOException e) {
      System.out.println("Serial Port IOException");
    }
  }
示例#2
0
  /** Close the serial port. */
  public void delete() {
    // Close the serial port
    try {
      if (inputStream != null) inputStream.close();
      outputStream.close();
    } catch (IOException e) {

    }
    mbedSerialPort.removeEventListener();
    mbedSerialPort.close();
    mbedPort.close();
  }