Java 类gnu.io.CommPortIdentifier 实例源码

项目:uyariSistemi    文件:UI_uyari.java   
public void initialize() {
        System.setProperty("gnu.io.rxtx.SerialPorts", getComPortName());        
        CommPortIdentifier portId = null;        
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if(currPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (currPortId.getName().equals(txtComPortName.getText())) {
                    System.out.println(txtComPortName.getText());
                    portId = currPortId;
                    break;
                }
            }
        }
        if (portId == null) {

            JOptionPane.showMessageDialog(null," Portuna bağlı cihaz yok!","Hata",JOptionPane.ERROR_MESSAGE);
            System.out.println("Porta bağlı cihaz yok!");
            return;
        }
System.out.println(portId);
        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);

            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

            serialPort.addEventListener((SerialPortEventListener) this);
           serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
项目:jaer    文件:PanTiltControlDynamixel.java   
void connect(String destination) throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
    if (portIdentifier.isCurrentlyOwned()) {
        log.warning("Error: Port for Dynamixel-Communication is currently in use");
    } else {
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        if (commPort instanceof SerialPort) {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            in = serialPort.getInputStream();
            out = serialPort.getOutputStream();
            serialReader = new SerialReader(in);
            serialPort.addEventListener(serialReader);
            serialPort.notifyOnDataAvailable(true);
            connected = true;
            log.info("Connected to Dynamixel!");
        } else {
            log.warning("Error: Cannot connect to Dynamixel!");
        }

    }
}
项目:jaer    文件:PanTiltControlPTU.java   
void connect(String destination) throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
    if (portIdentifier.isCurrentlyOwned()) {
        log.warning("Error: Port for Pan-Tilt-Communication is currently in use");
    } else {
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        if (commPort instanceof SerialPort) {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            in = serialPort.getInputStream();
            out = serialPort.getOutputStream();
            serialPort.addEventListener(new SerialReader(in));
            serialPort.notifyOnDataAvailable(true);
            connected = true;
            log.info("Connected to Pan-Tilt-Unit!");
        } else {
            log.warning("Error: Cannot connect to Pan-Tilt-Unit!");
        }
    }
}
项目:jaer    文件:DynamixelControl.java   
void connect(String destination) throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
    if (portIdentifier.isCurrentlyOwned()) {
        log.warning("Error: Port for Dynamixel-Communication is currently in use");
    } else {
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        if (commPort instanceof SerialPort) {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(57142, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            in = serialPort.getInputStream();
            out = serialPort.getOutputStream();
            serialPort.addEventListener(new SerialReader(in));
            serialPort.notifyOnDataAvailable(true);
            log.info("Connected to Dynamixel!");
        } else {
            log.warning("Error: Cannot connect to Dynamixel!");
        }
    }
}
项目:jaer    文件:StreamCommand.java   
/**
  * gets a list of all available serial port names
  *
  * @return a platform dependent list of strings representing all the
  *      available serial ports -- it is the application's responsibility
  *      to identify the right port to which the device is actually connected
  */
 public String[] getPortNames()
 {
     ArrayList<String> ret= new ArrayList<String>();
     Enumeration<CommPortIdentifier> portsEnum= CommPortIdentifier.getPortIdentifiers();

     while(portsEnum.hasMoreElements())
     {
         CommPortIdentifier pid= portsEnum.nextElement();
         if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL) {
    ret.add(pid.getName());
}
     }

     return ret.toArray(new String[ret.size()]);
 }
项目:jaer    文件:HyperTerminal.java   
private void openPort() {
        appendString("opening " + portName + "...");
        try {

            CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(portName);
            port = (SerialPort) cpi.open(portName, 1000);

            port.setSerialPortParams(
                    Integer.parseInt(baudText.getText()),
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
//            port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
            port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN
                    | SerialPort.FLOWCONTROL_RTSCTS_OUT);
            port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
            port.addEventListener(this);
            port.notifyOnDataAvailable(true);
            port.notifyOnCTS(true);

            isr = new InputStreamReader(port.getInputStream());
            os = port.getOutputStream();

        } catch (Exception e) {
            log.warning(e.toString());
            appendString(e.toString());
        } finally {

            updateFlags();
            appendString("done.\n");
        }
    }
项目:POPBL_V    文件:SerialManagement.java   
/** Method to connect to an available port */
@SuppressWarnings("static-access")
private void connect(String portName) throws Exception {
    CommPortIdentifier commPortIdentifier = CommPortIdentifier.getPortIdentifier(portName);

    if (commPortIdentifier.isCurrentlyOwned()) {
        System.out.println("Error: Port is currently in use");
    } else {
        CommPort commPort = commPortIdentifier.open(this.getClass().getName(), 2000);

        if (commPort instanceof SerialPort) {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(References.BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            this.inputStream = serialPort.getInputStream();
            this.outputStream = serialPort.getOutputStream();

            References.SERIAL_READER = new SerialReader(this.inputStream);
            readThread = new Thread(References.SERIAL_READER);
            readThread.start();
        } else {
            System.out.println("Error: Only serial ports allowed");
        }
    }
}
项目:FlashLib    文件:RXTXCommInterface.java   
@Override
public void open() {
    try{
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned())
            System.out.println("Error: Port is currently in use");
        else {
            CommPort port = portIdentifier.open(this.getClass().getName(), getTimeout());//for now class name

            if (port instanceof SerialPort){
                serial = (SerialPort) port;
                serial.setSerialPortParams(baudrate, SerialPort.DATABITS_8 
                        , SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                serial.enableReceiveTimeout(timeout);

                this.in = serial.getInputStream();
                this.out = serial.getOutputStream();
                isOpened = true;
            }
            else
                System.out.println("Error: Only serial ports are handled");
        }   
    }
    catch (PortInUseException | UnsupportedCommOperationException | NoSuchPortException | IOException e) {
        if(serial != null)
            serial.close();
        serial = null;
        e.printStackTrace();
    }
}
项目:RPLidar4J    文件:RpLidarLowLevelDriver.java   
/**
 * Initializes serial connection
 *
 * @param portName Path to serial port
 * @param listener Listener for in comming packets
 * @throws Exception
 */
public RpLidarLowLevelDriver(final String portName, final RpLidarListener listener) throws Exception {

    log.info("Opening port " + portName);

    this.listener = listener;

    //Configuration for Serial port operations
    final CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    final CommPort commPort = portIdentifier.open("FOO", 2000);
    serialPort = (SerialPort) commPort;
    serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
    serialPort.setDTR(false); // lovely undocumented feature where if true the motor stops spinning

    in = serialPort.getInputStream();
    out = serialPort.getOutputStream();

    readThread = new ReadSerialThread();
    new Thread(readThread).start();
}
项目:robotoy    文件:ArduinoController.java   
public static CommPortIdentifier findSerialPort() throws Exception {
    CommPortIdentifier serialPort = null;
       @SuppressWarnings("unchecked")
    java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
       while ( portEnum.hasMoreElements() ) 
       {
        CommPortIdentifier portIdentifier = portEnum.nextElement();
        if (CommPortIdentifier.PORT_SERIAL==portIdentifier.getPortType()) {
            if (serialPort==null)
                serialPort = portIdentifier;
            else
                throw new Exception("More than one serial port found! "+serialPort.getName()+" and "+portIdentifier.getName());
        }
       }
       return serialPort;
}
项目:4mila-1.0    文件:RXTXUtility.java   
public static FMilaSerialPort getPort(int speed, String port) throws IOException {
  SerialPort serialPort;

  try {

    CommPortIdentifier comm = CommPortIdentifier.getPortIdentifier(port);
    if (comm.isCurrentlyOwned()) {
      throw new IOException("StationInUseError");
    }
    serialPort = comm.open("4mila", 2000);

    serialPort.setSerialPortParams(speed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
    serialPort.disableReceiveTimeout();
    serialPort.disableReceiveFraming();
    serialPort.disableReceiveThreshold();
    serialPort.notifyOnDataAvailable(true);
    serialPort.notifyOnOutputEmpty(true);
  }
  catch (PortInUseException | UnsupportedCommOperationException | NoSuchPortException e) {
    throw new IOException(e.getMessage(), e);
  }

  return new RXTXSerialPort(serialPort);
}
项目:JLamp    文件:Serial.java   
public void connect(String portName) throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    LOG.info("Found the " + portName + " port.");
    if (portIdentifier.isCurrentlyOwned()) {
        LOG.error("Port is currently in use");
    } else {
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        LOG.info("Opened port " + portName);

        if (commPort instanceof SerialPort) {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.PARITY_EVEN, SerialPort.FLOWCONTROL_NONE);

            printWriter = new PrintWriter(serialPort.getOutputStream());
            bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

               // For skipping AnA string
               skipInput();

        } else {
            LOG.error("Only serial ports are handled by this example.");
        }
    }
}
项目:Ardulink-2    文件:SerialLinkFactory.java   
@Override
public LinkDelegate newLink(SerialLinkConfig config)
        throws NoSuchPortException, PortInUseException,
        UnsupportedCommOperationException, IOException {
    CommPortIdentifier portIdentifier = CommPortIdentifier
            .getPortIdentifier(config.getPort());
    checkState(!portIdentifier.isCurrentlyOwned(),
            "Port %s is currently in use", config.getPort());
    final SerialPort serialPort = serialPort(config, portIdentifier);

    StreamConnection connection = new StreamConnection(
            serialPort.getInputStream(), serialPort.getOutputStream(),
            config.getProto());

    ConnectionBasedLink connectionBasedLink = new ConnectionBasedLink(
            connection, config.getProto());
    @SuppressWarnings("resource")
    Link link = config.isQos() ? new QosLink(connectionBasedLink)
            : connectionBasedLink;

    waitForArdulink(config, connectionBasedLink);
    return new LinkDelegate(link) {
        @Override
        public void close() throws IOException {
            super.close();
            serialPort.close();
        }
    };
}
项目:GPSSimulator    文件:TwoWaySerialComm.java   
private void connect(String portName) throws Exception {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    if (portIdentifier.isCurrentlyOwned())
        throw new PortInUseException();

    CommPort commPort = portIdentifier.open(this.getClass().getName(), COMPORT_PORT);

    if (commPort instanceof SerialPort) {
        SerialPort serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(BAUD_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        InputStream in = serialPort.getInputStream();
        OutputStream out = serialPort.getOutputStream();

        new Thread(new SerialReader(in)).start();
        new Thread(new SerialWriter(out)).start();

    } else {
        throw new PortUnreachableException("ERROR - Only serial ports are handled by this class");
    }

}
项目:Serial    文件:SerialEvent.java   
public void handleEvent() {
    try {
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(SerialConf.WINDOWS_PORT);

        if (portId.isCurrentlyOwned()) {
            System.out.println("Port busy!");
        } else {
            CommPort commPort = portId.open("whatever it's name", SerialConf.TIME_OUT);
            if (commPort instanceof SerialPort) {
                serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(SerialConf.BAUD, 
                                                SerialPort.DATABITS_8, 
                                                SerialPort.STOPBITS_1, 
                                                SerialPort.PARITY_EVEN);
                in = serialPort.getInputStream();
                serialPort.notifyOnDataAvailable(true);
                serialPort.addEventListener(this);
            } else {
                commPort.close();
                System.out.println("the port is not serial!");
            }
        }
    } catch (Exception e) {
        serialPort.close();
        System.out.println("Error: SerialOpen!!!");
        e.printStackTrace();
    }
}
项目:micro-Blagajna    文件:CommStream.java   
private void init() {

        try {  
            if (m_out == null) {
                m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(m_sPort); // Tomamos el puerto                   
                m_CommPortPrinter = m_PortIdPrinter.open("PORTID", 2000); // Abrimos el puerto       

                m_out = m_CommPortPrinter.getOutputStream(); // Tomamos el chorro de escritura   

                if (m_PortIdPrinter.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    ((SerialPort)m_CommPortPrinter).setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // Configuramos el puerto
                } else if (m_PortIdPrinter.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
                    ((ParallelPort)m_CommPortPrinter).setMode(1);
                }
            }
        } catch (Exception e) {
            m_PortIdPrinter = null;
            m_CommPortPrinter = null;  
            m_out = null;
            m_in = null;
//        } catch (NoSuchPortException e) {
//        } catch (PortInUseException e) {
//        } catch (UnsupportedCommOperationException e) {
//        } catch (IOException e) {
        } 
    }
项目:modbus-mini    文件:RtuTransportRxtx.java   
@Override
synchronized protected void openPort() throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
    if (port != null)
        return;
    log.info("Opening port: " + portName);
    CommPortIdentifier ident = CommPortIdentifier.getPortIdentifier(portName);
    port = ident.open("ModbusRtuClient on " + portName, 2000);
    port.setOutputBufferSize(buffer.length);
    port.setInputBufferSize(buffer.length);
    try {
        port.setSerialPortParams(baudRate, dataBits, stopBits, parity);             
        port.enableReceiveTimeout(timeout);
        port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
    } catch (UnsupportedCommOperationException e) {
        close();
        throw e;
    }
    log.info("Port opened: " + port.getName());
}
项目:wot_gateways    文件:SerialComm.java   
/**
 * Constructor
 * 
 * @param portName
 *            The name of the serial port
 * @param listeners
 *            The listeners for incoming telegrams
 * @throws Exception
 */
