Search in sources :

Example 1 with ModbusRTUTransport

use of net.wimpi.modbus.io.ModbusRTUTransport in project openhab1-addons by openhab.

the class SerialConnection method open.

// getModbusTransport
/**
     * Opens the communication port.
     *
     * @throws Exception if an error occurs.
     */
public void open() throws Exception {
    if (isOpen()) {
        return;
    }
    // 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.error(errMsg);
        throw new Exception(errMsg);
    }
    logger.trace("Got Port Identifier");
    // 2. open the port, wait for given timeout
    try {
        m_SerialPort = m_PortIdentifyer.open("Modbus Serial Master", 30000);
    } catch (PortInUseException e) {
        String msg = "open port failed: " + e.getMessage();
        logger.error(msg);
        throw new Exception(msg);
    }
    logger.trace("Got Serial Port");
    // 3. set the parameters
    try {
        setConnectionParameters();
    } catch (Exception e) {
        // ensure it is closed
        m_SerialPort.close();
        logger.error("parameter setup failed: " + e.getMessage());
        throw e;
    }
    setReceiveTimeout(m_Parameters.getReceiveTimeoutMillis());
    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();
    } else if (Modbus.SERIAL_ENCODING_BIN.equals(m_Parameters.getEncoding())) {
        m_Transport = new ModbusBINTransport();
    }
    m_Transport.setEcho(m_Parameters.isEcho());
    // 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();
        String msg = "Error opening i/o streams: " + e.getMessage();
        logger.error(msg);
        throw new Exception(msg);
    }
    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: " + e.getMessage();
        logger.error(errMsg);
        throw new Exception(errMsg);
    }
    // Set notifyOnBreakInterrup to allow event driven break handling.
    m_SerialPort.notifyOnBreakInterrupt(true);
    m_Open = true;
}
Also used : TooManyListenersException(java.util.TooManyListenersException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) ModbusBINTransport(net.wimpi.modbus.io.ModbusBINTransport) ModbusASCIITransport(net.wimpi.modbus.io.ModbusASCIITransport) ModbusRTUTransport(net.wimpi.modbus.io.ModbusRTUTransport) IOException(java.io.IOException) File(java.io.File) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) IOException(java.io.IOException) NoSuchPortException(gnu.io.NoSuchPortException)

Aggregations

NoSuchPortException (gnu.io.NoSuchPortException)1 PortInUseException (gnu.io.PortInUseException)1 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)1 File (java.io.File)1 IOException (java.io.IOException)1 TooManyListenersException (java.util.TooManyListenersException)1 ModbusASCIITransport (net.wimpi.modbus.io.ModbusASCIITransport)1 ModbusBINTransport (net.wimpi.modbus.io.ModbusBINTransport)1 ModbusRTUTransport (net.wimpi.modbus.io.ModbusRTUTransport)1