MicroLogix 1200 in Modbus mode rtu slave mode does not respond java application jamod

yuri90

Member
Join Date
Mar 2015
Location
Caucaia
Posts
7
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(n));
}
k++;
} while (k < repeat);

//8. Close the connection
con.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

programtoplc.jpg modbusconfig.jpg
 
Last edited:

Similar Topics

Hi, I've got a ML 1200 C series with only 1 port and would like to connect to our DCS via Modbus. Since there's only one port, I've decided to...
Replies
1
Views
1,431
I am just getting information about a need that a site has. They currently have a machine that has a ML1200 and is being interfaced to by a...
Replies
16
Views
6,593
I know there are many threads about Modbus with the Micrologix but I cant find a specific answer to my query. I have a MLX 1200 single port...
Replies
3
Views
1,982
Hello I encountered a problem when connecting a Micrologix to a Siemens S7-1200 CPU using MODBUS RTU. HW configuration is: Siemens S7-1200 with...
Replies
4
Views
6,707
In micrologix1200 I have only one communication channel so that I do not unable to configure the PLC with Modbus rtu slave driver without losing...
Replies
9
Views
4,219
Back
Top Bottom