java类gnu.io.CommPortIdentifier的实例源码

UI_uyari.java 文件源码 项目:uyariSistemi 阅读 24 收藏 0 点赞 0 评论 0
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());
        }
    }
PanTiltControlDynamixel.java 文件源码 项目:jaer 阅读 18 收藏 0 点赞 0 评论 0
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!");
        }

    }
}
PanTiltControlPTU.java 文件源码 项目:jaer 阅读 19 收藏 0 点赞 0 评论 0
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!");
        }
    }
}
DynamixelControl.java 文件源码 项目:jaer 阅读 16 收藏 0 点赞 0 评论 0
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!");
        }
    }
}
StreamCommand.java 文件源码 项目:jaer 阅读 22 收藏 0 点赞 0 评论 0
/**
  * 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()]);
 }
HyperTerminal.java 文件源码 项目:jaer 阅读 27 收藏 0 点赞 0 评论 0
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");
        }
    }
SerialManagement.java 文件源码 项目:POPBL_V 阅读 16 收藏 0 点赞 0 评论 0
/** 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");
        }
    }
}
RXTXCommInterface.java 文件源码 项目:FlashLib 阅读 24 收藏 0 点赞 0 评论 0
@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();
    }
}
RpLidarLowLevelDriver.java 文件源码 项目:RPLidar4J 阅读 17 收藏 0 点赞 0 评论 0
/**
 * 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();
}
ArduinoController.java 文件源码 项目:robotoy 阅读 18 收藏 0 点赞 0 评论 0
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;
}
RXTXUtility.java 文件源码 项目:4mila-1.0 阅读 24 收藏 0 点赞 0 评论 0
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);
}
Serial.java 文件源码 项目:JLamp 阅读 32 收藏 0 点赞 0 评论 0
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.");
        }
    }
}
SerialLinkFactory.java 文件源码 项目:Ardulink-2 阅读 22 收藏 0 点赞 0 评论 0
@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();
        }
    };
}
TwoWaySerialComm.java 文件源码 项目:GPSSimulator 阅读 17 收藏 0 点赞 0 评论 0
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");
    }

}
SerialEvent.java 文件源码 项目:Serial 阅读 19 收藏 0 点赞 0 评论 0
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();
    }
}
CommStream.java 文件源码 项目:micro-Blagajna 阅读 31 收藏 0 点赞 0 评论 0
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) {
        } 
    }
RtuTransportRxtx.java 文件源码 项目:modbus-mini 阅读 18 收藏 0 点赞 0 评论 0
@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());
}
SerialComm.java 文件源码 项目:wot_gateways 阅读 24 收藏 0 点赞 0 评论 0
/**
 * 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();
}
OptolinkInterface.java 文件源码 项目:optolink 阅读 18 收藏 0 点赞 0 评论 0
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);
    }
SwegonVentilationSerialConnector.java 文件源码 项目:openhab-hdl 阅读 18 收藏 0 点赞 0 评论 0
@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);
    }
}
OpenEnergyMonitorSerialConnector.java 文件源码 项目:openhab-hdl 阅读 18 收藏 0 点赞 0 评论 0
@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);
    }
}
RFXComSerialConnector.java 文件源码 项目:openhab-hdl 阅读 26 收藏 0 点赞 0 评论 0
@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();
}
EpsonProjectorSerialConnector.java 文件源码 项目:openhab-hdl 阅读 19 收藏 0 点赞 0 评论 0
/**
 * {@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);
    }

}
SerialConnector.java 文件源码 项目:openhab-hdl 阅读 24 收藏 0 点赞 0 评论 0
/**
 * {@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;
    }
}
SerialConnection.java 文件源码 项目:Ardulink-1 阅读 22 收藏 0 点赞 0 评论 0
/**
 * 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;
}
AbstractSerialMeasuringDevice.java 文件源码 项目:tellervo 阅读 18 收藏 0 点赞 0 评论 0
/**
 * 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;
}
SerialConnection.java 文件源码 项目:contexttoolkit 阅读 21 收藏 0 点赞 0 评论 0
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;
}
MenuBar.java 文件源码 项目:Brino 阅读 22 收藏 0 点赞 0 评论 0
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);
    }

}
TwoWaySerialComm2.java 文件源码 项目:ATCommandTester 阅读 16 收藏 0 点赞 0 评论 0
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;
}
CommStream.java 文件源码 项目:unicenta 阅读 21 收藏 0 点赞 0 评论 0
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) {
        } 
    }


问题


面经


文章

微信
公众号

扫码关注公众号