以下是代码:
读取寄存器数据:
public class readRegisters {
/**
* 工厂。
*/
static ModbusFactory modbusFactory;
static {
if (modbusFactory == null) {
modbusFactory = new ModbusFactory();
}
}
/**
* 获取master
*
* @return
* @throws ModbusInitException
*/
public static ModbusMaster getMaster() throws ModbusInitException {
IpParameters params = new IpParameters();
params.setHost("192.168.1.100");//测试的时候填写从机ip
params.setPort(502);//从机端口
// modbusFactory.createRtuMaster(wapper); //RTU 协议
// modbusFactory.createUdpMaster(params);//UDP 协议
// modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
try {
//设置超时时间
master.setTimeout(30000);
//设置重连次数
master.setRetries(5);
//初始化
master.init();
} catch (ModbusInitException e) {
e.printStackTrace();
}
return master;
}
/**
* 读01功能码
*
* @param ip 从站IP
* @param port modbus端口
* @param start 起始地址偏移量
* @param readLenth 待读寄存器个数
* @return
* @throws ModbusInitException
*/
public static ByteQueue ReadCoilsRequest( int slaveId,int start, int readLenth) throws ModbusInitException {
ModbusRequest modbusRequest = null;
try {
modbusRequest = new ReadCoilsRequest(slaveId, start, readLenth);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ModbusResponse modbusResponse = null;
try {
modbusResponse = getMaster().send(modbusRequest);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ByteQueue byteQueue = new ByteQueue(1024);
modbusResponse.write(byteQueue);
System.out.println("功能码:" + modbusRequest.getFunctionCode());
System.out.println("从站地址:" + modbusRequest.getSlaveId());
System.out.println("收到的响应信息大小:" + byteQueue.size());
System.out.println("收到的响应信息值:" + byteQueue);
return byteQueue;
}
/**
* 读02功能码
*
* @param ip 从站IP
* @param port modbus端口
* @param start 起始地址偏移量
* @param readLenth 待读寄存器个数
* @return
* @throws ModbusInitException
*/
public static ByteQueue ReadDiscreteInputsRequest( int slaveId,int start, int readLenth) throws ModbusInitException {
ModbusRequest modbusRequest = null;
try {
modbusRequest = new ReadDiscreteInputsRequest(slaveId, start, readLenth);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ModbusResponse modbusResponse = null;
try {
modbusResponse = getMaster().send(modbusRequest);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ByteQueue byteQueue = new ByteQueue(1024);
modbusResponse.write(byteQueue);
System.out.println("功能码:" + modbusRequest.getFunctionCode());
System.out.println("从站地址:" + modbusRequest.getSlaveId());
System.out.println("收到的响应信息大小:" + byteQueue.size());
System.out.println("收到的响应信息值:" + byteQueue);
return byteQueue;
}
/**
* 读03功能码内容
*
* @param ip 从站IP
* @param port modbus端口
* @param start 起始地址偏移量
* @param readLenth 待读寄存器个数
* @return
* @throws ModbusInitException
*/
public static ByteQueue ReadHoldingRegistersRequest( int slaveId,int start, int readLenth) throws ModbusInitException {
ModbusRequest modbusRequest = null;
try {
// 功能码03 读取保持寄存器的值
modbusRequest = new ReadHoldingRegistersRequest(slaveId, start, readLenth);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ModbusResponse modbusResponse = null;
try {
modbusResponse = getMaster().send(modbusRequest);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ByteQueue byteQueue = new ByteQueue(1024);
modbusResponse.write(byteQueue);
System.out.println("功能码:" + modbusRequest.getFunctionCode());
System.out.println("从站地址:" + modbusRequest.getSlaveId());
System.out.println("收到的响应信息大小:" + byteQueue.size());
System.out.println("收到的响应信息值:" + byteQueue);
return byteQueue;
}
/**
* 读04功能码
*
* @param ip 从站IP
* @param port modbus端口
* @param start 起始地址偏移量
* @param readLenth 待读寄存器个数
* @return
* @throws ModbusInitException
*/
public static ByteQueue ReadInputRegistersRequest( int slaveId,int start, int readLenth) throws ModbusInitException {
ModbusRequest modbusRequest = null;
try {
modbusRequest = new ReadInputRegistersRequest(slaveId, start, readLenth);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ModbusResponse modbusResponse = null;
try {
modbusResponse = getMaster().send(modbusRequest);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ByteQueue byteQueue = new ByteQueue(1024);
modbusResponse.write(byteQueue);
System.out.println("功能码:" + modbusRequest.getFunctionCode());
System.out.println("从站地址:" + modbusRequest.getSlaveId());
System.out.println("收到的响应信息大小:" + byteQueue.size());
System.out.println("收到的响应信息值:" + byteQueue);
return byteQueue;
}
public static void main(String[] args) {
try {
ReadHoldingRegistersRequest(1,0,1);
} catch (ModbusInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对寄存器写入数据:
public class writeRegisters {
/**
* 工厂。
*/
static ModbusFactory modbusFactory;
static {
if (modbusFactory == null) {
modbusFactory = new ModbusFactory();
}
}
/**
* 获取master
*
* @return
* @throws ModbusInitException
*/
public static ModbusMaster getMaster() throws ModbusInitException {
IpParameters params = new IpParameters();
params.setHost("127.0.0.1");
params.setPort(502);
//
// modbusFactory.createRtuMaster(wapper); //RTU 协议
// modbusFactory.createUdpMaster(params);//UDP 协议
// modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
try {
// 设置超时时间
master.setTimeout(30000);
// 设置重连次数
master.setRetries(3);
// 初始化
master.init();
} catch (ModbusInitException e) {
e.printStackTrace();
}
return master;
}
/**
* 写单个开关量到01功能码
* @param slaveId 从站ID
* @param start 起始地址偏移量
* @param values 待写数据
* @throws ModbusInitException
*/
public static void WriteCoilRequest( int slaveId, int start, boolean value) throws ModbusInitException {
try {
WriteCoilRequest request = new WriteCoilRequest(slaveId, start, value);
WriteCoilResponse response = (WriteCoilResponse) getMaster().send(request);
if (response.isException()) {
System.out.println("Exception response: message=" + response.getExceptionMessage());
} else {
System.out.println("Success");
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
}
/**
* 写入批量开关量到01功能码
* @param slaveId 从站ID
* @param start 起始地址偏移量
* @param values 待写数据
* @throws ModbusInitException
*/
public static void WriteCoilsRequest( int slaveId, int start, boolean[] value) throws ModbusInitException {
try {
WriteCoilsRequest request = new WriteCoilsRequest(slaveId, start, value);
WriteCoilsResponse response = (WriteCoilsResponse) getMaster().send(request);
if (response.isException()) {
System.out.println("Exception response: message=" + response.getExceptionMessage());
} else {
System.out.println("Success");
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
}
/**
*
*
* @param slaveId 从站ID
* @param start 起始地址偏移量
* @param anMask
* @param orMask
* @throws ModbusInitException
*/
public static void WriteMaskRegisterRequest( int slaveId, int start, int andMask, int orMask) throws ModbusInitException {
try {
WriteMaskRegisterRequest request = new WriteMaskRegisterRequest(slaveId, start, andMask, orMask);
WriteMaskRegisterResponse response = (WriteMaskRegisterResponse) getMaster().send(request);
if (response.isException()) {
System.out.println("Exception response: message=" + response.getExceptionMessage());
} else {
System.out.println("Success");
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
}
/**
* 单个写数据到03功能码
*
* @param slaveId 从站ID
* @param start 起始地址偏移量
* @param values 待写数据
* @throws ModbusInitException
*/
public static void WriteRegisterRequest( int slaveId, int start, int values) throws ModbusInitException {
try {
WriteRegisterRequest request = new WriteRegisterRequest(slaveId, start, values);
WriteRegisterResponse response = (WriteRegisterResponse) getMaster().send(request);
if (response.isException()) {
System.out.println("Exception response: message=" + response.getExceptionMessage());
} else {
System.out.println("Success");
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
}
/**
* 批量写数据到03功能码
*
* @param slaveId 从站ID
* @param start 起始地址偏移量
* @param values 待写数据
* @throws ModbusInitException
*/
public static void WriteRegistersRequest( int slaveId, int start, short[] values) throws ModbusInitException {
try {
WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
WriteRegistersResponse response = (WriteRegistersResponse) getMaster().send(request);
if (response.isException()) {
System.out.println("Exception response: message=" + response.getExceptionMessage());
} else {
System.out.println("Success");
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
boolean[] data = {true,true,true,true,true,true,true};
try {
WriteCoilsRequest(1,0,data);
} catch (ModbusInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注意进行硬件测试的时候本机ip与从机ip的网址要在同一网段