public SerialComm(String portName, ArrayList<EnoceanListener> listeners)
        throws Exception {
    this.listeners = listeners;
    CommPortIdentifier portIdentifier = CommPortIdentifier
            .getPortIdentifier(portName);
    if (portIdentifier.isCurrentlyOwned()) {
        throw new Exception("Port is currently in use");
    }

    CommPort commPort = portIdentifier
            .open(this.getClass().getName(), 2000); // timeout 2 s.
    if (!(commPort instanceof SerialPort)) {
        throw new Exception("Only serial port is supported");
    }
    port = commPort;

    SerialPort serialPort = (SerialPort) commPort;

    // 57600 bit/s, 8 bits, stop bit length 1, no parity bit
    serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    input = serialPort.getInputStream();
    output = serialPort.getOutputStream();
}
项目:optolink    文件:OptolinkInterface.java   
OptolinkInterface(String device, int timeout) throws Exception {

        // constructor with implicit open

        this.device = device;

        log.debug("Open TTY {} ...", this.device);
        portIdentifier = CommPortIdentifier.getPortIdentifier(this.device);

        if (portIdentifier.isCurrentlyOwned()) {
            log.error("TTY {} in use.", this.device);
            throw new IOException();
        }
        commPort = portIdentifier.open(this.getClass().getName(), timeout);
        if (commPort instanceof SerialPort) {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_2, SerialPort.PARITY_EVEN);

            input = serialPort.getInputStream();
            output = serialPort.getOutputStream();
            commPort.enableReceiveTimeout(timeout); // Reading Time-Out
        }
        log.debug("TTY {} opened", this.device);
    }
