Ejemplo n.º 1
0
  /** @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);
    }
  }
Ejemplo n.º 2
0
  /**
   * Opens the Operation System Serial Port
   *
   * <p>This method opens the port and set Serial Port parameters according to the DSMR
   * specification. Since the specification is clear about these parameters there are not
   * configurable.
   *
   * <p>If there are problem while opening the port, it is the responsibility of the calling method
   * to handle this situation (and for example close the port again).
   *
   * <p>Opening an already open port is harmless. The method will return immediately
   *
   * @return true if opening was successful (or port was already open), false otherwise
   */
  private boolean open() {
    synchronized (portLock) {
      // Sanity check
      if (portState != PortState.CLOSED) {
        return true;
      }

      try {
        // Opening Operating System Serial Port
        logger.debug("Creating CommPortIdentifier");
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        logger.debug("Opening CommPortIdentifier");
        CommPort commPort = portIdentifier.open("org.openhab.binding.dsmr", readTimeoutMSec);
        logger.debug("Configure serial port");
        serialPort = (SerialPort) commPort;
        serialPort.enableReceiveThreshold(1);
        serialPort.enableReceiveTimeout(readTimeoutMSec);

        // Configure Serial Port based on specified port speed
        logger.debug("Configure serial port speed " + portSpeed);
        switch (portSpeed) {
          case LOW_SPEED:
            serialPort.setSerialPortParams(
                9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
            serialPort.setDTR(false);
            serialPort.setRTS(true);

            break;
          case HIGH_SPEED:
            serialPort.setSerialPortParams(
                115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            break;
          default:
            logger.error("Invalid speed, closing port");

            return false;
        }
      } catch (NoSuchPortException nspe) {
        logger.error("Could not open port: " + portName, nspe);

        return false;
      } catch (PortInUseException piue) {
        logger.error("Port already in use: " + portName, piue);

        return false;
      } catch (UnsupportedCommOperationException ucoe) {
        logger.error("Port is not suitable: " + portName, ucoe);

        return false;
      }

      // SerialPort is ready, open the reader
      logger.info("SerialPort opened successful");
      try {
        bis = new BufferedInputStream(serialPort.getInputStream());
      } catch (IOException ioe) {
        logger.error("Failed to get inputstream for serialPort. Closing port", ioe);

        return false;
      }
      logger.info("DSMR Port opened successful");

      return true;
    }
  }