package danji;
import java.util.Arrays;
import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.ModbusRequest;
import com.serotonin.modbus4j.msg.ModbusResponse;
import com.serotonin.modbus4j.msg.ReadDiscreteInputsRequest;
import com.serotonin.modbus4j.msg.ReadDiscreteInputsResponse;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;
import com.serotonin.modbus4j.msg.WriteRegistersRequest;
import com.serotonin.modbus4j.msg.WriteRegistersResponse;
import com.serotonin.util.queue.ByteQueue;
public class Modbus4jUtil {
public static void modbusWTCP(String ip, int port, int slaveId, int start, short[] values) {
ModbusFactory modbusFactory = new ModbusFactory();
// 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
IpParameters params = new IpParameters();
params.setHost(ip);
if (502 != port) {
params.setPort(port);
}// 设置端口,默认502
ModbusMaster tcpMaster = null;
// 参数1:IP和端口信息 参数2:保持连接激活
tcpMaster = modbusFactory.createTcpMaster(params, true);
try {
tcpMaster.init();
System.out.println("===============" + 1111111);
} catch (ModbusInitException e) {
// System.out.println("11111111111111=="+"此处出现问题了啊!");
// 如果出现了通信异常信息,则保存到数据库中
//CommunityExceptionRecord cer = new CommunityExceptionRecord();
//cer.setDate(new Date());
//cer.setIp(ip);
// cer.setRemark(bgName+"出现连接异常");
// batteryGroupRecordService.saveCommunityException(cer);
}
try {
WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
WriteRegistersResponse response = (WriteRegistersResponse) tcpMaster.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 ByteQueue modbusTCP(String ip, int port, int start,int readLenth) {
ModbusFactory modbusFactory = new ModbusFactory();
// 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
IpParameters params = new IpParameters();
params.setHost(ip);
if(502!=port){params.setPort(port);}//设置端口,默认502
ModbusMaster tcpMaster = null;
tcpMaster = modbusFactory.createTcpMaster(params, true);
try {
tcpMaster.init();
System.out.println("==============="+1111111);
} catch (ModbusInitException e) {
return null;
}
ModbusRequest modbusRequest=null;
try {
modbusRequest = new ReadHoldingRegistersRequest(2, start, readLenth);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ModbusResponse modbusResponse=null;
try {
modbusResponse = tcpMaster.send(modbusRequest);
} catch (ModbusTransportException e) {
e.printStackTrace();
}
ByteQueue byteQueue= new ByteQueue(12);
modbusResponse.write(byteQueue);
System.out.println("功能码:"+modbusRequest.getFunctionCode());
System.out.println("从站地址:"+modbusRequest.getSlaveId());
System.out.println("收到的响应信息大小:"+byteQueue.size());
System.out.println("收到的响应信息值:"+byteQueue);
return byteQueue;
}
//MODBUS网络上从站地址
private final static int SLAVE_ADDRESS=2;
//串行口波特率
private final static int BAUD_RATE = 9600;
public static void main(String[] args) throws Exception {
SerialParameters serialParameters = new SerialParameters();
//设定MODBUS通讯的串行口
serialParameters.setCommPortId("COM1");
//设定成无奇偶校验
serialParameters.setParity(0);
//设定成数据位是8位
serialParameters.setDataBits(8);
//设定为1个停止位
serialParameters.setStopBits(1);
serialParameters.setPortOwnerName("Numb nuts");
//串行口上的波特率
serialParameters.setBaudRate(BAUD_RATE);
ModbusFactory modbusFactory = new ModbusFactory();
ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
try {
master.init();
readDiscreteInputTest(master,SLAVE_ADDRESS,0,8);
writeRegistersTest(master,SLAVE_ADDRESS, 0, new short[]{0x31,0xb,0xc,0xd,0xe,0x9, 0x8, 0x7, 0x6} );
readHoldingRegistersTest(master,SLAVE_ADDRESS,0,8);
}
finally {
master.destroy();
}
}
/**
* 读开关量型的输入信号
* @param master 主站
* @param slaveId 从站地址
* @param start 起始偏移量
* @param len 待读的开关量的个数
*/
private static void readDiscreteInputTest(ModbusMaster master, int slaveId, int start, int len) {
try {
ReadDiscreteInputsRequest request = new ReadDiscreteInputsRequest(slaveId, start, len);
ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse) master.send(request);
if (response.isException())
System.out.println("Exception response: message=" + response.getExceptionMessage());
else
System.out.println(Arrays.toString(response.getBooleanData()));
}
catch (ModbusTransportException e) {
e.printStackTrace();
}
}
/**
* 读保持寄存器上的内容
* @param master 主站
* @param slaveId 从站地址
* @param start 起始地址的偏移量
* @param len 待读寄存器的个数
*/
private static void readHoldingRegistersTest(ModbusMaster master, int slaveId, int start, int len) {
try {
ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, start, len);
ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master.send(request);
if (response.isException())
System.out.println("Exception response: message=" + response.getExceptionMessage());
else
System.out.println(Arrays.toString(response.getShortData()));
}
catch (ModbusTransportException e) {
e.printStackTrace();
}
}
/**
* 批量写数据到保持寄存器
* @param master 主站
* @param slaveId 从站地址
* @param start 起始地址的偏移量
* @param values 待写数据
*/
public static void writeRegistersTest(ModbusMaster master, int slaveId, int start, short[] values) {
try {
WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
WriteRegistersResponse response = (WriteRegistersResponse) master.send(request);
if (response.isException())
System.out.println("Exception response: message=" + response.getExceptionMessage());
else
System.out.println("Success");
}
catch (ModbusTransportException e) {
e.printStackTrace();
}
}
}