项目:openhab-hdl    文件:SwegonVentilationSerialConnector.java   
@Override
public void connect() throws SwegonVentilationException {

    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier
                .getPortIdentifier(portName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(),
                2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        in = serialPort.getInputStream();

        logger.debug("Swegon ventilation Serial Port message listener started");

    } catch (Exception e) {
        throw new SwegonVentilationException(e);
    }
}
项目:openhab-hdl    文件:OpenEnergyMonitorSerialConnector.java   
@Override
public void connect() throws OpenEnergyMonitorException {

    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier
                .getPortIdentifier(portName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(),
                2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        in = serialPort.getInputStream();
        logger.debug("Open Energy Monitor Serial Port message listener started");

    } catch (Exception e) {
        throw new OpenEnergyMonitorException(e);
    }
}
项目:openhab-hdl    文件:RFXComSerialConnector.java   
@Override
public void connect(String device) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
    CommPortIdentifier portIdentifier = CommPortIdentifier
            .getPortIdentifier(device);

    CommPort commPort = portIdentifier
            .open(this.getClass().getName(), 2000);

    serialPort = (SerialPort) commPort;
    serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    serialPort.enableReceiveThreshold(1);
    serialPort.disableReceiveTimeout();

    in = serialPort.getInputStream();
    out = serialPort.getOutputStream();

    out.flush();
    if (in.markSupported()) {
        in.reset();
    }

    readerThread = new SerialReader(in);
    readerThread.start();
}
项目:openhab-hdl    文件:EpsonProjectorSerialConnector.java   
/**
 * {@inheritDoc}
 */
public void connect() throws EpsonProjectorException {

    try {
        logger.debug("Open connection to serial port '{}'", serialPortName);
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);

        CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();

        in = serialPort.getInputStream();
        out = serialPort.getOutputStream();

        out.flush();
        if (in.markSupported()) {
            in.reset();
        }

        // RXTX serial port library causes high CPU load
        // Start event listener, which will just sleep and slow down event loop
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        throw new EpsonProjectorException(e);
    }

}
项目:openhab-hdl    文件:SerialConnector.java   
/**
 * {@inheritDoc}
 **/
