/** * Start modem command manager. * * @throws Exception the exception */ public void startModemCommandManager() throws Exception { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(this.commandPortName); if (portIdentifier.isCurrentlyOwned()) { logger.error("Serial port " + this.commandPortName + " is currently in use"); } CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000); if (commPort instanceof SerialPort) { modemComPort = (SerialPort) commPort; modemComPort.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); modemComPort.notifyOnDataAvailable(true); commandInputStream = new DataInputStream(modemComPort.getInputStream()); serialCommandReader = new SerialCommandReader(commandInputStream, this); modemComPort.addEventListener(serialCommandReader); commandOutputStream = new DataOutputStream(modemComPort.getOutputStream()); this.sendCommandToModem(ATCommands.DISABLE_ECHO); this.sendCommandToModem(ATCommands.DISABLE_DIAGNOSTIC); this.sendCommandToModem(ATCommands.END_CALL); this.sendCommandToModem(ATCommands.CHECK_PIN); } }
/** * Gets the serial port on the given port name. Throws an {@link IOException} if it does not * succeed in doing so. * * @param portName The port name. * @return The port. * @throws IOException If the port could not be opened. */ private final SerialPort getSerialPort(final String portName) throws IOException { try { final CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(portName); if (identifier.isCurrentlyOwned()) { throw new IOException( "The port specified [" + portName + "] is already in use by [" + identifier.getCurrentOwner() + "] !"); } final CommPort rawPort = identifier.open("DOM_SER_DRV", 2000); if (rawPort instanceof SerialPort) { final SerialPort port = (SerialPort) rawPort; port.setSerialPortParams( BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); return port; } else { throw new IOException("Port [" + portName + "] is not a serial port !"); } } catch (NoSuchPortException e) { throw new IOException("The port specified [" + portName + "] does not exist !", e); } catch (PortInUseException e) { throw new IOException("The port specified [" + portName + "] is already in use !"); } catch (UnsupportedCommOperationException e) { throw new IOException("Unsupported comm operation when setting the serial parameters !", e); } }
/** * Start call. * * @throws Exception the exception */ public void startCall() throws Exception { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(this.streamPortName); if (portIdentifier.isCurrentlyOwned()) { logger.error("Serial port " + this.streamPortName + " is currently in use"); } CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000); if (commPort instanceof SerialPort) { voiceStreamCommPort = (SerialPort) commPort; voiceStreamCommPort.setSerialPortParams( 230400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); voiceInputStream = new DataInputStream(voiceStreamCommPort.getInputStream()); voiceOutputStream = new DataOutputStream(voiceStreamCommPort.getOutputStream()); serialVoiceReader = new SerialVoiceReader(voiceInputStream, af); serialVoiceWriter = new SerialVoiceWriter(voiceOutputStream, af, playMessage); (new Thread(serialVoiceReader, "VoiceReader")).start(); (new Thread(serialVoiceWriter, "VoiceWriter")).start(); } }
public CommDriverDS2480B(CommPortIdentifier identifier) throws PortInUseException { System.out.println(String.format("Opening %s", identifier.getName())); if (identifier.isCurrentlyOwned()) throw new IllegalStateException("Comm port owned by someone else"); CommPort unknownPort = identifier.open(this.getClass().getName(), TIMEOUT_OPEN_MILLIS); if (!(unknownPort instanceof SerialPort)) throw new IllegalStateException("not a serial port"); this.port = (SerialPort) unknownPort; try { // this.port.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); this.port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); // flashRTS(); input = new DataInputStream(new DebugInputStream(debug, this.port.getInputStream())); output = new DataOutputStream(new DebugOutputStream(debug, this.port.getOutputStream())); // cause software reset by using PARITY_SPACE for one byte... System.out.println("initial hardware reset/clock sync"); this.port.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_SPACE); output.write(0x00); Thread.sleep(100); // send sync (reset) pulse for clock calibration this.port.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); output.write(Integer.parseInt("11000001", 2)); output.flush(); Thread.sleep(50); // note, no response byte generated } catch (UnsupportedCommOperationException e) { throw new IllegalStateException("port did not accept params", e); } catch (IOException e) { throw new IllegalStateException("port did not accept params", e); } catch (InterruptedException e) { throw new IllegalStateException("port did not accept params", e); } }
public CardReader(String portName) throws IOException, PortInUseException, UnsupportedCommOperationException, NoSuchPortException { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if (portIdentifier.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use"); } else { int timeout = 2000; CommPort commPort = portIdentifier.open(this.getClass().getName(), timeout); if (commPort instanceof SerialPort) { SerialPort serialPort = (SerialPort) commPort; serialPort.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); in = serialPort.getInputStream(); } else { System.out.println("Error: Only serial ports are handled by this example."); } } }
private SerialPort connectSerial(String portName) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException { SerialPort serialPort = null; CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if (portIdentifier.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000); if (commPort instanceof SerialPort) { serialPort = (SerialPort) commPort; serialPort.setSerialPortParams( 57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, // SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } else { System.out.println("Error: Only serial ports are handled by this example."); } } return serialPort; }
/** @see com.svhelloworld.knotlog.engine.sources.StreamedSource#open() */ @Override public InputStream open() { try { // make sure the specified port is available CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(config.getName()); if (identifier.isCurrentlyOwned()) { throw new SerialPortOccupiedException( config.getName() + " port is currently opened by another application"); } // open up the port CommPort port = identifier.open(getOwnerName(), PORT_TIMEOUT); if (port instanceof SerialPort) { // configure serial port serialPort = (SerialPort) port; serialPort.setSerialPortParams( config.getBaudRate(), config.getDataBits().getRXTXConstant(), config.getStopBit().getRXTXConstant(), config.getParity().getRXTXConstant()); /* * the following 2 lines fix the exception: * "IOException: Underlying input stream returned zero bytes" * that is thrown when we read from the InputStream. */ serialPort.enableReceiveTimeout(PORT_TIMEOUT); serialPort.enableReceiveThreshold(0); InputStream is = serialPort.getInputStream(); return is; } // not a serial port throw new SerialPortException("must operate against a serial port"); } catch (PortInUseException e) { throw new SerialPortOccupiedException(e.getMessage(), e); } catch (Exception e) { throw new SerialPortException(e.getMessage(), e); } }
void connect(String portName) throws Exception { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if (portIdentifier.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000); if (commPort instanceof SerialPort) { SerialPort mSerialPort = (SerialPort) commPort; mSerialPort.setSerialPortParams( 57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); InputStream mIn = mSerialPort.getInputStream(); OutputStream mOut = mSerialPort.getOutputStream(); (new Thread(new SerialReader(mIn))).start(); (new Thread(new SerialWriter(mOut))).start(); } else { System.out.println("Error: Only serial ports are handled by this example."); } } }
public static void main(String[] args) { boolean boolArgs = false; if ((args.length == 0) || (args.length == 1)) { log.error( MARKER_RunPcOpRs1Sender, "{} please input device name (e.g. /dev/ttyUSB0) and input file", Thread.currentThread().getStackTrace()[1].getMethodName()); } else if (args.length > 2) { log.error( MARKER_RunPcOpRs1Sender, "{} too many args", Thread.currentThread().getStackTrace()[1].getMethodName()); } else { boolArgs = true; } if (boolArgs) { String deviceName = args[0]; // String deviceName = "/dev/ttyUSB0"; log.debug( MARKER_RunPcOpRs1Sender, "{} [deviceName] {}", Thread.currentThread().getStackTrace()[1].getMethodName(), deviceName); String signalPath = args[1]; log.debug( MARKER_RunPcOpRs1Sender, "{} [signalPath] {}", Thread.currentThread().getStackTrace()[1].getMethodName(), signalPath); CommPortIdentifier portID; // SerialPort port; @SuppressWarnings("unchecked") Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portID = (CommPortIdentifier) portList.nextElement(); log.debug( MARKER_RunPcOpRs1Sender, "{} [portID.getName()] {}", Thread.currentThread().getStackTrace()[1].getMethodName(), portID.getName()); if (portID.getPortType() == CommPortIdentifier.PORT_SERIAL) { log.debug( MARKER_RunPcOpRs1Sender, "{} {} is a serial device", Thread.currentThread().getStackTrace()[1].getMethodName(), deviceName); if (portID.getName().equals(deviceName)) { if (portID.isCurrentlyOwned()) { log.debug( MARKER_RunPcOpRs1Sender, "{} {} is currently owned", Thread.currentThread().getStackTrace()[1].getMethodName(), deviceName); break; } else { log.debug( MARKER_RunPcOpRs1Sender, "{} {} can be used", Thread.currentThread().getStackTrace()[1].getMethodName(), deviceName); PcOpRs1Sender rxtx = new PcOpRs1Sender(deviceName, signalPath); if (rxtx.openSerialPort()) { if (rxtx.setSerialPort()) { rxtx.send0x74(); } else { break; } } else { break; } } } else { log.debug( MARKER_RunPcOpRs1Sender, "{} {} is not match {}", Thread.currentThread().getStackTrace()[1].getMethodName(), portID.getName(), deviceName); } } else if (portID.getPortType() == CommPortIdentifier.PORT_PARALLEL) { log.debug( MARKER_RunPcOpRs1Sender, "{} {} is a parallel device", Thread.currentThread().getStackTrace()[1].getMethodName(), deviceName); break; } else { log.debug( MARKER_RunPcOpRs1Sender, "{} {} is an unknown device", Thread.currentThread().getStackTrace()[1].getMethodName(), deviceName); break; } } // while() } // if bool } // main()
// open a serial connection to a device. We won't know it's the robot until public int OpenPort(String portName) { if (portOpened && portName.equals(recentPort)) return 0; ClosePort(); Log("<font color='green'>Connecting to " + portName + "...</font>\n"); // find the port try { portIdentifier = CommPortIdentifier.getPortIdentifier(portName); } catch (Exception e) { Log("<span style='color:red'>Ports could not be identified:" + e.getMessage() + "</span>\n"); e.printStackTrace(); return 1; } if (portIdentifier.isCurrentlyOwned()) { Log( "<span style='color:red'>Error: Another program is currently using this port." + "</span>\n"); return 2; } // open the port try { commPort = portIdentifier.open("DrawbotGUI", 2000); } catch (Exception e) { Log("Port could not be opened:" + e.getMessage() + NL); e.printStackTrace(); return 3; } if ((commPort instanceof SerialPort) == false) { Log("<span style='color:red'>Only serial ports are handled by this example." + "</span>\n"); return 4; } // set the port parameters (like baud rate) serialPort = (SerialPort) commPort; try { serialPort.setSerialPortParams( BAUD_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (Exception e) { Log("<span style='color:red'>Port could not be configured:" + e.getMessage() + "</span>\n"); return 5; } try { in = serialPort.getInputStream(); out = serialPort.getOutputStream(); } catch (Exception e) { Log("<span style='color:red'>Streams could not be opened:" + e.getMessage() + "</span>\n"); return 6; } try { serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); } catch (TooManyListenersException e) { Log("<span style='color:red'>Streams could not be opened:" + e.getMessage() + "</span>\n"); return 7; } Log("<span style='green'>Opened.</span>\n"); SetRecentPort(portName); portOpened = true; UpdateMenuBar(); return 0; }