Search in sources :

Example 11 with ModbusIOException

use of com.ghgande.j2mod.modbus.ModbusIOException in project OpenMUC by isc-konstanz.

the class ModbusRTUTCPTransport method readResponse.

// readRequest
@Override
public synchronized /**
 * Lazy implementation: avoid CRC validation...
 */
ModbusResponse readResponse() throws ModbusIOException {
    // the received response
    ModbusResponse response = null;
    // reset the timed out flag
    this.isTimedOut = false;
    // init and start the timeout timer
    this.readTimeoutTimer = new Timer();
    this.readTimeoutTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            isTimedOut = true;
        }
    }, this.readTimeout);
    try {
        // atomic access to the input buffer
        synchronized (inputBuffer) {
            // clean the input buffer
            inputBuffer.reset(new byte[Modbus.MAX_MESSAGE_LENGTH]);
            // sleep for the time needed to receive the first part of the
            // response
            int available = this.inputStream.available();
            while ((available < 4) && (!this.isTimedOut)) {
                // 1ms * #bytes (4bytes in the worst case)
                Thread.yield();
                available = this.inputStream.available();
            // if (logger.isTraceEnabled()) {
            // logger.trace("Available bytes: " + available);
            // }
            }
            // check if timedOut
            if (this.isTimedOut) {
                throw new ModbusIOException("I/O exception - read timeout.\n");
            }
            // get a reference to the inner byte buffer
            byte[] inBuffer = this.inputBuffer.getBuffer();
            // read the first 2 bytes from the input stream
            this.inputStream.read(inBuffer, 0, 2);
            // this.inputStream.readFully(inBuffer);
            // read the progressive id
            int packetId = inputBuffer.readUnsignedByte();
            if (logger.isTraceEnabled()) {
                logger.trace(ModbusRTUTCPTransport.logId + "Read packet with progressive id: " + packetId);
            }
            // read the function code
            int functionCode = inputBuffer.readUnsignedByte();
            if (logger.isTraceEnabled()) {
                logger.trace(" uid: " + packetId + ", function code: " + functionCode);
            }
            // compute the number of bytes composing the message (including
            // the CRC = 2bytes)
            int packetLength = computePacketLength(functionCode);
            // response
            while ((this.inputStream.available() < (packetLength - 3)) && (!this.isTimedOut)) {
                try {
                    inputBuffer.wait(10);
                } catch (InterruptedException ie) {
                    // do nothing
                    System.err.println("Sleep interrupted while waiting for response body...\n" + ie);
                }
            }
            // check if timedOut
            if (this.isTimedOut) {
                throw new ModbusIOException("I/O exception - read timeout.\n");
            }
            // read the remaining bytes
            this.inputStream.read(inBuffer, 3, packetLength);
            if (logger.isTraceEnabled()) {
                logger.trace(" bytes: " + ModbusUtil.toHex(inBuffer, 0, packetLength) + ", desired length: " + packetLength);
            }
            // compute the CRC
            int[] crc = ModbusUtil.calculateCRC(inBuffer, 0, packetLength - 2);
            // check the CRC against the received one...
            if (ModbusUtil.unsignedByteToInt(inBuffer[packetLength - 2]) != crc[0] || ModbusUtil.unsignedByteToInt(inBuffer[packetLength - 1]) != crc[1]) {
                throw new IOException("CRC Error in received frame: " + packetLength + " bytes: " + ModbusUtil.toHex(inBuffer, 0, packetLength));
            }
            // reset the input buffer to the given packet length (excluding
            // the CRC)
            this.inputBuffer.reset(inBuffer, packetLength - 2);
            // create the response
            response = ModbusResponse.createModbusResponse(functionCode);
            response.setHeadless();
            // read the response
            response.readFrom(inputBuffer);
        }
    } catch (IOException e) {
        // debug
        System.err.println(ModbusRTUTCPTransport.logId + "Error while reading from socket: " + e);
        // clean the input stream
        try {
            while (this.inputStream.read() != -1) {
                ;
            }
        } catch (IOException e1) {
            // debug
            System.err.println(ModbusRTUTCPTransport.logId + "Error while emptying input buffer from socket: " + e);
        }
        // wrap and re-throw
        throw new ModbusIOException("I/O exception - failed to read.\n" + e);
    }
    // reset the timeout timer
    this.readTimeoutTimer.cancel();
    // return the response read from the socket stream
    return response;