public void open() {

    try {

        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveThreshold(1);
        serialPort.disableReceiveTimeout();

        serialOutput = new OutputStreamWriter(serialPort.getOutputStream(), "US-ASCII");
           serialInput = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

           setSerialEventHandler(this);

           connected = true;

    } catch (NoSuchPortException noSuchPortException) {
        logger.error("open(): No Such Port Exception: ", noSuchPortException);
           connected = false;
    } catch (PortInUseException portInUseException) {
        logger.error("open(): Port in Use Exception: ", portInUseException);
           connected = false;
    } catch (UnsupportedCommOperationException unsupportedCommOperationException) {
        logger.error("open(): Unsupported Comm Operation Exception: ", unsupportedCommOperationException);
           connected = false;
    } catch (UnsupportedEncodingException unsupportedEncodingException) {
        logger.error("open(): Unsupported Encoding Exception: ", unsupportedEncodingException);
           connected = false;
    } catch (IOException ioException) {
        logger.error("open(): IO Exception: ", ioException);
           connected = false;
    }
}
项目:Ardulink-1    文件:SerialConnection.java   
/**
 * This method is used to get a list of all the available Serial ports
 * (note: only Serial ports are considered). Any one of the elements
 * contained in the returned {@link List} can be used as a parameter in
 * {@link #connect(String)} or {@link #connect(String, int)} to open a
 * Serial connection.
 * 
 * @return A {@link List} containing {@link String}s showing all available
 *         Serial ports.
 */
public List<String> getPortList() {
    List<String> ports = new ArrayList<String>();
    Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            ports.add(portId.getName());
        }
    }
    writeLog("found the following ports:");
    for (String port : ports) {
        writeLog("   " + port);
    }

    return ports;
}
项目:tellervo    文件:AbstractSerialMeasuringDevice.java   
/**
 * Get a vector of all the ports identified on this computer
 * 
 * @return
 */

public static Vector enumerateSerialPorts() {
    Enumeration ports = CommPortIdentifier.getPortIdentifiers();
    Vector portStrings = new Vector();

    while(ports.hasMoreElements()) {
        CommPortIdentifier currentPort = (CommPortIdentifier)ports.nextElement();

        if(currentPort.getPortType() != CommPortIdentifier.PORT_SERIAL)
            continue;

        portStrings.add(new String(currentPort.getName()));
    }

    return portStrings;
}
项目:contexttoolkit    文件:SerialConnection.java   
private CommPortIdentifier getSerialPort() {

    if (port != null) {
        try {
            return CommPortIdentifier.getPortIdentifier(port);
        } catch (NoSuchPortException e) {
            e.printStackTrace();
        }
        return null;
    }

    Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                return currPortId;
            }
        }
    }
    return null;
}
项目:Brino    文件:MenuBar.java   
public void setComs() {
    ArrayList<String> comList = new ArrayList<String>();
    Enumeration<CommPortIdentifier> comm = CommPortUtils.getComPorts();
    while (comm.hasMoreElements()) {
        CommPortIdentifier port_identifier = (CommPortIdentifier) comm
                .nextElement();
        if (port_identifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            // if (!comOldList.contains(port_identifier.getName()))
            comList.add(port_identifier.getName());
        }
    }
    if (comList.isEmpty()) {
        addCom("N�o h� portas dispon�veis");
    } else {
        addCom(comList);
    }

}
项目:ATCommandTester    文件:TwoWaySerialComm2.java   
public String listPorts() {
    List<String> port_name = new ArrayList<String>();
    String tmp = "";

    // int i = 0;
    @SuppressWarnings("unchecked")
    Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier
            .getPortIdentifiers();

    // Enumeration portList = CommPortIdentifier.getPortIdentifiers();
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum
                .nextElement();
        if (getPortTypeName(portIdentifier.getPortType()) == "Serial") {
            port_name.add(portIdentifier.getName());
            String tmp2 = portIdentifier.getName() + "#";

            tmp = tmp + tmp2;
        }
        System.out.println(portIdentifier.getName() + " - "
                + getPortTypeName(portIdentifier.getPortType()));
    }
    // i = port_name.size();

    return tmp;
}
项目:unicenta    文件:CommStream.java   
private void init() {

        try {  
            if (m_out == null) {
                m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(m_sPort); // Tomamos el puerto                   
                m_CommPortPrinter = m_PortIdPrinter.open("PORTID", 2000); // Abrimos el puerto       

                m_out = m_CommPortPrinter.getOutputStream(); // Tomamos el chorro de escritura   

                if (m_PortIdPrinter.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    ((SerialPort)m_CommPortPrinter).setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // Configuramos el puerto
                } else if (m_PortIdPrinter.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
                    ((ParallelPort)m_CommPortPrinter).setMode(1);
                }
            }
        } catch (Exception e) {
            m_PortIdPrinter = null;
            m_CommPortPrinter = null;  
            m_out = null;
            m_in = null;
//        } catch (NoSuchPortException e) {
//        } catch (PortInUseException e) {
//        } catch (UnsupportedCommOperationException e) {
//        } catch (IOException e) {
        } 
    }
