Vint - 2013-06-19

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)

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 {

public static void main(String[] args) throws Exception {
    System.setProperty("net.wimpi.modbus.debug","true");
    System.setProperty("build.serial.gnu","true");

    String portname = "/dev/ttyS02";
    /* The important instances and variables */
    ModbusSerialListener listener = null;
    SimpleProcessImage spi = null;

  //1. 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));

    //2. Create the coupler and set the slave identity
    ModbusCoupler.getReference().setProcessImage(spi);
    ModbusCoupler.getReference().setMaster(false);
    ModbusCoupler.getReference().setUnitID(2);

  //3. Set up serial parameters
    SerialParameters params = new SerialParameters();
    params.setPortName(portname);
    params.setBaudRate(9600);
    params.setDatabits(8);
    params.setParity("None");
    params.setStopbits(1);
    params.setEncoding("ascii");
    params.setEcho(false);

  //4. Set up serial listener
    listener = new ModbusSerialListener(params);
    listener.setListening(true);

}

}

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 {

/* The important instances of the classes mentioned before */
SerialConnection con = null; // the connection
ModbusSerialTransaction trans = null; // the transaction
ReadInputRegistersRequest req = null; // the request
ReadInputRegistersResponse res = null; // the response

/* Variables for storing the parameters */
String portname = "/dev/ttyS03"; // the name of the serial port to be used
int unitid = 1; // the unit identifier we will be talking to
int ref = 0; // the reference, where to start reading from
int count = 0; // the count of IR's to read
int repeat = 1; // a loop for repeating the transaction

public static void main(String[] args) throws Exception {
    new Master().test();

}

public void test() throws Exception {
    System.setProperty("net.wimpi.modbus.debug","true");
    System.setProperty("build.serial.gnu","true");
    // 2. Set master identifier

// ModbusCoupler.createModbusCoupler(null);
ModbusCoupler.getReference().setUnitID(1);
ModbusCoupler.getReference().setMaster(true);

    // 3. Setup serial parameters
    SerialParameters params = new SerialParameters();
    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 connection
    con = new SerialConnection(params);
    con.open();


    // 5. Prepare a request
    req = new ReadInputRegistersRequest(ref, count);
    req.setUnitID(unitid);
    req.setHeadless();


    // 6. Prepare a transaction
    trans = new ModbusSerialTransaction(con);
    trans.setRequest(req);

// trans.setTransDelayMS(100000);

  //7. Execute the transaction repeat times
    int k = 0;
    do {
      trans.execute();
      res = (ReadInputRegistersResponse) trans.getResponse();
      for (int n = 0; n < res.getWordCount(); n++) {
        System.out.println("Word " + n + "=" + res.getRegisterValue(n));
      }
      k++;
    } while (k < repeat);
    //8. Close the connection
    con.close();  
}

}

Thank you in advance