Menu

Serial connection - I/O Exception failed to read

2016-06-06
2016-06-07
  • Julien Jakob

    Julien Jakob - 2016-06-06

    Hello !
    There is the following exception I can't resolve :

    com.ghgande.j2mod.modbus.ModbusIOException: I/O exception - failed to read
            at com.ghgande.j2mod.modbus.io.ModbusASCIITransport.readResponseIn(Modbu
    sASCIITransport.java:192)
            at com.ghgande.j2mod.modbus.io.ModbusSerialTransport.readResponse(Modbus
    SerialTransport.java:124)
            at com.ghgande.j2mod.modbus.io.ModbusSerialTransaction.execute(ModbusSer
    ialTransaction.java:147)
            at j2modTest.readOne(j2modTest.java:39)
            at j2modTest.main(j2modTest.java:59)
    

    I really need help, can you figure out what's wrong with my code ? Here it is :

    import java.net.*;
    import java.io.*;
    import com.ghgande.j2mod.modbus.*;
    import com.ghgande.j2mod.modbus.facade.*;
    import com.ghgande.j2mod.modbus.io.*;
    import com.ghgande.j2mod.modbus.msg.*;
    import com.ghgande.j2mod.modbus.net.*;
    import com.ghgande.j2mod.modbus.util.*;
    
    public class j2modTest {
    
        SerialConnection con;
    
        public j2modTest() {
            SerialParameters param = new SerialParameters();
            param.setPortName("COM4");
            this.con = new SerialConnection(param);
        }
    
        public void readOne(String jbus) {
            try {
                //Ouverture de la connexion
                this.con.open();
    
                //Preparation de la requete
                int ref = Integer.parseInt(jbus, 16);
                ReadMultipleRegistersRequest req = new ReadMultipleRegistersRequest(ref, 1);
                req.setUnitID(1);
                req.setHeadless(true);
    
                //Preparation de la transaction
                ModbusSerialTransaction trans = new ModbusSerialTransaction(this.con);
                trans.setRetries(5);
                trans.setRequest(req);
    
                //Execution de la transaction
                trans.execute();
                Thread.sleep(500);
                ReadMultipleRegistersResponse res = (ReadMultipleRegistersResponse) trans.getResponse();
                for (int j = 0; j < res.getWordCount(); j++) {
                    System.out.println("Info " + j + " : " + res.getRegisterValue(j));
                }
    
                //Fermeture de la connexion
                this.con.close();
            }
            catch(Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    
        public static void main(String[] args) {
            j2modTest t = new j2modTest();
            t.readOne("0130");
        }
    }
    

    The readOne() method is supposed to read one holding register.

    I thank you in advance !

     

    Last edit: Julien Jakob 2016-06-06
  • Julien Jakob

    Julien Jakob - 2016-06-07

    I have completely modified my code but I still get the exact same error.
    (Most of it comes from the example in the cmd folder)
    Here it is :

    import java.io.IOException;
    import java.util.Arrays;
    
    import gnu.io.*;
    import com.ghgande.j2mod.modbus.Modbus;
    import com.ghgande.j2mod.modbus.ModbusException;
    import com.ghgande.j2mod.modbus.io.*;
    import com.ghgande.j2mod.modbus.msg.*;
    import com.ghgande.j2mod.modbus.net.ModbusMasterFactory;
    import com.ghgande.j2mod.modbus.procimg.Register;
    import com.ghgande.j2mod.modbus.util.SerialParameters;
    
    public class ReadHoldingRegistersTest {
    
        private static void printUsage() {
            System.out.println("java com.ghgande.j2mod.modbus.cmd.ReadHoldingRegistersTest"
                    + " <address{:port{:unit}} [String]>"
                    + " <base [int]> <count [int]> {<repeat [int]>}"
                    + " Ici on ecrira par exemple : device:COM4:1 0130 1");
        }
    
        public static void main(String[] args) {
            ModbusRTUTransport transport = null;
            ModbusRequest req = null;
            ModbusTransaction trans = null;
            int ref = 0;
            int count = 0;
            int repeat = 1;
            int unit = 0;
    
            //Mise en place des parametres
            if (args.length < 3) {
                printUsage();
                System.exit(1);
            }
    
            try {
                try {
                    //Ouverture de la connexion
                    String parts[] = args[0].split(":");
                    if (parts.length < 2)
                        throw new IllegalArgumentException("missing connection information");
    
                    SerialParameters params = new SerialParameters();
                    params.setPortName(parts[1]);
    
                    try {
                        transport = new ModbusRTUTransport();
                        transport.setCommPort(new RXTXPort(params.getPortName()));
                        transport.setEcho(false);
                    }
                    catch (IOException e) {
                        transport = null;
                    }
    
                    if (transport == null) {
                        System.err.println("Cannot open " + args[0]);
                        System.exit(1);
                    }
    
                    ((ModbusSerialTransport) transport).setReceiveTimeout(500);
                    if (System.getProperty("com.ghgande.j2mod.modbus.baud") != null)
                        ((ModbusSerialTransport) transport).setBaudRate(Integer.parseInt(System.getProperty("com.ghgande.j2mod.modbus.baud")));
                    else
                        ((ModbusSerialTransport) transport).setBaudRate(9600);
    
                    /*
                     * Certains appareils ne s'initialisent pas immediatement, 
                     * on leur laisse le temps
                     */
                    Thread.sleep(2000);
    
                    ref = Integer.parseInt(args[1], 16);
                    count = Integer.parseInt(args[2]);
    
                    if (args.length == 4)
                        repeat = Integer.parseInt(args[3]);
    
                    if (parts.length >= 3)
                        unit = Integer.parseInt(parts[2]);
    
                    String baud = System.getProperty("com.ghgande.j2mod.modbus.baud");
                    if (baud != null)
                        ((ModbusRTUTransport) transport).setBaudRate(Integer.parseInt(baud));
                } 
                catch (Exception ex) {
                    ex.printStackTrace();
                    printUsage();
                    System.exit(1);
                }
    
                //Creation de la requete
                req = new ReadMultipleRegistersRequest(ref, count);
                req.setUnitID(unit);
    
                //Preparation de la transaction
                trans = transport.createTransaction();
                trans.setRequest(req);
                req.setHeadless(true);
    
                if (Modbus.debug)
                    System.out.println("Request: " + req.getHexMessage());
    
                //Execution de la transaction "repeat" fois
    
                for (int i = 0; i < repeat; i++) {
                    try {
                        trans.execute();
                    } 
                    catch (ModbusException x) {
                        System.err.println(x.getMessage());
                        continue;
                    }
                    ModbusResponse res = trans.getResponse();
    
                    if (Modbus.debug) {
                        if (res != null)
                            System.out.println("Response: " + res.getHexMessage());
                        else
                            System.err.println("No response to READ HOLDING request.");
                    }
    
                    if (res instanceof ExceptionResponse) {
                        ExceptionResponse exception = (ExceptionResponse) res;
                        System.out.println(exception);
                        continue;
                    }
    
                    if (! (res instanceof ReadMultipleRegistersResponse))
                        continue;
    
                    ReadMultipleRegistersResponse data = (ReadMultipleRegistersResponse) res;
                    Register values[] = data.getRegisters();
    
                    System.out.println("Data: " + Arrays.toString(values));
                }
            } 
            catch (Exception ex) {
                ex.printStackTrace();
            }
    
            try {
                //Fermeture de la connexion
                if (transport != null)
                    transport.close();
            } 
            catch (IOException e) {
                //Ne rien faire
            }
            System.exit(0);
        }
    }
    
     

    Last edit: Julien Jakob 2016-06-13

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.