项目:jmoduleconnect    文件:CommHandlerImpl.java   
/**
 * Creates an instance of this class.
 * @param commPortIdentifier Must point to an serial port
 * @param baudrate The baudrate of the communication. The baudrate must be 
 *        setted with <code>AT+IPR=*baudrate*</code>. The default baudrate
 *        of an device is <code>115200</code>
 * @param flowControlMode The flow control mode of the serial port
 * @return An instance of <code>CommHandlerImpl</code>
 * @throws PortInUseException The selected port is used by another application
 * @throws IOException The communication to the device failed
 * @throws IllegalArgumentException If parameter commPortIdentifier is 
 *         <code>null</code> or the result of {@link CommPortIdentifier#open(java.lang.String, int) } 
 *         is not an instance of {@link SerialPort}.
 * @since 1.5
 */
public static final CommHandler createCommHandler(final CommPortIdentifier commPortIdentifier
        , final int baudrate, final EnumSet<FlowControlMode> flowControlMode) 
        throws PortInUseException, IOException
{
    final CommHandlerImpl commHandler = new CommHandlerImpl();

    try
    {
        commHandler.init(commPortIdentifier, baudrate, flowControlMode);
        return commHandler;
    }
    catch (final PortInUseException | IOException ex)
    {
        commHandler.close();
        throw ex;
    }
}
项目:arduino-remote-uploader    文件:SerialSketchUploader.java   
public CommPortIdentifier findPort(String port) {
    // parse ports and if the default port is found, initialized the reader
    Enumeration portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {

        CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();

        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

            //System.out.println("Found port: " + portId.getName());

            if (portId.getName().equals(port)) {
                //System.out.println("Using Port: " + portId.getName());
                return portId;
            }
        }
    }

    throw new RuntimeException("Port not found " + port);
}
项目:arduino-remote-uploader    文件:SerialSketchUploader.java   
public void openSerial(String serialPortName, int speed) throws PortInUseException, IOException, UnsupportedCommOperationException, TooManyListenersException {

    CommPortIdentifier commPortIdentifier = findPort(serialPortName);

    // initalize serial port
    serialPort = (SerialPort) commPortIdentifier.open("Arduino", 2000);
    inputStream = serialPort.getInputStream();
    serialPort.addEventListener(this);

    // activate the DATA_AVAILABLE notifier
    serialPort.notifyOnDataAvailable(true);

    serialPort.setSerialPortParams(speed, SerialPort.DATABITS_8, 
            SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
}
项目:EasyVote    文件:VCDSerialPort.java   
/**
 * Versucht den seriellen Port zu �ffnen. Klappt nur, wenn er noch nicht von einer anderen Anwendung benutzt wird. Der jeweilige
 * Port wird mit �bergeben. UNter Windows ist die Schreibweise "COM8" zum Beispeil unter Linux "dev/tts0"
 * @param portName
 * @return
 * @throws PortInUseException
 * @throws IOException
 * @throws UnsupportedCommOperationException
 */
public boolean oeffneSerialPort(String portName) throws PortInUseException, IOException, UnsupportedCommOperationException
{
    Boolean foundPort = false;
    if (serialPortGeoeffnet != false) {
        System.out.println("Serialport bereits ge�ffnet");
        schliesseSerialPort();
        return false;
    }
    System.out.println("�ffne Serialport");
    enumComm = CommPortIdentifier.getPortIdentifiers();
    while(enumComm.hasMoreElements()) {
        serialPortId = (CommPortIdentifier) enumComm.nextElement();
        System.out.println("SerialportIDs:" + serialPortId.getName());
        if (portName.contentEquals(serialPortId.getName())) {
            foundPort = true;
            break;
        }
    }
    if (foundPort != true) {
        System.out.println("Serialport nicht gefunden: " + portName);
        return false;
    }
        serialPort = (SerialPort) serialPortId.open("�ffnen und Senden", 500);
        outputStream = serialPort.getOutputStream();
        inputStream = serialPort.getInputStream();

    serialPort.notifyOnDataAvailable(true);
        serialPort.setSerialPortParams(baudrate, dataBits, stopBits, parity);


    serialPortGeoeffnet = true;
    return true;
}
项目:Animator    文件:FlashExporter.java   
public String getPortTypeName(int portType) {
    switch (portType) {
    case CommPortIdentifier.PORT_I2C:
        return "I2C";
    case CommPortIdentifier.PORT_PARALLEL:
        return "Parallel";
    case CommPortIdentifier.PORT_RAW:
        return "Raw";
    case CommPortIdentifier.PORT_RS485:
        return "RS485";
    case CommPortIdentifier.PORT_SERIAL:
        return "Serial";
    default:
        return "unknown type";
    }
}
项目:openhab2-addons    文件:PlugwiseStickDiscoveryService.java   
protected void discoverSticks() {
    if (discovering) {
        logger.debug("Stick discovery not possible (already discovering)");
    } else {
        discovering = true;

        @SuppressWarnings("unchecked")
        Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers();

        while (discovering && portIdentifiers.hasMoreElements()) {
            CommPortIdentifier portIdentifier = portIdentifiers.nextElement();
            if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL
                    && !portIdentifier.isCurrentlyOwned()) {
                discoverStick(portIdentifier.getName());
            }
        }

        discovering = false;
        logger.debug("Finished discovering Sticks on serial ports");
    }
}
项目:openhab2-addons    文件:PlugwiseCommunicationContext.java   
@SuppressWarnings("unchecked")
private CommPortIdentifier findSerialPortIdentifier() throws PlugwiseInitializationException {
    Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier identifier = portList.nextElement();
        if (identifier.getPortType() == CommPortIdentifier.PORT_SERIAL
                && identifier.getName().equals(configuration.getSerialPort())) {
            logger.debug("Serial port '{}' has been found", configuration.getSerialPort());
            return identifier;
        }
    }

    // Build exception message when port not found
    StringBuilder sb = new StringBuilder();
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            sb.append(String.format("%s%n", id.getName()));
        }
    }
    throw new PlugwiseInitializationException(String.format(
            "Serial port '%s' could not be found. Available ports are:%n%s", configuration.getSerialPort(), sb));
}
项目:kkMulticopterFlashTool    文件:PortScanner.java   
/**
 * @return List all serial ports
 */
