package com.intelligt.modbus;
|
|
import java.net.InetAddress;
|
import java.util.ArrayList;
|
import java.util.List;
|
import com.intelligt.modbus.jlibmodbus.Modbus;
|
import com.intelligt.modbus.jlibmodbus.data.DataHolder;
|
import com.intelligt.modbus.jlibmodbus.data.ModbusCoils;
|
import com.intelligt.modbus.jlibmodbus.data.ModbusHoldingRegisters;
|
import com.intelligt.modbus.jlibmodbus.exception.IllegalDataAddressException;
|
import com.intelligt.modbus.jlibmodbus.exception.IllegalDataValueException;
|
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
|
import com.intelligt.modbus.jlibmodbus.slave.ModbusSlave;
|
import com.intelligt.modbus.jlibmodbus.slave.ModbusSlaveFactory;
|
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
|
|
public class MyJlibModbus {
|
public String server_ip;
|
public int slave_id;
|
public ModbusSlave slave;
|
public ModbusCoils coils_ser;
|
public ModbusCoils disinputs_ser;
|
public ModbusHoldingRegisters holdings_ser;
|
public ModbusHoldingRegisters inputregs_ser;
|
|
public int coil_count;
|
public int disinputs_count;
|
public int hold_count;
|
public int inputreg_count;
|
|
|
|
public MyJlibModbus(String server_ip,int slave_id,int coil_count,int disinputs_count,int hold_count,int inputreg_count) {
|
this.server_ip = server_ip;
|
this.slave_id = slave_id;
|
this.coil_count = coil_count;
|
this.disinputs_count = disinputs_count;
|
this.hold_count = hold_count;
|
this.inputreg_count = inputreg_count;
|
}
|
|
|
public void init() {
|
try {
|
// ÉèÖôӻúTCP²ÎÊý
|
TcpParameters tcpParameters = new TcpParameters();
|
|
// ÉèÖÃTCPµÄipµØÖ·
|
InetAddress adress = InetAddress.getByName(server_ip);
|
|
// getLocalHost()·µ»ØµÄÊDZ¾»úµØÖ·
|
// tcpParameters.setHost(InetAddress.getLocalHost());
|
|
// Ϊ´Ó»úTCPÉèÖÃÉÏÊöipµØÖ·²ÎÊý
|
tcpParameters.setHost(adress);
|
|
// ÉèÖôӻúTCPµÄÊÇ·ñ³¤Á¬½Ó£¬Í¨Ë׵㽲¾ÍÊÇÒ»Ö±±£³ÖÁ¬½Ó£¬Ò»´ÎÁ¬½ÓÍêÏ´ξͲ»ÒªÔÚÁ¬½ÓÁË
|
tcpParameters.setKeepAlive(true);
|
|
// ÉèÖôӻúTCPµÄ¶Ë¿Ú
|
tcpParameters.setPort(Modbus.TCP_PORT);
|
|
// ´´½¨Ò»¸ö´Ó»ú
|
slave = ModbusSlaveFactory.createModbusSlaveTCP(tcpParameters);
|
// ÉèÖÿØÖÆÌ¨Êä³öÖ÷»úºÍ´Ó»úÃüÁî½»»¥ÈÕÖ¾
|
//Modbus.setLogLevel(Modbus.LogLevel.LEVEL_DEBUG);
|
//Modbus.setAutoIncrementTransactionId(true);
|
|
// ´´½¨´Ó»úµÄ¼Ä´æÆ÷
|
MyOwnDataHolder dh = new MyOwnDataHolder();
|
|
// Ϊ´Ó»ú¼Ä´æÆ÷Ìí¼Ó¼àÌýʼþ£¬ÕâÀïµÄ¼àÌýʼþÖ÷ÒªÊÇÖ÷»úÈç¹û·¢ËÍдÃüÁîÐ޸ĴӻúÔò¿ØÖÆÌ¨Êä³ö
|
dh.addEventListener(new ModbusEventListener() {
|
@Override
|
public void onWriteToSingleCoil(int address, boolean value) {
|
System.out
|
.print("onWriteToSingleCoil: address " + address + ", value " + value);
|
}
|
|
@Override
|
public void onWriteToMultipleCoils(int address, int quantity, boolean[] values) {
|
System.out.print("onWriteToMultipleCoils: address " + address + ", quantity "
|
+ quantity);
|
}
|
|
@Override
|
public void onWriteToSingleHoldingRegister(int address, int value) {
|
System.out.print("onWriteToSingleHoldingRegister: address " + address
|
+ ", value " + value);
|
}
|
|
@Override
|
public void onWriteToMultipleHoldingRegisters(int address, int quantity,
|
int[] values) {
|
System.out.print("onWriteToMultipleHoldingRegisters: address " + address
|
+ ", quantity " + quantity);
|
}
|
});
|
|
// Ϊ´Ó»úÉèÖüĴæÆ÷
|
slave.setDataHolder(dh);
|
// ÉèÖôӻúµÄ¶Á³¬Ê±Ê±¼ä£¬½¨ÒéÖ÷»ú¶ÁµÄ³¬Ê±Ê±¼äСÓÚ¸ÃÖµ
|
slave.setReadTimeout(2000);
|
|
// ÉèÖôӻú¼Ä´æÆ÷µÄ01ºÍ02¹¦ÄÜÂë¶ÔÓ¦µÄλ¼Ä´æÆ÷£¬¼´Ö»ÓÐfalseºÍtrueÖµ£¨»ò0ºÍ1£©
|
coils_ser = new ModbusCoils(coil_count);
|
// ÉèÖöÔӦλ¼Ä´æÆ÷µØÖ·µÄλֵ
|
//coils_ser.set(0, true);
|
// Ϊ´Ó»úÉèÖÃ01¹¦ÄÜÂë¶ÔÓ¦µÄÊýÖµ¼Ä´æÆ÷
|
slave.getDataHolder().setCoils(coils_ser);
|
|
|
|
// ÉèÖôӻú¼Ä´æÆ÷µÄ03ºÍ04¹¦ÄÜÂë¶ÔÓ¦µÄÊýÖµ¼Ä´æÆ÷
|
holdings_ser = new ModbusHoldingRegisters(hold_count);
|
// ÐÞ¸ÄÊýÖµ¼Ä´æÆ÷¶ÔӦλÖõÄÖµ£¬µÚÒ»¸ö²ÎÊý´ú±í¼Ä´æÆ÷µØÖ·£¬µÚ¶þ¸ö´ú±íÐ޸ĵÄÊýÖµ
|
//holding_ser.set(0, 12345);
|
// Ϊ´Ó»úÉèÖÃ04¹¦ÄÜÂë¶ÔÓ¦µÄÊýÖµ¼Ä´æÆ÷
|
slave.getDataHolder().setHoldingRegisters(holdings_ser);
|
|
disinputs_ser = new ModbusCoils(disinputs_count);
|
// ÉèÖöÔӦλ¼Ä´æÆ÷µØÖ·µÄλֵ
|
//ic.set(1, true);
|
slave.getDataHolder().setDiscreteInputs(disinputs_ser);
|
|
// // ÉèÖôӻú¼Ä´æÆ÷µÄ03ºÍ04¹¦ÄÜÂë¶ÔÓ¦µÄÊýÖµ¼Ä´æÆ÷
|
// inputregs_ser = new ModbusHoldingRegisters(inputreg_count);
|
// // ÐÞ¸ÄÊýÖµ¼Ä´æÆ÷¶ÔӦλÖõÄÖµ£¬µÚÒ»¸ö²ÎÊý´ú±í¼Ä´æÆ÷µØÖ·£¬µÚ¶þ¸ö´ú±íÐ޸ĵÄÊýÖµ
|
// //holding_ser.set(0, 12345);
|
// // Ϊ´Ó»úÉèÖÃ04¹¦ÄÜÂë¶ÔÓ¦µÄÊýÖµ¼Ä´æÆ÷
|
// slave.getDataHolder().setHoldingRegisters(inputregs_ser);
|
|
// Ϊ´Ó»úÉèÖôӻú·þÎñµØÖ·slaveid
|
slave.setServerAddress(slave_id);
|
// ¿ªÆô´Ó»ú¼àÌýʼþ£¬±ØÐëÒªÕâÒ»¾ä
|
slave.listen();
|
|
//Õⲿ·Ö´úÂëÖ÷ÒªÊÇÉèÖÃJavaÐéÄâ»ú¹Ø±ÕµÄʱºòÐèÒª×öµÄÊÂÇ飬¼´±¾³ÌÐò¹Ø±ÕµÄʱºòÐèÒª×öµÄÊÂÇ飬ֱ½ÓʹÓü´¿É
|
if (slave.isListening()) {
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
@Override
|
public void run() {
|
synchronized (slave) {
|
slave.notifyAll();
|
}
|
}
|
});
|
|
synchronized (slave) {
|
slave.wait();
|
}
|
|
/*
|
* using master-branch it should be #slave.close();
|
*/
|
slave.shutdown();
|
}
|
} catch (RuntimeException e) {
|
throw e;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void destory() {
|
try {
|
slave.shutdown();
|
} catch (ModbusIOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
// ¼àÌý½Ó¿Ú
|
public interface ModbusEventListener {
|
void onWriteToSingleCoil(int address, boolean value);
|
|
void onWriteToMultipleCoils(int address, int quantity, boolean[] values);
|
|
void onWriteToSingleHoldingRegister(int address, int value);
|
|
void onWriteToMultipleHoldingRegisters(int address, int quantity, int[] values);
|
}
|
|
// ¼Ä´æÆ÷ÀඨÒå
|
public static class MyOwnDataHolder extends DataHolder {
|
|
final List<ModbusEventListener> modbusEventListenerList = new ArrayList<ModbusEventListener>();
|
|
public MyOwnDataHolder() {
|
// you can place the initialization code here
|
/*
|
* something like that: setHoldingRegisters(new
|
* SimpleHoldingRegisters(10)); setCoils(new Coils(128)); ... etc.
|
*/
|
}
|
|
public void addEventListener(ModbusEventListener listener) {
|
modbusEventListenerList.add(listener);
|
}
|
|
public boolean removeEventListener(ModbusEventListener listener) {
|
return modbusEventListenerList.remove(listener);
|
}
|
|
@Override
|
public void writeHoldingRegister(int offset, int value) throws IllegalDataAddressException,
|
IllegalDataValueException {
|
for (ModbusEventListener l : modbusEventListenerList) {
|
l.onWriteToSingleHoldingRegister(offset, value);
|
}
|
super.writeHoldingRegister(offset, value);
|
}
|
|
@Override
|
public void writeHoldingRegisterRange(int offset, int[] range)
|
throws IllegalDataAddressException, IllegalDataValueException {
|
for (ModbusEventListener l : modbusEventListenerList) {
|
l.onWriteToMultipleHoldingRegisters(offset, range.length, range);
|
}
|
super.writeHoldingRegisterRange(offset, range);
|
}
|
|
@Override
|
public void writeCoil(int offset, boolean value) throws IllegalDataAddressException,
|
IllegalDataValueException {
|
for (ModbusEventListener l : modbusEventListenerList) {
|
l.onWriteToSingleCoil(offset, value);
|
}
|
super.writeCoil(offset, value);
|
}
|
|
@Override
|
public void writeCoilRange(int offset, boolean[] range) throws IllegalDataAddressException,
|
IllegalDataValueException {
|
for (ModbusEventListener l : modbusEventListenerList) {
|
l.onWriteToMultipleCoils(offset, range.length, range);
|
}
|
super.writeCoilRange(offset, range);
|
}
|
}
|
}
|