按位取值是指该测点的数据类型为16位有/无符号、32位有/无符号时,将读到的值按BIT位解析,起始位和终止位相同时,表示解析1位数据,值为0或1;起始位和终止位根据数据类型不同,最小0,最大31;
例如读到的值为十进制的10,那么将其转换为二进制为0000 1010,其第0位(起始位和终止位都配置成0)为0,第1为1,第2位为0,第3位为1。
杭州领祺科技智能物联网关支持MODBUS采集、转发、监听,支持485串口MODBUS异构组网,支持多上位机WEB组态同时接收信息,支持MODBUS和M-BUS协议转换,MODBUS TCP MODBUS RTU等各种协议转换、混采。
在线帮助文档
https://dingiiot.com/help-doc/drive_page/modbus-rtu%E9%87%87%E9%9B%86/
设备地址:从设备的地址编号
CRC校验:用来确认接收消息完整性和正确性
Java代码取值实现
网上有许多开源的modbos工具包,例如modbus4j、jamod等。这里选用jamod,以Modbus RTU通讯为例
maven依赖如下,同时需要在C://Windows//System32目录下,放入RXTXcomm.jar、rxtxParallel.dll、rxtxSerial.dll三个文件,用来实现串口通信
net.wimpi
jamod
1.2
创建和串口的连接
public class ModBusConnection {
public static SerialConnection getSerialConnection() throws Exception {
SerialParameters parameters = new SerialParameters();
// 串口名称
parameters.setPortName("COM2");
// 波特率
parameters.setBaudRate(9600);
// 数据位8位
parameters.setDatabits(SerialPort.DATABITS_8);
// 停止位2位
parameters.setStopbits(SerialPort.STOPBITS_2);
// 无奇偶校验位
parameters.setParity(SerialPort.PARITY_NONE);
// rtu模式,取代默认的ascii模式
parameters.setEncoding("rtu");
SerialConnection connection = new SerialConnection(parameters);
connection.open();
return connection;
}
}
创建request取数工具类
public class ModBusUtil {
/**
* 读写 boolean类型 function code 01
* @param connection con
* @param param param
* @return BitVector
*/
public static BitVector readCoils(SerialConnection connection, ModBusRequest param) throws Exception {
ReadCoilsRequest request = new ReadCoilsRequest(param.getAddressNo(), param.getWordCount());
request.setUnitID(param.getSlaveId());
ReadCoilsResponse response = (ReadCoilsResponse) getResponse(connection, request);
return response.getCoils();
}
/**
* 只读 boolean类型 function code 02
* @param connection con
* @param param param
* @return BitVector
*/
public static BitVector readInputDiscretes(SerialConnection connection, ModBusRequest param) throws Exception {
ReadInputDiscretesRequest request = new ReadInputDiscretesRequest(param.getAddressNo(), param.getWordCount());
request.setUnitID(param.getSlaveId());
ReadInputDiscretesResponse response = (ReadInputDiscretesResponse) getResponse(connection, request);
return response.getDiscretes();
}
/**
* 读写 int类型 function code 03
* @param connection con
* @param param param
* @return InputRegister
*/
public static InputRegister[] readMultipleRegisters(SerialConnection connection, ModBusRequest param) throws Exception {
ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(param.getAddressNo(), param.getWordCount());
request.setUnitID(param.getSlaveId());
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) getResponse(connection, request);
return response.getRegisters();
}
/**
* 只读 int类型 function code 04
* @param connection con
* @param param param
* @return InputRegister
*/
public static InputRegister[] readInputRegisters(SerialConnection connection, ModBusRequest param) throws Exception {
// 寄存器地址编号 属性数据数量
ReadInputRegistersRequest request = new ReadInputRegistersRequest(param.getAddressNo(), param.getWordCount());
// 从设备编号
request.setUnitID(param.getSlaveId());
ReadInputRegistersResponse response = (ReadInputRegistersResponse) getResponse(connection, request);
return response.getRegisters();
}
/**
* 获取response
* @param connection con
* @param request req
* @return ModbusResponse
*/
private static ModbusResponse getResponse(SerialConnection connection, ModbusRequest request) throws Exception {
ModbusSerialTransaction transaction = new ModbusSerialTransaction(connection);
transaction.setRequest(request);
transaction.execute();
return transaction.getResponse();
}
}
main方法测试类
public class SerialPortTest {
public static void main(String[] args) throws Exception {
SerialConnection connection = ModBusConnection.getSerialConnection();
ModBusRequest param = new ModBusRequest();
// 从设备id编号
param.setSlaveId(1);
// 寄存器地址编号 这边使用了function 01,addressNo=0表示寄存器地址为00001
param.setAddressNo(0);
// 读取数据个数
param.setWordCount(1);
InputRegister[] registers = ModBusUtil.readInputRegisters(connection, param);
for (InputRegister register : registers) {
System.out.println(register.getValue());
}
connection.close();
}
}