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


Java CommPortIdentifier類代碼示例

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


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

示例1: initialize

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
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());
        }
    }
 
開發者ID:altnokburcu,項目名稱:uyariSistemi,代碼行數:38,代碼來源:UI_uyari.java

示例2: SerialComm

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
/**
 * 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();
}
 
開發者ID:heia-fr,項目名稱:wot_gateways,代碼行數:34,代碼來源:SerialComm.java

示例3: connect

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

}
 
開發者ID:andrey-desman,項目名稱:openhab-hdl,代碼行數:35,代碼來源:EpsonProjectorSerialConnector.java

示例4: connect

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

    }
}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:23,代碼來源:PanTiltControlDynamixel.java

示例5: connect

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
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!");
        }
    }
}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:21,代碼來源:PanTiltControlPTU.java

示例6: connect

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
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!");
        }
    }
}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:20,代碼來源:DynamixelControl.java

示例7: getPortNames

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
/**
  * 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()]);
 }
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:23,代碼來源:StreamCommand.java

示例8: openPort

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
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");
        }
    }
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:33,代碼來源:HyperTerminal.java

示例9: connect

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
/** 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");
		}
	}
}
 
開發者ID:AitorB,項目名稱:POPBL_V,代碼行數:27,代碼來源:SerialManagement.java

示例10: open

import gnu.io.CommPortIdentifier; //導入依賴的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

示例11: RpLidarLowLevelDriver

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
/**
 * 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();
}
 
開發者ID:ev3dev-lang-java,項目名稱:RPLidar4J,代碼行數:28,代碼來源:RpLidarLowLevelDriver.java

示例12: findSerialPort

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
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;
}
 
開發者ID:gustavohbf,項目名稱:robotoy,代碼行數:17,代碼來源:ArduinoController.java

示例13: getPort

import gnu.io.CommPortIdentifier; //導入依賴的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

示例14: connect

import gnu.io.CommPortIdentifier; //導入依賴的package包/類
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.");
		}
	}
}
 
開發者ID:AoLab,項目名稱:JLamp,代碼行數:25,代碼來源:Serial.java

示例15: newLink

import gnu.io.CommPortIdentifier; //導入依賴的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


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