Hello people.Thank you in advance. Below is a java application which I try to communicate my java application with ml 1200 through the Modbus protocol but have no success, apparently the ml1200 does not respond to requests made by my application.
I setup the modbus rtu slave driver according to the discussion made in another post:
http://www.plctalk.net/qanda/showthread.php?p=634126#post634126
The application runs but does not answer the Modbus requests.
Error java:
Serial port status...true
Last request: 01 04 00 02 00 01 90 0a
CRC Error in received frame: 0 bytes:
execute try 1 error: I/O exception - failed to read
Last request: 01 04 00 02 00 01 90 0a
CRC Error in received frame: 0 bytes:
execute try 2 error: I/O exception - failed to read
Last request: 01 04 00 02 00 01 90 0a
CRC Error in received frame: 0 bytes:
net.wimpi.modbus.ModbusIOException: I/O exception - failed to read
at net.wimpi.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:180)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:183)
at SerialAITest.main(SerialAITest.java:143)
Program:----------------------------------------------------
import java.net.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.ParallelPort;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.util.*;
public class SerialAITest {
public static void main(String[] args) {
/* The important instances of the classes mentioned before */
SerialConnection con = null; //the connection
ModbusSerialTransaction trans = null; //the transaction
ReadInputRegistersRequest req = null; //the request, The implementation directly correlates with the class 0 function read multiple registers
ReadInputRegistersResponse res = null; //the response
ModbusCoupler coupler = null;
ReadInputDiscretesRequest reqDisc =null; //add por mim
/* Variables for storing the parameters */
String portname= null; //the name of the serial port to be used
int unitid = 0; //the unit identifier we will be talking to
int ref = 2; //the reference, where to start reading from
int count = 1; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction
portname = "COM1";
unitid = 1;
//2. Set master identifier
//ModbusCoupler.createModbusCoupler(null); método createModbusCoupler não existe!
coupler = ModbusCoupler.getReference();
coupler.setUnitID(1);
coupler.setMaster(true);
//ModbusCoupler.getReference().setUnitID(1);
portname = "COM1";
//3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(19200);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("RTU");
params.setEcho(false);
//params.setReceiveTimeout(1000);
//4. Open the connection
con = new SerialConnection(params);
try {
con.open();
System.out.println("Serial port status..." + con.isOpen());
//5. Prepare a request
req = new ReadInputRegistersRequest(ref, count); /*Constructs a new
ReadInputRegistersRequest instance with a given reference and count of words to be read.*/
req.setUnitID(unitid); // Sets the unit identifier of this ModbusMessage.
req.setHeadless(); //Sets the headless flag of this message.
//6. Prepare a transaction
trans = new ModbusSerialTransaction(); /*Constructs a new ModbusSerialTransaction instance
with a given ModbusRequest to be send when the transaction is executed.*/
trans.setRequest(req); //Sets the ModbusRequest req for this ModbusTransaction.
trans.setRetries(3);
trans.setSerialConnection(con);
//Thread.sleep(500);
//7. Execute the transaction repeat times
int k = 0;
do {
//trans.setTransDelayMS(50);
trans.execute();
//Thread.sleep(500);
//ModbusResponse pippo = null;
//pippo = trans.getResponse();
//System.out.println(pippo.getHexMessage());
res = (ReadInputRegistersResponse) trans.getResponse();
for (int n = 0; n < res.getWordCount(); n++) { // getWordCount() Returns the number of words to be read with this ReadInputRegistersRequest res .
System.out.println("Word " + n + "=" + res.getRegisterValue);
}
k++;
} while (k < repeat);
//8. Close the connection
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I setup the modbus rtu slave driver according to the discussion made in another post:
http://www.plctalk.net/qanda/showthread.php?p=634126#post634126
The application runs but does not answer the Modbus requests.
Error java:
Serial port status...true
Last request: 01 04 00 02 00 01 90 0a
CRC Error in received frame: 0 bytes:
execute try 1 error: I/O exception - failed to read
Last request: 01 04 00 02 00 01 90 0a
CRC Error in received frame: 0 bytes:
execute try 2 error: I/O exception - failed to read
Last request: 01 04 00 02 00 01 90 0a
CRC Error in received frame: 0 bytes:
net.wimpi.modbus.ModbusIOException: I/O exception - failed to read
at net.wimpi.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:180)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:183)
at SerialAITest.main(SerialAITest.java:143)
Program:----------------------------------------------------
import java.net.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.ParallelPort;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.util.*;
public class SerialAITest {
public static void main(String[] args) {
/* The important instances of the classes mentioned before */
SerialConnection con = null; //the connection
ModbusSerialTransaction trans = null; //the transaction
ReadInputRegistersRequest req = null; //the request, The implementation directly correlates with the class 0 function read multiple registers
ReadInputRegistersResponse res = null; //the response
ModbusCoupler coupler = null;
ReadInputDiscretesRequest reqDisc =null; //add por mim
/* Variables for storing the parameters */
String portname= null; //the name of the serial port to be used
int unitid = 0; //the unit identifier we will be talking to
int ref = 2; //the reference, where to start reading from
int count = 1; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction
portname = "COM1";
unitid = 1;
//2. Set master identifier
//ModbusCoupler.createModbusCoupler(null); método createModbusCoupler não existe!
coupler = ModbusCoupler.getReference();
coupler.setUnitID(1);
coupler.setMaster(true);
//ModbusCoupler.getReference().setUnitID(1);
portname = "COM1";
//3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(19200);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("RTU");
params.setEcho(false);
//params.setReceiveTimeout(1000);
//4. Open the connection
con = new SerialConnection(params);
try {
con.open();
System.out.println("Serial port status..." + con.isOpen());
//5. Prepare a request
req = new ReadInputRegistersRequest(ref, count); /*Constructs a new
ReadInputRegistersRequest instance with a given reference and count of words to be read.*/
req.setUnitID(unitid); // Sets the unit identifier of this ModbusMessage.
req.setHeadless(); //Sets the headless flag of this message.
//6. Prepare a transaction
trans = new ModbusSerialTransaction(); /*Constructs a new ModbusSerialTransaction instance
with a given ModbusRequest to be send when the transaction is executed.*/
trans.setRequest(req); //Sets the ModbusRequest req for this ModbusTransaction.
trans.setRetries(3);
trans.setSerialConnection(con);
//Thread.sleep(500);
//7. Execute the transaction repeat times
int k = 0;
do {
//trans.setTransDelayMS(50);
trans.execute();
//Thread.sleep(500);
//ModbusResponse pippo = null;
//pippo = trans.getResponse();
//System.out.println(pippo.getHexMessage());
res = (ReadInputRegistersResponse) trans.getResponse();
for (int n = 0; n < res.getWordCount(); n++) { // getWordCount() Returns the number of words to be read with this ReadInputRegistersRequest res .
System.out.println("Word " + n + "=" + res.getRegisterValue);
}
k++;
} while (k < repeat);
//8. Close the connection
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Last edited: