コード例 #1
0
  public int setUpBam() {
    Enumeration<?> portIdentifiers = CommPortIdentifier.getPortIdentifiers();
    CommPortIdentifier portId = null;

    while (portIdentifiers.hasMoreElements()) {
      CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();

      if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL
          && pid.getName().equals(wantedPortName)) {
        portId = pid;
        System.out.println("Found port: " + pid.getName());
        break;
      }
    }

    try {
      port = (SerialPort) portId.open("Driver", 10000);
    } catch (PortInUseException e) {
      System.err.println("Port already in use: " + e);
      System.exit(1);
    }

    try {
      port.setSerialPortParams(
          57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {
      System.err.println("Failed to set up port: " + e);
    }
    System.out.println("Port should be open now");
    return 0;
  }
コード例 #2
0
  public void printAllPorts() {
    Enumeration ports = CommPortIdentifier.getPortIdentifiers();

    if (ports == null) {
      System.out.println("No comm ports found!");
      return;
    }

    // print out all ports
    System.out.println("printing all ports...");
    while (ports.hasMoreElements()) {
      System.out.println("  " + ((CommPortIdentifier) ports.nextElement()).getName());
    }
    System.out.println("done.");
  }