I am new to coding and i want to connect my pi to Schneider pm 1200 to get values of holding registers. and when i executed it, i got many errors related to exception.
How to write register? I am trying to use writeSingleRegister method ,but I cant's get it's register type error.
I am using j2mod
jar file
This is the code
import com.ghgande.j2mod.modbus.facade.ModbusSerialMaster;
import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.ModbusException;
import com.ghgande.j2mod.modbus.procimg.Register;
import com.ghgande.j2mod.modbus.util.*;
public class ModbusMaster
{
/**
* @param args
*/
public static void main(String[] args)
{
/* The important instances of the classes mentioned before */
ModbusSerialMaster serialMaster = null; // the connection
/* Variables for storying the parameters */
String portname = "/dev/ttyUSB0"; // the name of the serial port to be used
int unitID = 1 ; // the unit identifier we will be talking to
int startingRegister = 100001; // the reference, where to start reading from
int registerCount = 0; // the count of the input registers to read
Register[] slaveResponse = new Register[registerCount];
try
{
/* Setup the serial parameters */
SerialParameters parameters = new SerialParameters();
parameters.setPortName(portname);
parameters.setBaudRate(9600);
parameters.setDatabits(8);
parameters.setParity("None");
parameters.setStopbits(1);
parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU);
parameters.setEcho(false);
/* Open the connection */
serialMaster = new ModbusSerialMaster(parameters);
serialMaster.connect();
}
catch (Exception exception)
{
exception.printStackTrace();
}
/* Read the first four holding registers */
try
{
slaveResponse = serialMaster.readMultipleRegisters(unitID, startingRegister, registerCount);
for (int i = 0; i < slaveResponse.length; i++)
{
System.out.println("reg" + i + " = " + slaveResponse[i]);
}
}
catch (ModbusException e)
{
e.printStackTrace();
}
/* Close the connection */
serialMaster.disconnect();
}
}
this is the output
Exception in thread "main" java.lang.NoClassDefFoundError: gnu/io/SerialPortEventListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.ghgande.j2mod.modbus.facade.ModbusSerialMaster.<init>(ModbusSerialMaster.java:78)
at ModbusMaster.main(ModbusMaster.java:38)
Caused by: java.lang.ClassNotFoundException: gnu.io.SerialPortEventListener
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 14 more
Your answer will be helpful for me.
At runtime, the code executed for a Java program starts out in "class files", stored by the compiler in files with a ".class" extension. These files are often packaged in groups in "jar files", and the Java runtime can find the classes in the jar files while it's running.
Your error indicates that the runtime cannot find the class indicated. The java.io.SerialPortEventListener class appears to be available in the jar file rxtx-2.1.7.jar. You can download that (search for its reliable source) and copy it to the target machine. You'll then need to read up on what a classpath is; you can set a classpath in an environment variable or with a parameter passed to the "java" command at runtime. Either way, the runtime can find the class in that jar file when you run your program.
I've touched on a handful of concepts here; am sorry it isn't a real simple do 1-2-3 kind of answer, but you'll need to understand jar files and the classpath as you code more.