按位取值是指該測點的數(shù)據(jù)類型為16位有/無符號、32位有/無符號時,將讀到的值按BIT位解析,起始位和終止位相同時,表示解析1位數(shù)據(jù),值為0或1;起始位和終止位根據(jù)數(shù)據(jù)類型不同,最小0,最大31;
例如讀到的值為十進制的10,那么將其轉(zhuǎn)換為二進制為0000 1010,其第0位(起始位和終止位都配置成0)為0,第1為1,第2位為0,第3位為1。
杭州領(lǐng)祺科技智能物聯(lián)網(wǎng)關(guān)支持MODBUS采集、轉(zhuǎn)發(fā)、監(jiān)聽,支持485串口MODBUS異構(gòu)組網(wǎng),支持多上位機WEB組態(tài)同時接收信息,支持MODBUS和M-BUS協(xié)議轉(zhuǎn)換,MODBUS TCP MODBUS RTU等各種協(xié)議轉(zhuǎn)換、混采。
在線幫助文檔
https://dingiiot.com/help-doc/drive_page/modbus-rtu%E9%87%87%E9%9B%86/
設(shè)備地址:從設(shè)備的地址編號
CRC校驗:用來確認接收消息完整性和正確性
Java代碼取值實現(xiàn)
網(wǎng)上有許多開源的modbos工具包,例如modbus4j、jamod等。這里選用jamod,以Modbus RTU通訊為例
maven依賴如下,同時需要在C://Windows//System32目錄下,放入RXTXcomm.jar、rxtxParallel.dll、rxtxSerial.dll三個文件,用來實現(xiàn)串口通信
net.wimpi
jamod
1.2
創(chuàng)建和串口的連接
public class ModBusConnection {
public static SerialConnection getSerialConnection() throws Exception {
SerialParameters parameters = new SerialParameters();
// 串口名稱
parameters.setPortName("COM2");
// 波特率
parameters.setBaudRate(9600);
// 數(shù)據(jù)位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;
}
}
創(chuàng)建request取數(shù)工具類
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 {
// 寄存器地址編號 屬性數(shù)據(jù)數(shù)量
ReadInputRegistersRequest request = new ReadInputRegistersRequest(param.getAddressNo(), param.getWordCount());
// 從設(shè)備編號
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();
// 從設(shè)備id編號
param.setSlaveId(1);
// 寄存器地址編號 這邊使用了function 01,addressNo=0表示寄存器地址為00001
param.setAddressNo(0);
// 讀取數(shù)據(jù)個數(shù)
param.setWordCount(1);
InputRegister[] registers = ModBusUtil.readInputRegisters(connection, param);
for (InputRegister register : registers) {
System.out.println(register.getValue());
}
connection.close();
}
}