Coder Social home page Coder Social logo

Comments (21)

gkrananth avatar gkrananth commented on August 15, 2024

i have tried serial example to read float32 holding register value. Please find the code

          	ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest();
                request.setServerAddress(slaveId);
                request.setStartAddress(offset);
                request.setQuantity(quantity);
                ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) request.getResponse();

                m.processRequest(request);
                ModbusHoldingRegisters registers = response.getHoldingRegisters();
                for (int r : registers) {
                    System.out.println(r);
                }
                //get float
                System.out.println("PI is approximately equal to " + registers.getFloat32At(0));

i am getting ILLEGAL Data_Value error code 3. My register is floating 32 data type how do i get value for the register value of float32. Integer is working.

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

You get ILLEGAL Data_Value at line "m.processRequest(request);"?

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

yes.

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

appreciate if you can help me faster. Thanks

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

At string "m.processRequest(request);" you just request multiple holding registers (16-bit values). If a device responds you with ILLEGAL_Data_Value see the documentation on the device (memory map). Perhaps, you made a mistake in address or quantity field.

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

Note that reading float32 requires requesting 2 holding register.

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

thanks for your reply, i am getting the following error even my quantity increased to 2

ipc01@ipc01-desktop:~/Downloads$ java -jar TestRTU.jar /dev/ttyUSB5 1 3110 2
INFO: Frame sent: 01030C2600022690
INFO: Frame recv: 0183030131
com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException: ILLEGAL_DATA_VALUE: Exception Code = 3
at com.intelligt.modbus.jlibmodbus.ModbusMaster.processRequest(ModbusMaster.java:142)
at com.hdb.ModbusTestRTU.main(ModbusTestRTU.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

You received the exception code 3 from the remote device with slaveId = 1, function code = 03. It means that the remote device cannot send you 2 valid register values with offset 0C26. You receive valid MODBUS EXCEPTION response from the remote slave and the library handle the response correctly. Can you show me the slave's memory-map?

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

modbusregister

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

I do not see an element with offset 3110 (0x0C26).

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

3110 is there but my attachment is referring the float32 register data type and even 2700 is also giving same error.

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

please show the source code.

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

package com.hdb;

import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.data.ModbusHoldingRegisters;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.intelligt.modbus.jlibmodbus.msg.request.ReadHoldingRegistersRequest;
import com.intelligt.modbus.jlibmodbus.msg.response.ReadHoldingRegistersResponse;
import com.intelligt.modbus.jlibmodbus.serial.SerialParameters;
import com.intelligt.modbus.jlibmodbus.serial.SerialPort;
import com.intelligt.modbus.jlibmodbus.serial.SerialPortException;

import jssc.SerialPortList;

/*

  • Copyright (C) 2016 "Invertor" Factory", JSC

  • All rights reserved

  • This file is part of JLibModbus.

  • Redistribution and use in source and binary forms, with or without modification,

  • are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
  • this list of conditions and the following disclaimer.

    1. Redistributions in binary form must reproduce the above copyright notice,
  • this list of conditions and the following disclaimer in the documentation and/or

  • other materials provided with the distribution.

    1. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
  • or promote products derived from this software without specific prior written permission.

  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,

  • INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.

  • IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,

  • OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;

  • OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

  • OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,

  • EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  • Authors: Vladislav Y. Kochedykov, software engineer.

  • email: [email protected]
    */
    public class ModbusTestRTU {

    static public void main(String[] arg) throws SerialPortException {
    SerialParameters sp = new SerialParameters();
    Modbus.setLogLevel(Modbus.LogLevel.LEVEL_DEBUG);
    try {
    // you can use just string to get connection with remote slave,
    // but you can also get a list of all serial ports available at your system.
    // String[] dev_list = SerialPortList.getPortNames();
    // if there is at least one serial port at your system
    // if (dev_list.length > 0) {
    // you can choose the one of those you need
    sp.setDevice(arg[0]);
    // these parameters are set by default
    sp.setBaudRate(SerialPort.BaudRate.BAUD_RATE_19200);
    sp.setDataBits(8);
    sp.setParity(SerialPort.Parity.EVEN);
    sp.setStopBits(1);
    //you can choose the library to use.
    //the library uses jssc by default.
    //
    //first, you should set the factory that will be used by library to create an instance of SerialPort.
    //SerialUtils.setSerialPortFactory(new SerialPortFactoryRXTX());
    // JSSC is Java Simple Serial Connector
    //SerialUtils.setSerialPortFactory(new SerialPortFactoryJSSC());
    // PJC is PureJavaComm.
    //SerialUtils.setSerialPortFactory(new SerialPortFactoryPJC());
    // JavaComm is the Java Communications API (also known as javax.comm)
    //SerialUtils.setSerialPortFactory(new SerialPortFactoryJavaComm());
    //in case of using serial-to-wifi adapter
    //String ip = "192.168.0.180";//for instance
    //int port = 777;
    //SerialUtils.setSerialPortFactory(new SerialPortFactoryTcp(new TcpParameters(InetAddress.getByName(ip), port, true)));
    // you should use another method:
    //next you just create your master and use it.
    ModbusMaster m = ModbusMasterFactory.createModbusMasterRTU(sp);
    m.connect();
    while(true){

             int slaveId = Integer.parseInt(arg[1]);
             int offset = Integer.parseInt(arg[2]);
             int quantity = Integer.parseInt(arg[3]);
             //you can invoke #connect method manually, otherwise it'll be invoked automatically
             try {
             	ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest();
                 request.setServerAddress(slaveId);
                 request.setStartAddress(offset);
                 request.setQuantity(quantity);
                 ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) request.getResponse();
    
                 m.processRequest(request);
                 ModbusHoldingRegisters registers = response.getHoldingRegisters();
                 for (int r : registers) {
                     System.out.println(r);
                 }
                 //get float
                 System.out.println("PI is approximately equal to " + registers.getFloat32At(0));
                 System.out.println();
    
             //    m.disconnect();
             } catch (ModbusProtocolException e) {
                 e.printStackTrace();
             } catch (ModbusIOException e) {
                 e.printStackTrace();
             } catch (ModbusNumberException e) {
                 e.printStackTrace();
             }
             Thread.sleep(1000);
             }
    

    // }
    } catch (RuntimeException e) {
    throw e;
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

Try using ReadInputRegisters.

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

But it is holding register. Is it ok to use ReadInputRegisters?

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

It is READ ONLY but holding register can be modified. So I guess that, actually, it is input register,

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

anyhow i got a error Illegal Function

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

I think you should ask the developer of the device you use because you send a valid request and device respond you with an exception. This issue does not relate to the library.

from jlibmodbus.

gkrananth avatar gkrananth commented on August 15, 2024

Hi
How to read holding register with reverse float 32 bit register value?

from jlibmodbus.

kochedykov avatar kochedykov commented on August 15, 2024

Hello. Holding register takes 16 bit of memory and cannot store float32. If you mean that you have to read two registers holding one float32 and each register is a bit-reversed number then you can read these two registers and apply a bit-reversal algorithm for each of the two registers. To get a floating point number you have to combine the result by bitwise operations "left-shift" and "or".

from jlibmodbus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.