/*-------------------------- SERIAL IMPLEMENTATION -----------------------------------
        
        try
        {
            do
            {
                // block the input stream
                synchronized (byteInputStream)
                {
                    // get the packet uid
                    int uid = inputStream.read();
                    
                    if (Modbus.debug)
                        System.out.println(ModbusRTUTCPTransport.logId + "UID: " + uid);
                    
                    // if the uid is valid (i.e., > 0) continue
                    if (uid != -1)
                    {
                        // get the function code
                        int fc = inputStream.read();
                        
                        if (Modbus.debug)
                            System.out.println(ModbusRTUTCPTransport.logId + "Function code: " + uid);
                        
                        //bufferize the response
                        byteOutputStream.reset();
                        byteOutputStream.writeByte(uid);
                        byteOutputStream.writeByte(fc);
                        
                        // create the Modbus Response object to acquire length of message
                        response = ModbusResponse.createModbusResponse(fc);
                        response.setHeadless();
                        
                        // With Modbus RTU, there is no end frame. Either we
                        // assume the message is complete as is or we must do
                        // function specific processing to know the correct length.
                        
                        //bufferize the response according to the given function code
                        getResponse(fc, byteOutputStream);
                        
                        //compute the response length without considering the CRC
                        dlength = byteOutputStream.size() - 2; // less the crc
                        
                        //debug
                        if (Modbus.debug)
                            System.out.println("Response: "
                                    + ModbusUtil.toHex(byteOutputStream.getBuffer(), 0, dlength + 2));
                        
                        //TODO: check if needed (restore the buffer state, cursor at 0, same content)
                        byteInputStream.reset(inputBuffer, dlength);
                        
                        // cmopute the buffer CRC
                        int[] crc = ModbusUtil.calculateCRC(inputBuffer, 0, dlength);
                        
                        // check the CRC against the received one...
                        if (ModbusUtil.unsignedByteToInt(inputBuffer[dlength]) != crc[0]
                                || ModbusUtil.unsignedByteToInt(inputBuffer[dlength + 1]) != crc[1])
                        {
                            throw new IOException("CRC Error in received frame: " + dlength + " bytes: "
                                    + ModbusUtil.toHex(byteInputStream.getBuffer(), 0, dlength));
                        }
                    }
                    else
                    {
                        throw new IOException("Error reading response");
                    }
                    
                    // restore the buffer state, cursor at 0, same content
                    byteInputStream.reset(inputBuffer, dlength);
                    
                    //actually read the response
                    if (response != null)
                    {
                        response.readFrom(byteInputStream);
                    }
                    
                    //flag completion...
                    done = true;
                    
                }// synchronized
            }
            while (!done);
            return response;
        }
        catch (Exception ex)
        {
            System.err.println("Last request: " + ModbusUtil.toHex(lastRequest));
            System.err.println(ex.getMessage());
            throw new ModbusIOException("I/O exception - failed to read");
        }
        
        ------------------------------------------------------------------------------*/
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) ModbusResponse(com.ghgande.j2mod.modbus.msg.ModbusResponse) IOException(java.io.IOException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException)

Example 12 with ModbusIOException

use of com.ghgande.j2mod.modbus.ModbusIOException in project OpenMUC by isc-konstanz.

the class ModbusConnection method readChannelGroupHighLevel.

public Object readChannelGroupHighLevel(List<ChannelRecordContainer> containers, Object containerListHandle, String samplingGroup) throws ConnectionException {
    // NOTE: containerListHandle is null if something changed in configuration!!!
    ModbusChannelGroup channelGroup = null;
    // use existing channelGroup
    if (containerListHandle != null) {
        if (containerListHandle instanceof ModbusChannelGroup) {
            channelGroup = (ModbusChannelGroup) containerListHandle;
        }
    }
    // create new channelGroup
    if (channelGroup == null) {
        ArrayList<ModbusChannel> channelList = new ArrayList<>();
        for (ChannelRecordContainer container : containers) {
            channelList.add(getModbusChannel(container.getChannelAddress(), EAccess.READ));
        }
        channelGroup = new ModbusChannelGroup(samplingGroup, channelList);
    }
    // read all channels of the group
    try {
        readChannelGroup(channelGroup, containers);
    } catch (ModbusIOException e) {
        logger.error("ModbusIOException while reading samplingGroup:" + samplingGroup, e);
        disconnect();
        throw new ConnectionException(e);
    } catch (ModbusException e) {
        logger.error("Unable to read ChannelGroup", e);
        // set channel values and flag, otherwise the datamanager will throw a null pointer exception
        // and the framework collapses.
        setChannelsWithErrorFlag(containers);
    }
    return channelGroup;
}
Also used : ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) ArrayList(java.util.ArrayList) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Aggregations

ModbusIOException (com.ghgande.j2mod.modbus.ModbusIOException)12 ModbusException (com.ghgande.j2mod.modbus.ModbusException)8 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)7 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)6 ScanException (org.openmuc.framework.config.ScanException)6 ModbusChannel (org.openmuc.framework.driver.modbus.ModbusChannel)6 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)4 Record (org.openmuc.framework.data.Record)3 Value (org.openmuc.framework.data.Value)3 ChannelValueContainer (org.openmuc.framework.driver.spi.ChannelValueContainer)3 ModbusResponse (com.ghgande.j2mod.modbus.msg.ModbusResponse)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 ModbusSlaveException (com.ghgande.j2mod.modbus.ModbusSlaveException)1 ExceptionResponse (com.ghgande.j2mod.modbus.msg.ExceptionResponse)1 InputRegister (com.ghgande.j2mod.modbus.procimg.InputRegister)1 ArrayList (java.util.ArrayList)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1