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; }
/* * Open the port, wait for chip init */ public static int initialize(String portName) { System.out.println("Enumerating serial ports:"); portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { // DDT System.out.println(portId.getName()); if (portId.getName().equals(portName)) { System.out.println("Name match on port " + portName); // Init the port, matches name we were given try { serialPort = (SerialPort) portId.open("tx-skeleton", 64); } catch (PortInUseException e) { e.printStackTrace(); System.out.println("Port in use!"); return (4); } try { inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); } catch (IOException e) { System.out.println("Unable to connect to I/O streams"); return (3); } try { System.out.println("Initializing ADXL202 board..."); // 38400N81 serialPort.setSerialPortParams( 38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // ADXL202 is powered via DTR line, so set it on serialPort.setDTR(true); try { // Wait a couple of seconds for chip to initialize (blinking LED) Thread.sleep(2000); } catch (InterruptedException ie) { } } catch (UnsupportedCommOperationException e) { System.out.println("Unable to configure serial port!"); return (1); } return (0); } } } return (1); }
public static void initSerial() { Enumeration portList = CommPortIdentifier.getPortIdentifiers(); CommPortIdentifier portId = null; boolean portFound = false; while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM3")) { portFound = true; } } } if (!portFound) { System.out.println("port COM3 not found."); return; } SerialPort port; try { port = (SerialPort) portId.open("COM3", 2000); port.setSerialPortParams( 115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); comOS = new DataOutputStream(port.getOutputStream()); } catch (PortInUseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (UnsupportedCommOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** Opens the Serial port with the baud and port_name given */ public boolean OpenSerial(int baud, String port) { CommPortIdentifier portId = null; Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); while (portEnum.hasMoreElements()) { CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); if (currPortId.getName().equals(port)) { portId = currPortId; break; } } if (portId == null) { System.err.println("Can not open serial port"); return false; } try { serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000); serialPort.setSerialPortParams( baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); input = serialPort.getInputStream(); output = serialPort.getOutputStream(); serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); Thread.sleep(1500); } catch (Exception e) { return false; } return true; }
public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM17")) { // if (portId.getName().equals("/dev/term/a")) { try { serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000); } catch (PortInUseException e) { } try { outputStream = serialPort.getOutputStream(); } catch (IOException e) { } try { serialPort.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { } try { outputStream.write(messageString.getBytes()); } catch (IOException e) { } } } } }
public static void listPorts() { Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); while (portEnum.hasMoreElements()) { CommPortIdentifier portIdentifier = portEnum.nextElement(); System.out.println( portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType())); } }
public static Serial open(String defaultPort) { boolean portFound = false; final int mComPortIdentifier = CommPortIdentifier.PORT_SERIAL; final Enumeration portList = CommPortIdentifier.getPortIdentifiers(); final int BAUD = 115200; while (portList.hasMoreElements()) { final CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); System.out.println("Found port id: " + portId); if (portId.getPortType() == mComPortIdentifier) { System.out.println("Found CommPortIdentifier."); if (portId.getName().equals(defaultPort)) { System.out.println("Found port " + defaultPort); SerialPort serialPort; OutputStream outputStream = null; try { serialPort = (SerialPort) portId.open("SimpleWrite", 2000); } catch (PortInUseException e) { System.out.println("Port in use."); continue; } try { outputStream = serialPort.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } try { serialPort.setSerialPortParams( BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { e.printStackTrace(); } try { serialPort.notifyOnOutputEmpty(true); } catch (Exception e) { System.out.println("Error setting event notification"); System.out.println(e.toString()); System.exit(-1); } return new Serial(serialPort, outputStream); } } } if (!portFound) { System.out.println("port " + defaultPort + " not found."); } return null; }
public static List<String> getAvailablePorts() { List<String> portNames = new ArrayList<>(); Enumeration identifiers = CommPortIdentifier.getPortIdentifiers(); while (identifiers.hasMoreElements()) { CommPortIdentifier identifier = (CommPortIdentifier) identifiers.nextElement(); if (identifier.getPortType() == CommPortIdentifier.PORT_SERIAL) { portNames.add(identifier.getName()); } } return portNames; }
// search for all the serial ports // pre: none // post: adds all the found ports to a combo box on the GUI public void searchForPorts() { ports = CommPortIdentifier.getPortIdentifiers(); while (ports.hasMoreElements()) { CommPortIdentifier curPort = (CommPortIdentifier) ports.nextElement(); // get only serial ports if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL) { portMap.put(curPort.getName(), curPort); } } }
/** * @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently * being used. */ @SuppressWarnings({"rawtypes"}) private static HashSet<CommPortIdentifier> getAvailableSerialPorts() { HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>(); Enumeration thePorts = CommPortIdentifier.getPortIdentifiers(); while (thePorts.hasMoreElements()) { CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement(); switch (com.getPortType()) { case CommPortIdentifier.PORT_SERIAL: try { CommPort thePort = com.open("CommUtil", 50); thePort.close(); h.add(com); } catch (PortInUseException e) { System.out.println("Port, " + com.getName() + ", is in use."); } catch (Exception e) { System.err.println("Failed to open port " + com.getName()); e.printStackTrace(); } } } return h; }
// find all available serial ports for the settings->ports menu. public String[] ListSerialPorts() { @SuppressWarnings("unchecked") Enumeration<CommPortIdentifier> ports = (Enumeration<CommPortIdentifier>) CommPortIdentifier.getPortIdentifiers(); ArrayList<String> portList = new ArrayList<String>(); while (ports.hasMoreElements()) { CommPortIdentifier port = (CommPortIdentifier) ports.nextElement(); if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) { portList.add(port.getName()); } } portsDetected = (String[]) portList.toArray(new String[0]); return portsDetected; }
public boolean oeffneSerialPort() { Boolean foundPort = false; if (serialPortGeoeffnet != false) { System.out.println("Serialport bereits geöffnet"); return false; } System.out.println("Öffne Serialport"); enumComm = CommPortIdentifier.getPortIdentifiers(); while (enumComm.hasMoreElements()) { serialPortId = (CommPortIdentifier) enumComm.nextElement(); if (portName.contentEquals(serialPortId.getName())) { foundPort = true; break; } } if (foundPort != true) { System.out.println("Serialport nicht gefunden: " + portName); return false; } try { serialPort = (SerialPort) serialPortId.open("Öffnen und Senden", 500); } catch (PortInUseException e) { System.out.println("Port belegt"); } try { outputStream = serialPort.getOutputStream(); } catch (IOException e) { System.out.println("Keinen Zugriff auf OutputStream"); } try { inputStream = serialPort.getInputStream(); } catch (IOException e) { System.out.println("Keinen Zugriff auf InputStream"); } serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(baudrate, dataBits, stopBits, parity); } catch (UnsupportedCommOperationException e) { System.out.println("Konnte Schnittstellen-Paramter nicht setzen"); } reader = new BufferedReader(new InputStreamReader(inputStream)); writer = new BufferedWriter(new OutputStreamWriter(outputStream)); serialPortGeoeffnet = true; return true; }