public static Vector<String> listSerialPorts()
    {
        Vector<String> ports = new Vector<String>();
        if (KKMulticopterFlashTool.ENABLE_PORT_CHECK) {
            java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
            while ( portEnum.hasMoreElements() ) 
            {
                CommPortIdentifier portIdentifier = portEnum.nextElement();
                if (portIdentifier.getPortType()==CommPortIdentifier.PORT_SERIAL){
                    if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                        if (portIdentifier.getName().contains("cu")){
                            ports.add(portIdentifier.getName());
                        }
                    } else {
                        ports.add(portIdentifier.getName());
                    }
                }
            } 
        }
        return ports;
    }
项目:kkMulticopterFlashTool    文件:SerialReader.java   
private void openPort() throws Exception{
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
    CommPort commPort = portIdentifier.open("LightController",2000);

       if ( commPort instanceof SerialPort )
       {
           serialPort = (SerialPort) commPort;
           serialPort.setSerialPortParams(baud,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

           in = serialPort.getInputStream();

           isr = new InputStreamReader(in);
           br = new BufferedReader(isr);
       }
}
项目:Girinoscope    文件:Serial.java   
public void connect(CommPortIdentifier portId) throws Exception {
    serialPort = (SerialPort) portId.open(getClass().getName(), TIME_OUT);

    serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

    output = serialPort.getOutputStream();
    input = serialPort.getInputStream();

    serialPort.notifyOnDataAvailable(false);
}