Comments (21)
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.
You get ILLEGAL Data_Value at line "m.processRequest(request);"?
from jlibmodbus.
yes.
from jlibmodbus.
appreciate if you can help me faster. Thanks
from jlibmodbus.
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.
Note that reading float32 requires requesting 2 holding register.
from jlibmodbus.
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.
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.
from jlibmodbus.
I do not see an element with offset 3110 (0x0C26).
from jlibmodbus.
from jlibmodbus.
3110 is there but my attachment is referring the float32 register data type and even 2700 is also giving same error.
from jlibmodbus.
please show the source code.
from jlibmodbus.
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:
-
- Redistributions of source code must retain the above copyright notice,
-
this list of conditions and the following disclaimer.
-
- 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.
-
- 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.
Try using ReadInputRegisters.
from jlibmodbus.
But it is holding register. Is it ok to use ReadInputRegisters?
from jlibmodbus.
It is READ ONLY but holding register can be modified. So I guess that, actually, it is input register,
from jlibmodbus.
anyhow i got a error Illegal Function
from jlibmodbus.
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.
Hi
How to read holding register with reverse float 32 bit register value?
from jlibmodbus.
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)
- Serial RTU Slave doesn't work properly on multi device network. HOT 1
- SimpleMasterTCP How to obtain the Slave station original codeοΌ
- WARNING: Maximum PDU size is reached. : 1024
- Cannot create more than one slave under a TCP connection HOT 4
- A problem with FrameEventListener
- Custom function code HOT 1
- simplemasterRTU not running HOT 1
- Using jSerialComm Serial Communication is not working with stop bit 2.
- Problem of register value type HOT 1
- support negative value for register HOT 1
- java.lang.ArrayIndexOutOfBoundsException when accessing coil index HOT 1
- Inconsistency between the source code and the published JAR results in RTU access timeout HOT 1
- Publishing to maven central
- Error When connect to linux serial com
- Using Modbus RTU with custom Baudrate HOT 2
- java.lang.UnsatisfiedLinkError: when using old computer with windows 10 or 11 HOT 1
- Error when connecting to Modbus RTU and connecting with jSerialComm HOT 9
- No pom for latest release (1.2.9.9) HOT 2
- Why can't the slave receive data when the master station sends more than 12 bytes? HOT 1
- Possible update jssc version? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jlibmodbus.