Menu

Read Float/WriteFloat

Help
2016-01-06
2016-03-10
  • Greg Cushing

    Greg Cushing - 2016-01-06

    Hi,

    I am trying to write a float value to a 984-265 quantum using the jamod library.

    I cannot seem to find really good documentation/code regarding this. As such I have two requests.

    1)I was hoping to find a decent example of writing a hold register(any hold register)

    2) I was hoping to find out if its possible to actually write a float directly.

    Below is the code I am currently using to try and read a 4x register and i get an error code 2 or i get a 0 every time.

    import java.io.;
    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 NRFCutterHoldingRead
    {
    public static void main(String[] args) {
    TCPMasterConnection con = null; //the connection
    ModbusTCPTransaction trans = null; //the transaction
    ReadMultipleRegistersRequest req = null; //the request
    ReadMultipleRegistersResponse res = null; //the response
    try {
    String astr = "10.80.46.2"; //Modbus Device

        InetAddress addr = InetAddress.getByName(astr);
    
        int port =502;
        int ref = 0; //the reference; offset where to start reading from
        int count = 4; //the number of DI's to read
        Integer[] result = new Integer[count];
        //1. Setup the parameters
        if (args.length < 1) {
          System.exit(1);
        } else {
          try {
            ref = Integer.decode(args[0]).intValue();
          } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
          }
        }
    
        /* Variables for storing the parameters */
        con = new TCPMasterConnection(addr);
        con.setPort(port);
        con.connect();
    
        //3. Prepare the request
        req = new ReadMultipleRegistersRequest(ref, count);
    
        //4. Prepare the transaction
        trans = new ModbusTCPTransaction(con);
        trans.setRequest(req);
        trans.execute();
        res = (ReadMultipleRegistersResponse) trans.getResponse();
        for ( int w = 0; w < res.getWordCount(); w++ ) {
            System.out.println("Got Modbus read {} response {}" + ref + w + res.getRegisterValue(w));
            result[w] = res.getRegisterValue(w);
        }
        System.out.println(Arrays.toString(result));
        con.close();
        System.exit(1);
        } catch (Exception ex) {
        ex.printStackTrace();
        }
    
    }
    

    }

    Any help or guidance would be appreciated.

     

    Last edit: Greg Cushing 2016-01-06
  • Joe Roberts

    Joe Roberts - 2016-03-02

    Regarding reading the data, can you not do it by using the ReadInputRegistersRequest class with a count of 4? I am fairly new to this project and modbus (started today) - but that is what I am doing in my code. I basically passed in the start register address and count of 4 and the code read the next 3 registers. The net.wimpi.modbus.cmd.AITest class shows how to do this.

    Not sure about the writing part yet - still exploring...:)

     
  • Julie Haugh

    Julie Haugh - 2016-03-10

    There's no standard way of reading or writing floating point values. You'd have to find out what the byte order and floating point format for your slave device is, so you can marshall the data correctly. Once you've done that, you'd populate enough 16-bit "short" values with the byte representation, then do the WRITE HOLDING REGISTER request. Just remember that "input" and "holding" registers are in a different Modbus address space, so you need to make sure your READ request is correct.

     
  • Christopher Piggott

    Here is what I used. You call ModbusCOnvUtil.wrap(response) around the response. I only filled in a couple of conversions, and the byte ordering is for a Modicon M340. As has been pointed out here there isn't really a "standard" but this byte ordering works most places I have needed it. Hope it works fo ryou.

    import net.wimpi.modbus.msg.ModbusResponse;
    
    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    
    /**
     * Helper for converting some jamod response messages.
     * 
     * @author <a href="mailto:cepasp@rit.edu">Christopher Piggott</a>
     *         Rochester Institute of Technology
     */
    public class ModbusConvUtil {
    
        private final ByteBuffer buf;
    
        protected ModbusConvUtil(ByteBuffer buf) {
            this.buf = buf;
        }
    
        public static ModbusConvUtil wrap(ModbusResponse response) throws IOException {
            int len = response.getDataLength();
            ByteArrayOutputStream baos = new ByteArrayOutputStream(len);
            DataOutputStream daos = new DataOutputStream(baos);
    
            /* Write data from the message into our output stream */
            response.writeData(daos);
            return new ModbusConvUtil(ByteBuffer.wrap(baos.toByteArray(), 1, len - 2 - 1));
        }
    
        public byte readByte() throws IOException {
            return buf.get();
        }
    
        public float readFloat() throws IOException {
            byte[] bytes = new byte[4];
            bytes[2] = buf.get();
            bytes[3] = buf.get();
            bytes[0] = buf.get();
            bytes[1] = buf.get();
    
            ByteBuffer temp = ByteBuffer.wrap(bytes);
            return temp.getFloat();
        }
    
        public int readShortUnsigned() {
            return ((buf.get() & 0xff) << 8 | (buf.get() & 0xff));
        }
    
        public int readInteger(byte[] bytes) {
            return ((buf.get() & 0xff) << 24) |
                    ((buf.get() & 0xff) << 16) |
                    ((buf.get() & 0xff) << 8) |
                    (buf.get() & 0xff);
        }
    }
    
     

Log in to post a comment.