예제 #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);
    }
  }
예제 #2
0
  /**
   * Opens the communication port.
   *
   * @throws Exception if an error occurs.
   */
  public void open() throws Exception {

    // If this is Linux then first of all we need to check that
    // device file exists. Otherwise call to m_PortIdentifyer.open()
    // method will crash JVM.
    // It is ugly patch but it is too late...
    if (SystemUtils.IS_OS_LINUX) {
      File portDevFile = new File(m_Parameters.getPortName());

      if (!portDevFile.exists()) {
        throw new Exception(
            "Modbus serial device " + m_Parameters.getPortName() + " doesn't exist!");
      }
    }

    // 1. obtain a CommPortIdentifier instance
    try {
      m_PortIdentifyer = CommPortIdentifier.getPortIdentifier(m_Parameters.getPortName());
    } catch (NoSuchPortException e) {
      final String errMsg =
          "Could not get port identifier, maybe insufficient permissions. " + e.getMessage();
      logger.debug(errMsg);
      throw new Exception(errMsg);
    }
    logger.trace("Got Port Identifier");

    // 2. open the port, wait for given timeout
    try {
      m_SerialPort = (SerialPort) m_PortIdentifyer.open("Modbus Serial Master", 30000);
    } catch (PortInUseException e) {
      logger.debug("open port failed: " + e.getMessage());

      throw new Exception(e.getMessage());
    }
    logger.trace("Got Serial Port");

    // 3. set the parameters
    try {
      setConnectionParameters();
    } catch (Exception e) {
      // ensure it is closed
      m_SerialPort.close();
      logger.debug("parameter setup failed: " + e.getMessage());
      throw e;
    }

    if (Modbus.SERIAL_ENCODING_ASCII.equals(m_Parameters.getEncoding())) {
      m_Transport = new ModbusASCIITransport();
    } else if (Modbus.SERIAL_ENCODING_RTU.equals(m_Parameters.getEncoding())) {
      m_Transport = new ModbusRTUTransport();
      setReceiveTimeout(m_Parameters.getReceiveTimeout()); // just here for the moment.
    } else if (Modbus.SERIAL_ENCODING_BIN.equals(m_Parameters.getEncoding())) {
      m_Transport = new ModbusBINTransport();
    }
    m_Transport.setEcho(m_Parameters.isEcho());

    // Open the input and output streams for the connection. If they won't
    // open, close the port before throwing an exception.
    try {
      m_SerialIn = m_SerialPort.getInputStream();
      m_Transport.setCommPort(m_SerialPort);
      //       m_Transport.prepareStreams(m_SerialIn,
      //                                  m_SerialPort.getOutputStream());
    } catch (IOException e) {
      m_SerialPort.close();
      logger.debug(e.getMessage());

      throw new Exception("Error opening i/o streams");
    }
    logger.trace("i/o Streams prepared");

    // Add this object as an event listener for the serial port.
    try {
      m_SerialPort.addEventListener(this);
    } catch (TooManyListenersException e) {
      m_SerialPort.close();
      final String errMsg = "too many listeners added";
      logger.debug("{}: {}", errMsg, e.getMessage());
      throw new Exception(errMsg);
    }

    // Set notifyOnBreakInterrup to allow event driven break handling.
    m_SerialPort.notifyOnBreakInterrupt(true);

    m_Open = true;
  } // open