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();
    }
  }
Exemplo n.º 2
0
    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);
    }
Exemplo n.º 3
0
    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);
      }
    }