private void openPort() { String port = (String) m_properties.get(SERIAL_DEVICE_PROP_NAME); if (port == null) { s_logger.info("Port name not configured"); return; } int baudRate = Integer.valueOf((String) m_properties.get(SERIAL_BAUDRATE_PROP_NAME)); int dataBits = Integer.valueOf((String) m_properties.get(SERIAL_DATA_BITS_PROP_NAME)); int stopBits = Integer.valueOf((String) m_properties.get(SERIAL_STOP_BITS_PROP_NAME)); String sParity = (String) m_properties.get(SERIAL_PARITY_PROP_NAME); int parity = CommURI.PARITY_NONE; if (sParity.equals("none")) { parity = CommURI.PARITY_NONE; } else if (sParity.equals("odd")) { parity = CommURI.PARITY_ODD; } else if (sParity.equals("even")) { parity = CommURI.PARITY_EVEN; } String uri = new CommURI.Builder(port) .withBaudRate(baudRate) .withDataBits(dataBits) .withStopBits(stopBits) .withParity(parity) .withTimeout(1000) .build() .toString(); try { m_commConnection = (CommConnection) m_connectionFactory.createConnection(uri, 1, false); m_commIs = m_commConnection.openInputStream(); m_commOs = m_commConnection.openOutputStream(); // m_commBr = new BufferedReader(new InputStreamReader(m_commIs)); // m_commBw = new BufferedWriter(new OutputStreamWriter(m_commOs)); s_logger.info(port + " open"); } catch (IOException e) { s_logger.error("Failed to open port", e); cleanupPort(); } }
private void cleanupPort() { // if (m_commBr != null) { // try { // m_commBr.close(); // } catch (IOException e) { // s_logger.error("Cannot close port buffered reader", e); // } // m_commBr = null; // } // if (m_commBw != null) { // try { // m_commBw.close(); // } catch (IOException e) { // s_logger.error("Cannot close port buffered writer", e); // } // m_commBw = null; // } if (m_commIs != null) { try { s_logger.info("Closing port input stream..."); m_commIs.close(); s_logger.info("Closed port input stream"); } catch (IOException e) { s_logger.error("Cannot close port input stream", e); } m_commIs = null; } if (m_commOs != null) { try { s_logger.info("Closing port output stream..."); m_commOs.close(); s_logger.info("Closed port output stream"); } catch (IOException e) { s_logger.error("Cannot close port output stream", e); } m_commOs = null; } if (m_commConnection != null) { try { s_logger.info("Closing port..."); m_commConnection.close(); s_logger.info("Closed port"); } catch (IOException e) { s_logger.error("Cannot close port", e); } m_commConnection = null; } }
public void disconnect() throws ModbusProtocolException { if (conn != null) { try { conn.close(); s_logger.debug("Serial connection closed"); } catch (IOException e) { throw new ModbusProtocolException( ModbusProtocolErrorCode.TRANSACTION_FAILURE, e.getMessage()); } conn = null; } if (m_serial485) { if (gpioModeSwitch != null) { try { gpioModeSwitch.close(); } catch (IOException e) { throw new ModbusProtocolException( ModbusProtocolErrorCode.TRANSACTION_FAILURE, e.getMessage()); } } } }
public void disconnect() { if ((m_task != null) && (!m_task.isDone())) { s_logger.debug("disconnect() :: Cancelling GpsSerialCommunicate task ..."); m_task.cancel(true); s_logger.info("disconnect() :: GpsSerialCommunicate task cancelled? = {}", m_task.isDone()); m_task = null; } if (m_executor != null) { s_logger.debug("disconnect() :: Terminating GpsSerialCommunicate Thread ..."); m_executor.shutdownNow(); try { m_executor.awaitTermination(THREAD_TERMINATION_TOUT, TimeUnit.SECONDS); } catch (InterruptedException e) { s_logger.warn("Interrupted", e); } s_logger.info( "disconnect() :: GpsSerialCommunicate Thread terminated? - {}", m_executor.isTerminated()); m_executor = null; } if (conn != null) { try { if (in != null) { in.close(); in = null; } conn.close(); } catch (IOException e) { e.printStackTrace(); } conn = null; } }
public SerialCommunicate(ConnectionFactory connFactory, Properties connectionConfig) throws PositionException { s_logger.debug("Configure serial connection"); connConfig = connectionConfig; String sPort; String sBaud; String sStop; String sParity; String sBits; if (((sPort = connectionConfig.getProperty("port")) == null) || ((sBaud = connectionConfig.getProperty("baudRate")) == null) || ((sStop = connectionConfig.getProperty("stopBits")) == null) || ((sParity = connectionConfig.getProperty("parity")) == null) || ((sBits = connectionConfig.getProperty("bitsPerWord")) == null)) throw new PositionException("Invalid serial port configuration"); int baud = Integer.valueOf(sBaud).intValue(); int stop = Integer.valueOf(sStop).intValue(); int parity = Integer.valueOf(sParity).intValue(); int bits = Integer.valueOf(sBits).intValue(); String uri = new CommURI.Builder(sPort) .withBaudRate(baud) .withDataBits(bits) .withStopBits(stop) .withParity(parity) .withTimeout(2000) .build() .toString(); try { conn = (CommConnection) connFactory.createConnection(uri, 1, false); } catch (IOException e1) { throw new PositionException("Invalid GPS serial Port", e1); } // get the streams try { in = conn.openInputStream(); conn.openOutputStream(); } catch (Exception e) { throw new PositionException("input stream", e); } // clean up if this is not our first run if ((m_task != null) && (!m_task.isDone())) { s_logger.debug("SerialCommunicate() :: Cancelling GpsSerialCommunicate task ..."); m_task.cancel(true); s_logger.info( "SerialCommunicate() :: GpsSerialCommunicate task cancelled? = {}", m_task.isDone()); m_task = null; } m_executor = Executors.newSingleThreadScheduledExecutor(); m_task = m_executor.scheduleAtFixedRate( new Runnable() { @Override public void run() { Thread.currentThread().setName("GpsSerialCommunicate"); doPollWork(); } }, 0, 20, TimeUnit.MILLISECONDS); }
public SerialCommunicate(ConnectionFactory connFactory, Properties connectionConfig) throws ModbusProtocolException { s_logger.debug("Configure serial connection"); String sPort; String sBaud; String sStop; String sParity; String sBits; String gpioSwitchPin = null; String gpioRsModePin = null; if (((sPort = connectionConfig.getProperty("port")) == null) || ((sBaud = connectionConfig.getProperty("baudRate")) == null) || ((sStop = connectionConfig.getProperty("stopBits")) == null) || ((sParity = connectionConfig.getProperty("parity")) == null) || ((sBits = connectionConfig.getProperty("bitsPerWord")) == null)) throw new ModbusProtocolException(ModbusProtocolErrorCode.INVALID_CONFIGURATION); int baud = Integer.valueOf(sBaud).intValue(); int stop = Integer.valueOf(sStop).intValue(); int parity = Integer.valueOf(sParity).intValue(); int bits = Integer.valueOf(sBits).intValue(); if (connectionConfig.getProperty("serialMode") != null) { m_serial485 = (connectionConfig.getProperty("serialMode") == SERIAL_485); if (m_serial485) { if (((gpioSwitchPin = connectionConfig.getProperty("serialGPIOswitch")) == null) || ((gpioRsModePin = connectionConfig.getProperty("serialGPIOrsmode")) == null)) throw new ModbusProtocolException(ModbusProtocolErrorCode.INVALID_CONFIGURATION); } } String uri = new CommURI.Builder(sPort) .withBaudRate(baud) .withDataBits(bits) .withStopBits(stop) .withParity(parity) .withTimeout(2000) .build() .toString(); try { conn = (CommConnection) connFactory.createConnection(uri, 1, false); } catch (IOException e1) { throw new ModbusProtocolException(ModbusProtocolErrorCode.CONNECTION_FAILURE, e1); } if (m_serial485) { setupSerialGpio(gpioRsModePin, gpioSwitchPin); } // get the streams try { in = conn.openInputStream(); out = conn.openOutputStream(); if (m_serial485) gpioModeSwitch = new FileWriter(new File("/sys/class/gpio/" + gpioSwitchPin + "/value")); } catch (Exception e) { throw new ModbusProtocolException(ModbusProtocolErrorCode.CONNECTION_FAILURE, e); } }