[Jamod-users] Problem with basic example
Brought to you by:
wimpi
|
From: David G. R. <rei...@in...> - 2014-12-04 16:08:16
|
Hi, I am currently trying to get the basic Jamod-Example from the howto for master and slave from http://jamod.sourceforge.net/kb/tcp_master_howto.html running. Unfortunately, the Slave seems to be running correctly all the time but I can't get any information through the maser. The following code: import java.lang.Thread.UncaughtExceptionHandler; import java.net.InetAddress; import net.wimpi.modbus.Modbus; import net.wimpi.modbus.ModbusCoupler; import net.wimpi.modbus.io.ModbusTCPTransaction; import net.wimpi.modbus.msg.ReadInputDiscretesRequest; import net.wimpi.modbus.msg.ReadInputDiscretesResponse; import net.wimpi.modbus.net.ModbusTCPListener; import net.wimpi.modbus.net.TCPMasterConnection; import net.wimpi.modbus.procimg.SimpleDigitalIn; import net.wimpi.modbus.procimg.SimpleDigitalOut; import net.wimpi.modbus.procimg.SimpleInputRegister; import net.wimpi.modbus.procimg.SimpleProcessImage; import net.wimpi.modbus.procimg.SimpleRegister; class SlaveThread implements Runnable{ private int port; public SlaveThread(int port) { this.port = port; } @Override public void run() { /* The important instances and variables */ ModbusTCPListener listener = null; SimpleProcessImage spi = null; System.out.println("Waiting at " + port); // 2. Prepare a process image spi = new SimpleProcessImage(); spi.addDigitalOut(new SimpleDigitalOut(true)); spi.addDigitalOut(new SimpleDigitalOut(false)); spi.addDigitalIn(new SimpleDigitalIn(false)); spi.addDigitalIn(new SimpleDigitalIn(true)); spi.addDigitalIn(new SimpleDigitalIn(false)); spi.addDigitalIn(new SimpleDigitalIn(true)); spi.addRegister(new SimpleRegister(251)); spi.addInputRegister(new SimpleInputRegister(45)); // 3. Set the image on the coupler ModbusCoupler.getReference().setProcessImage(spi); ModbusCoupler.getReference().setMaster(false); ModbusCoupler.getReference().setUnitID(15); //4. Create a listener with 3 threads in pool listener = new ModbusTCPListener(3); listener.setPort(port); listener.start(); while (true){ System.out.println("Listening: " + listener.isListening()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class MasterThread implements Runnable { private String addressString; private int ref, count, repeat; public MasterThread(String addressstring, int ref, int count, int repeat) { addressString = addressstring; this.ref = ref; this.count = count; this.repeat = repeat; } @Override public void run() { TCPMasterConnection con = null; // the connection ModbusTCPTransaction trans = null; // the transaction ReadInputDiscretesRequest req = null; // the request ReadInputDiscretesResponse res = null; // the response /* Variables for storing the parameters */ InetAddress addr = null; // the slave's address int port = Modbus.DEFAULT_PORT; // 1. Setup the parameters try { String astr = addressString; int idx = astr.indexOf(':'); if (idx == 0) { port = Integer.parseInt(astr.substring(idx + 1)); astr = astr.substring(0, idx); } System.out.println("Name: " + astr); addr = InetAddress.getByName(astr); } catch (Exception ex) { ex.printStackTrace(); return; // System.exit(1); } // 2. Open the connection con = new TCPMasterConnection(addr); con.setPort(port); try { con.connect(); // 3. Prepare the request req = new ReadInputDiscretesRequest(ref, count); // 4. Prepare the transaction trans = new ModbusTCPTransaction(con); trans.setRequest(req); // 5. Execute the transaction repeat times int k = 0; do { trans.execute(); res = (ReadInputDiscretesResponse) trans.getResponse(); System.out.println("Digital Inputs Status=" + res.getDiscretes().toString()); k++; } while (k < repeat); // 6. Close the connection con.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class Master { public static void main(String args[]) throws Exception { int ref = new Integer(args[1]); int count = new Integer(args[2]); int repeat = (args.length > 3 ? new Integer(args[3]) : 0); Thread slaveThread = new Thread(new SlaveThread(5502)); Thread masterThread = new Thread(new MasterThread(args[0], ref, count, repeat)); slaveThread.start(); masterThread.start(); UncaughtExceptionHandler uce = new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { e.printStackTrace(); } }; slaveThread.setUncaughtExceptionHandler(uce); slaveThread.join(); masterThread.join(); System.out.println("Finished"); } } only returns: Waiting at 5502 Name: localhost:5502 Listening: true java.net.UnknownHostException: localhost:5502: Der Name oder der Dienst ist nicht bekannt at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293) at java.net.InetAddress.getAllByName0(InetAddress.java:1246) at java.net.InetAddress.getAllByName(InetAddress.java:1162) at java.net.InetAddress.getAllByName(InetAddress.java:1098) at java.net.InetAddress.getByName(InetAddress.java:1048) at MasterThread.run(Master.java:111) at java.lang.Thread.run(Thread.java:745) Listening: true and even if I try to get information with ModPoll (from http://www.modbusdriver.com/modpoll.html with ./modpoll -p 5502 -m tcp -r 100 -c 5 -1 localhost), no connection can't be established. Can anyone tell me where the mistake is? Best regards, DaGeRe |