Hello,
I'm using rxtx implementation installed on Ubuntu 12.04.
For simulating serial comm ports I did next things:
I use jamod-1.2rc1 version
used socat to create 2 linked ports
socat -d -d pty pty
it creates me 2 virtual ports whihc communicates one wiht another /dev/pts/0 and /dev/pts/1
after that I create 2 links to created ports
ln -s /dev/pts/0 /dev/ttyS00
ln -s /dev/pts/1 /dev/ttyS01 I did so because rxtx see just only ports started with tty
after that I run my serial slave app (took from jamod examples) running on /dev/tty00
try to communicate with serial master on /dev/tty02 port and it throws time out exception:
Devel Library
Native lib Version = RXTX-2.0-7pre2
Java lib Version = RXTX-2.0-7pre2
RXTX Warning: Removing stale lock file. /var/lock/LCK..ttyS03
RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyS02: File exists
Writing: 01 01 00 00 00 00
I/O exception - Serial port timeout.
execute try 1 error: I/O exception - failed to read.
Writing: 01 01 00 00 00 00
I/O exception - Serial port timeout.
execute try 2 error: I/O exception - failed to read.
Writing: 01 01 00 00 00 00
I/O exception - Serial port timeout.
Exception in thread "main" net.wimpi.modbus.ModbusIOException: I/O exception - failed to read.
at net.wimpi.modbus.io.ModbusASCIITransport.readResponse(ModbusASCIITransport.java:217)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:183)
at com.jamod.master.Master.test(Master.java:73)
at com.jamod.master.Master.main(Master.java:31)
/*Theimportantinstancesoftheclassesmentionedbefore*/SerialConnectioncon=null;// the connectionModbusSerialTransactiontrans=null;// the transactionReadInputRegistersRequestreq=null;// the requestReadInputRegistersResponseres=null;// the response/*Variablesforstoringtheparameters*/Stringportname="/dev/ttyS03";// the name of the serial port to be usedintunitid=1;// the unit identifier we will be talking tointref=0;// the reference, where to start reading fromintcount=0;// the count of IR's to readintrepeat=1;// a loop for repeating the transactionpublicstaticvoidmain(String[]args)throwsException{newMaster().test();}publicvoidtest()throwsException{System.setProperty("net.wimpi.modbus.debug","true");System.setProperty("build.serial.gnu","true");// 2. Set master identifier
// 3. Setup serial parametersSerialParametersparams=newSerialParameters();params.setPortName(portname);params.setBaudRate(9600);params.setDatabits(8);params.setParity("None");params.setStopbits(1);params.setEncoding("ascii");params.setEcho(false);
// params.setFlowControlIn(1);
// 4. Open the connectioncon=newSerialConnection(params);con.open();// 5. Prepare a requestreq=newReadInputRegistersRequest(ref,count);req.setUnitID(unitid);req.setHeadless();// 6. Prepare a transactiontrans=newModbusSerialTransaction(con);trans.setRequest(req);
// trans.setTransDelayMS(100000);
//7. Execute the transaction repeat timesintk=0;do{trans.execute();res=(ReadInputRegistersResponse)trans.getResponse();for(intn=0;n<res.getWordCount();n++){System.out.println("Word "+n+"="+res.getRegisterValue(n));}k++;}while(k<repeat);//8. Close the connectioncon.close();}
}
Thank you in advance
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I'm using rxtx implementation installed on Ubuntu 12.04.
For simulating serial comm ports I did next things:
socat -d -d pty pty
it creates me 2 virtual ports whihc communicates one wiht another /dev/pts/0 and /dev/pts/1
after that I create 2 links to created ports
ln -s /dev/pts/0 /dev/ttyS00
ln -s /dev/pts/1 /dev/ttyS01
I did so because rxtx see just only ports started with tty
Devel Library
Native lib Version = RXTX-2.0-7pre2
Java lib Version = RXTX-2.0-7pre2
RXTX Warning: Removing stale lock file. /var/lock/LCK..ttyS03
RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyS02: File exists
Writing: 01 01 00 00 00 00
I/O exception - Serial port timeout.
execute try 1 error: I/O exception - failed to read.
Writing: 01 01 00 00 00 00
I/O exception - Serial port timeout.
execute try 2 error: I/O exception - failed to read.
Writing: 01 01 00 00 00 00
I/O exception - Serial port timeout.
Exception in thread "main" net.wimpi.modbus.ModbusIOException: I/O exception - failed to read.
at net.wimpi.modbus.io.ModbusASCIITransport.readResponse(ModbusASCIITransport.java:217)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:183)
at com.jamod.master.Master.test(Master.java:73)
at com.jamod.master.Master.main(Master.java:31)
My Slave code
package com.jamod.slave;
import net.wimpi.modbus.ModbusCoupler;
import net.wimpi.modbus.net.ModbusSerialListener;
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;
import net.wimpi.modbus.util.SerialParameters;
public class Slave {
}
My Master code
package com.jamod.master;
import java.io.DataOutput;
import java.io.IOException;
import net.wimpi.modbus.ModbusCoupler;
import net.wimpi.modbus.io.BytesOutputStream;
import net.wimpi.modbus.io.ModbusSerialTransaction;
import net.wimpi.modbus.msg.ReadInputRegistersRequest;
import net.wimpi.modbus.msg.ReadInputRegistersResponse;
import net.wimpi.modbus.net.SerialConnection;
import net.wimpi.modbus.util.SerialParameters;
public class Master {
// ModbusCoupler.createModbusCoupler(null);
ModbusCoupler.getReference().setUnitID(1);
ModbusCoupler.getReference().setMaster(true);
// params.setFlowControlIn(1);
// trans.setTransDelayMS(100000);
}
Thank you in advance