當前位置: 首頁>>代碼示例>>Java>>正文


Java PortInUseException類代碼示例

本文整理匯總了Java中gnu.io.PortInUseException的典型用法代碼示例。如果您正苦於以下問題:Java PortInUseException類的具體用法?Java PortInUseException怎麽用?Java PortInUseException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


PortInUseException類屬於gnu.io包,在下文中一共展示了PortInUseException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: open

import gnu.io.PortInUseException; //導入依賴的package包/類
@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();
	}
}
 
開發者ID:Flash3388,項目名稱:FlashLib,代碼行數:32,代碼來源:RXTXCommInterface.java

示例2: open

import gnu.io.PortInUseException; //導入依賴的package包/類
public void open() throws PortInUseException, UnsupportedCommOperationException, TooManyListenersException, IOException
{
	if (open)
	{
		log.info(commId.getName() + " already open, skipping");
		return;
	}

	log.info("Opening port " + commId.getName());
	buffer = new ByteBuffer();

	port = (SerialPort)commId.open("TimerInterface-"+commId.getName(), 30000);
	port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
	port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

	port.addEventListener(this);
	port.notifyOnDataAvailable(true);
	port.enableReceiveTimeout(30);

	os = port.getOutputStream();
	is = port.getInputStream();
	open = true;
   }
 
開發者ID:drytoastman,項目名稱:scorekeeperfrontend,代碼行數:24,代碼來源:SerialDataInterface.java

示例3: getPort

import gnu.io.PortInUseException; //導入依賴的package包/類
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);
}
 
開發者ID:innovad,項目名稱:4mila-1.0,代碼行數:26,代碼來源:RXTXUtility.java

示例4: newLink

import gnu.io.PortInUseException; //導入依賴的package包/類
@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();
		}
	};
}
 
開發者ID:Ardulink,項目名稱:Ardulink-2,代碼行數:30,代碼來源:SerialLinkFactory.java

示例5: connect

import gnu.io.PortInUseException; //導入依賴的package包/類
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");
    }

}
 
開發者ID:BenjiTrapp,項目名稱:GPSSimulator,代碼行數:23,代碼來源:TwoWaySerialComm.java

示例6: openPort

import gnu.io.PortInUseException; //導入依賴的package包/類
@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());
}
 
開發者ID:sp20,項目名稱:modbus-mini,代碼行數:20,代碼來源:RtuTransportRxtx.java

示例7: connect

import gnu.io.PortInUseException; //導入依賴的package包/類
@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();
}
 
開發者ID:andrey-desman,項目名稱:openhab-hdl,代碼行數:26,代碼來源:RFXComSerialConnector.java

示例8: open

import gnu.io.PortInUseException; //導入依賴的package包/類
/**
 * {@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;
	}
}
 
開發者ID:andrey-desman,項目名稱:openhab-hdl,代碼行數:40,代碼來源:SerialConnector.java

示例9: createCommHandler

import gnu.io.PortInUseException; //導入依賴的package包/類
/**
 * 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;
    }
}
 
開發者ID:sitec-systems,項目名稱:jmoduleconnect,代碼行數:33,代碼來源:CommHandlerImpl.java

示例10: openSerial

import gnu.io.PortInUseException; //導入依賴的package包/類
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);
}
 
開發者ID:andrewrapp,項目名稱:arduino-remote-uploader,代碼行數:17,代碼來源:SerialSketchUploader.java

示例11: oeffneSerialPort

import gnu.io.PortInUseException; //導入依賴的package包/類
/**
 * 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;
}
 
開發者ID:SecUSo,項目名稱:EasyVote,代碼行數:43,代碼來源:VCDSerialPort.java

示例12: write

import gnu.io.PortInUseException; //導入依賴的package包/類
/**
 * Write contents of stream inputStream to serial port
 * 
 * @param stream
 * @throws IOException
 * @throws UnsupportedCommOperationException
 * @throws PortInUseException
 */
public void write(InputStream inputStream) throws IOException,
		UnsupportedCommOperationException, PortInUseException {
	SerialPort serialPort = initSerialPort(getPortIdentifier(deviceName));
	OutputStream serialOut = serialPort.getOutputStream();
	writeTo(inputStream, serialOut);
	serialOut.close();
	serialPort.close();
}
 
開發者ID:Twissi,項目名稱:Animator,代碼行數:17,代碼來源:FlashExporter.java

示例13: read

import gnu.io.PortInUseException; //導入依賴的package包/類
@Override
public SmartMeterReading read(String device) throws PortInUseException, IOException, UnsupportedCommOperationException {

    BigDecimal READING_POWER = new BigDecimal(new Random().nextInt(5000));
    READING_TOTAL = READING_TOTAL.add(READING_POWER.divide(new BigDecimal(WATT_TO_WATTHOURS), 2, BigDecimal.ROUND_DOWN));
    READING_ONE = READING_TOTAL;

    SmartMeterReading smartMeterReading = new SmartMeterReading();
    smartMeterReading.meterTotal = new Meter(READING_TOTAL, "WH");
    smartMeterReading.meterOne = new Meter(READING_ONE, "WH");
    smartMeterReading.meterTwo = new Meter(READING_TWO, "WH");
    smartMeterReading.power = new Meter(READING_POWER, "W");
    smartMeterReading.complete = true;
    log.debug(smartMeterReading.toString());

    return smartMeterReading;
}
 
開發者ID:oliverwehrens,項目名稱:jpowermeter,代碼行數:18,代碼來源:SimulatedEhzSmlReader.java

示例14: connect

import gnu.io.PortInUseException; //導入依賴的package包/類
@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();
}
 
開發者ID:openhab,項目名稱:openhab1-addons,代碼行數:24,代碼來源:RFXComSerialConnector.java

示例15: open

import gnu.io.PortInUseException; //導入依賴的package包/類
/**
 * {@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;
    }
}
 
開發者ID:openhab,項目名稱:openhab1-addons,代碼行數:40,代碼來源:SerialConnector.java


注:本文中的gnu.io.PortInUseException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。