static public void init(String[] args) { m_TestPortName = "cu.usbserial-FTOXM3NX"; if (args.length > 0) m_TestPortName = args[0]; Enumeration e = CommPortIdentifier.getPortIdentifiers(); boolean found = false; String last = null; while (e.hasMoreElements()) { CommPortIdentifier portid = (CommPortIdentifier) e.nextElement(); if (portid.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portid.getName().equals(m_TestPortName)) found = true; last = portid.getName(); } } if (!found) m_TestPortName = last; }
public boolean isCurrentlyOwned() { return getCurrentOwner() != null; }
/** * This has not been tested at all */ public static void addPortName(String portName, int portType, CommDriver driver) { synchronized (m_Mutex) { m_PortIdentifiers.put(portName, new CommPortIdentifier(portName, portType, driver)); } }
static protected void openPort() throws Exception { try { CommPortIdentifier portid = CommPortIdentifier.getPortIdentifier(m_TestPortName); m_Port = (SerialPort) portid.open(APPLICATION_NAME, 1000); m_Out = m_Port.getOutputStream(); m_In = m_Port.getInputStream(); drain(m_In); } catch (NoSuchPortException e) { fail("could no open port '%s'\n", m_TestPortName); } }
/** * Returns the serial port instance. * * @param aOptions * the serial port options, cannot be <code>null</code>. * @return the serial port instance, never <code>null</code>. * @throws NoSuchPortException * in case the desired port does not exist; * @throws PortInUseException * in case the desired port is already in use; * @throws IOException * in case of other I/O problems. */ private SerialPort getSerialPort( final CommPortOptions aOptions ) throws NoSuchPortException, PortInUseException, IOException { final CommPortIdentifier commPortId = CommPortIdentifier.getPortIdentifier( aOptions.getPortName() ); if ( commPortId.isCurrentlyOwned() && ( commPortId.getCurrentOwner() != CONNECT_ID ) ) { throw new PortInUseException(); } final CommPort commPort = commPortId.open( CONNECT_ID, 2000 ); if ( !( commPort instanceof SerialPort ) ) { throw new IOException( "Not a serial port?!" ); } return ( SerialPort )commPort; }
.getPortIdentifier(m_Port); if (id == null) { fail("No id for this serial port"); if (id.getCurrentOwner() == null || !id.getCurrentOwner().equals(APPLICATION_NAME)) { fail("Wrong or missing owner for this serial port (got \"%s\", expected \"%s\")", id.getCurrentOwner(), APPLICATION_NAME); cpiEnum = CommPortIdentifier.getPortIdentifiers(); if (cpi.getName().equals(getPortName())) { origOwnerName = cpi.getCurrentOwner(); break; cpiEnum = CommPortIdentifier.getPortIdentifiers(); if (cpi.getName().equals(getPortName())) { checkOwnerName = cpi.getCurrentOwner(); break;
boolean ENUMERATE = false; for (CommPortIdentifier portid : m_OpenPorts.values()) if (portid.getName().equals(portName)) return portid; if (ENUMERATE) { // enumerating ports takes time, lets see if we can avoid it Enumeration e = getPortIdentifiers(); while (e.hasMoreElements()) { CommPortIdentifier portid = (CommPortIdentifier) e.nextElement(); if (portid.getName().equals(portName)) return portid; return new CommPortIdentifier(portName, PORT_SERIAL, null); return new CommPortIdentifier(portName, PORT_SERIAL, null);
System.out.println("PureJavaCommDemo"); CommPortIdentifier portid = null; Enumeration e = CommPortIdentifier.getPortIdentifiers(); while (e.hasMoreElements()) { portid = (CommPortIdentifier) e.nextElement(); System.out.println("found " + portid.getName()); System.out.println("use " + portid.getName()); SerialPort port = (SerialPort) portid.open("PureJavaCommDemo", 1000); port.notifyOnDataAvailable(true); port.notifyOnOutputEmpty(true);
cpiEnum = CommPortIdentifier.getPortIdentifiers(); if (cpi.getName().equals(getPortName())) { origOwnerName = cpi.getCurrentOwner(); break; cpiEnum = CommPortIdentifier.getPortIdentifiers(); if (cpi.getName().equals(getPortName())) { checkOwnerName = cpi.getCurrentOwner(); break;
fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED); try { while (System.currentTimeMillis() - t0 < timeout) { m_Mutex.wait(5); if (!isCurrentlyOwned()) break; if (isCurrentlyOwned()) throw new PortInUseException(getCurrentOwner()); m_OpenPorts.put(port, this); m_Owners.put(this, appname); fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED); return port;
public static void testDevicePathToInvalidTTYInCommPortIdentifier() throws Exception { begin("TestDevicePathToInvalidTTY");// - getPortIdentifier on invalid device"); // Must throw NoSuchPortException File tempFile = File.createTempFile("pjc", null); tempFile.deleteOnExit(); try { CommPortIdentifier.getPortIdentifier(tempFile.getAbsolutePath()); fail("Got an identifier for an invalid device"); } catch (NoSuchPortException nspe) { // All good } finishedOK(); } }
fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED); try { while (System.currentTimeMillis() - t0 < timeout) { m_Mutex.wait(5); if (!isCurrentlyOwned()) break; if (isCurrentlyOwned()) throw new PortInUseException(); m_OpenPorts.put(port, this); m_Owners.put(this, appname); fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED); return port;
public class Ports { public static List<CommPortIdentifier> listCommPorts() { List<CommPortIdentifier> listOfPorts = new ArrayList<>(25); Enumeration ports = CommPortIdentifier.getPortIdentifiers(); while (ports.hasMoreElements()) { CommPortIdentifier curPort = (CommPortIdentifier) ports.nextElement(); //get only serial ports if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL) { listOfPorts.add(curPort); } } return listOfPorts; } }
/** * Closes the communications port. */ public void close() { CommPortIdentifier.close(this); }
static void close(CommPort port) { synchronized (m_Mutex) { CommPortIdentifier portid = m_OpenPorts.remove(port); if (portid != null) { portid.fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED); m_Owners.remove(portid); } } }
static protected void openPort() throws Exception { try { CommPortIdentifier portid = CommPortIdentifier.getPortIdentifier(m_TestPortName); m_Port = (SerialPort) portid.open("PureJavaCommTestSuite", 1000); m_Out = m_Port.getOutputStream(); m_In = m_Port.getInputStream(); drain(m_In); } catch (NoSuchPortException e) { fail("could no open port '%s'\n", m_TestPortName); } }
boolean ENUMERATE = false; for (CommPortIdentifier portid : m_OpenPorts.values()) if (portid.getName().equals(portName)) return portid; if (ENUMERATE) { // enumerating ports takes time, lets see if we can avoid it Enumeration e = getPortIdentifiers(); while (e.hasMoreElements()) { CommPortIdentifier portid = (CommPortIdentifier) e.nextElement(); if (portid.getName().equals(portName)) return portid; return new CommPortIdentifier(portName, PORT_SERIAL, null); } else { return new CommPortIdentifier(portName, PORT_SERIAL, null);
fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED); try { while (System.currentTimeMillis() - t0 < timeout) { m_Mutex.wait(5); if (!isCurrentlyOwned()) break; if (isCurrentlyOwned()) throw new PortInUseException(getCurrentOwner()); m_OpenPorts.put(port, this); m_Owners.put(this, appname); fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED); return port;
public static void testMissingPortInCommPortIdentifier() throws Exception { begin("TestMissingPort"); // - getPortIdentifier on missing port // Must throw NoSuchPortException try { CommPortIdentifier.getPortIdentifier("blablub"); fail("Got an identifier for non-exisiting path"); } catch (NoSuchPortException nspe) { // All good } finishedOK(); }
/** * Closes the communications port. */ public void close() { CommPortIdentifier.close(this); }