public int setUpBam() { Enumeration<?> portIdentifiers = CommPortIdentifier.getPortIdentifiers(); CommPortIdentifier portId = null; while (portIdentifiers.hasMoreElements()) { CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL && pid.getName().equals(wantedPortName)) { portId = pid; System.out.println("Found port: " + pid.getName()); break; } } try { port = (SerialPort) portId.open("Driver", 10000); } catch (PortInUseException e) { System.err.println("Port already in use: " + e); System.exit(1); } try { port.setSerialPortParams( 57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { System.err.println("Failed to set up port: " + e); } System.out.println("Port should be open now"); return 0; }
public static void main(String[] args) { /* * 不带参数的getPortIdentifiers方法获得一个枚举对象 * , * 该对象又包含了系统中管理每个端口的CommPortIdentifier对象 * 。 * 注意这里的端口不仅仅是指串口,也包括并口 * 。这个方法还可以带参数。 * getPortIdentifiers * (CommPort) * 获得与已经被应用程序打开的端口相对应的CommPortIdentifier对象 * 。 * getPortIdentifier * (String * portName)获取指定端口名 * (比如“COM1”) * 的CommPortIdentifier对象 * 。 */ portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) /* getPortType方法返回端口类型 */ { if (portId.getName().equals(CommPortUtil.getCommPortName())) /* 找Windows下的第一个串口*/ { // if (portId.getName().equals("/dev/term/a"))/* // * 找Unix-like系统下的第一个串口 // */{ @SuppressWarnings("unused") SimpleRead reader = new SimpleRead(); } } } }
public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); // hasMoreElements()指的是,是否還有其它物件內容 while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); System.out.println(" " + portId.getName()); // 取得通訊埠的名稱,例如:COM1、COM2 } }
/** * Check that the port specified is in fact available; * * @return The portName specified, or, if not available, return first port found (to use as * default) */ public static String checkPortAvailable(String portName) { String firstPort = null; Enumeration enumr = CommPortIdentifier.getPortIdentifiers(); int i = 1; while (enumr.hasMoreElements()) { CommPortIdentifier comId = (CommPortIdentifier) enumr.nextElement(); if (comId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (i == 1) { firstPort = comId.getName(); } i++; if (portName.equalsIgnoreCase(comId.getName())) { return portName; } } } return firstPort; }
/** * Check that the port specified is in fact available; * * @return true or false */ public static boolean isPortAvailable(String portName) { Enumeration enumr = CommPortIdentifier.getPortIdentifiers(); while (enumr.hasMoreElements()) { CommPortIdentifier comId = (CommPortIdentifier) enumr.nextElement(); if (comId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portName.equalsIgnoreCase(comId.getName())) { return true; } } } return false; }
public static String getStatusOfPorts() { Enumeration enumr = CommPortIdentifier.getPortIdentifiers(); StringBuffer s = new StringBuffer(); while (enumr.hasMoreElements()) { CommPortIdentifier comId = (CommPortIdentifier) enumr.nextElement(); if (comId.getPortType() == CommPortIdentifier.PORT_SERIAL) { s.append(comId.getName() + ": "); if (comId.isCurrentlyOwned()) { s.append("owner: " + comId.getCurrentOwner() + " "); } else { s.append("no owner."); } s.append("\n"); } } return s.toString(); }
/** * -------------------------------------------------------------------------- Get serial ports * * @return String array of ports on workstation (may return null) */ public static String[] getAvailablePorts() { Enumeration enumr = CommPortIdentifier.getPortIdentifiers(); String[] retStr = null; ArrayList list = new ArrayList(); while (enumr.hasMoreElements()) { CommPortIdentifier comId = (CommPortIdentifier) enumr.nextElement(); if (comId.getPortType() == CommPortIdentifier.PORT_SERIAL) { list.add(comId.getName()); // System.out.println(comId.getName()); } } Object[] ports = list.toArray(); if (ports != null) { retStr = new String[ports.length]; for (int i = 0; i < ports.length; i++) { retStr[i] = (String) ports[i]; } } return retStr; }
private void initialize() { // ========================================== /* * get System porperties: */ String operatingSystem = null; Properties p = System.getProperties(); Enumeration e = p.elements(); while (e.hasMoreElements()) { operatingSystem = e.nextElement().toString(); // System.out.println(operatingSystem); if (operatingSystem.equals("Linux")) { logger.debug("found " + operatingSystem + " Operating System"); comPort = "/dev/ttyS0"; break; } if (operatingSystem.equals("Windows XP")) { logger.debug("found " + operatingSystem + " Operating System"); comPort = "COM1"; break; } // if(operatingSystem.equals("Linux") || operatingSystem.equals("Windows")) { // logger.debug("found "+operatingSystem+" Operating System"); // break; // } } // ===============end=========================== Enumeration pList = CommPortIdentifier.getPortIdentifiers(); // logger.debug("(Khepera) initializeeeee()..."); while (pList.hasMoreElements()) { CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement(); if (cpi.getName().equals(comPort)) { try { com = null; com = (SerialPort) cpi.open("KHEPERA_" + comPort, 1000); try { // ======================================== /* under Linux this block is crucial to setSerialPortParams() * - i have no idea why... */ String s = "default settings: " + com.getBaudRate() + " " + com.getDataBits() + " " + com.getParity() + " " + com.getStopBits(); System.out.println(s); // ===============end========================= com.setSerialPortParams( 57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE); System.out.println( "Current settings: " + com.getBaudRate() + " " + com.getDataBits() + " " + com.getParity() + " " + com.getStopBits()); } catch (UnsupportedCommOperationException e1) { logger.debug("(Khepera) UnsupportedCommOperation: " + e1); e1.printStackTrace(); } // try { // com.setSerialPortParams(BAUDRATE, // com.DATABITS_8, // com.STOPBITS_2, // com.PARITY_NONE); // com.setFlowControlMode(com.FLOWCONTROL_NONE); // } catch (UnsupportedCommOperationException e1) { // logger.debug("(Khepera) UnsupportedCommOperation: "+e1); // e1.printStackTrace(); // } logger.debug("[Khepera.initialize()] established " + comPort); out = null; out = com.getOutputStream(); inStream = null; inStream = com.getInputStream(); byte[] readBuffer2 = new byte[1000]; if (inStream.available() > 0) { int numBytes2 = inStream.read(readBuffer2); String result2 = new String(readBuffer2, 0, numBytes2); logger.debug("result2: " + result2); } // in = null; in = new BufferedReader(new InputStreamReader(inStream)); bufferedKheperaAnswer = new BufferedKheperaAnswer(logger, inStream, this, writelock, debug, operatingSystem); try { com.addEventListener(bufferedKheperaAnswer); com.notifyOnOutputEmpty(true); com.notifyOnDataAvailable(true); com.notifyOnOverrunError(true); com.notifyOnBreakInterrupt(true); com.notifyOnCarrierDetect(true); com.notifyOnCTS(true); com.notifyOnDSR(true); com.notifyOnFramingError(true); com.notifyOnParityError(true); com.notifyOnRingIndicator(false); } catch (TooManyListenersException e1) { logger.debug( "[Khepera.initialize()] can not add further eventListeners to serialPort object" + e); e1.printStackTrace(); } } catch (PortInUseException e2) { logger.debug("[Khepera.initialize()] Port in use: " + e2); e2.printStackTrace(); } catch (IOException e3) { logger.debug( "[Khepera.initialize()] IOException while initialising COMport: <" + comPort + ">" + e); e3.printStackTrace(); } break; } } if (com == null) logger.error( "[Khepera.initialize()] unable to find specified COMport " + comPort + ". giving up! (the robot is not connected yet.)"); else { logger.debug("[Khepera.initialize()] starting bufferedKheperaAnswer.start()"); bufferedKheperaAnswer.start(); if (debug) logger.debug("[Khepera.initialize()] initialize() finished"); update = new KheperaUpdateManager(this, logger, debug); logger.debug("[Khepera.initialize()] starting KheperaUpdateManager update.start()"); update.start(); } }