@@ -45,6 +45,7 @@ android { | |||
} | |||
dependencies { | |||
implementation 'com.github.licheedev:Modbus4Android:2.0.2' | |||
implementation 'androidx.appcompat:appcompat:1.6.1' | |||
implementation 'com.google.android.material:material:1.5.0' | |||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | |||
@@ -57,6 +58,5 @@ dependencies { | |||
implementation files('libs\\gson-2.2.2.jar') | |||
implementation files('libs\\sdkapi.jar') | |||
compileOnly files('libs/sdkapi.jar') | |||
implementation files('libs/modbus4J.jar') | |||
implementation files('libs/modbus4Android-1.2.jar') | |||
} |
@@ -14,7 +14,7 @@ | |||
android:allowBackup="true" | |||
android:icon="@mipmap/ncd" | |||
android:label="@string/app_name" | |||
android:supportsRtl="false" | |||
android:supportsRtl="true" | |||
android:theme="@style/AppTheme" | |||
android:requestLegacyExternalStorage="true" | |||
tools:targetApi="31"> | |||
@@ -0,0 +1,8 @@ | |||
package com.example.bpa.app; | |||
public enum DataFormat { | |||
ABCD, | |||
CDAB, | |||
BADC, | |||
DCBA | |||
} |
@@ -0,0 +1,5 @@ | |||
package com.example.bpa.app; | |||
public interface IReadCallBack <T>{ | |||
void onSuccess(T t); | |||
} |
@@ -1,73 +1,35 @@ | |||
package com.example.bpa.app; | |||
import android.util.Log; | |||
import com.example.bpa.modbus4j.ModbusParam; | |||
import com.example.bpa.modbus4j.ModbusReq; | |||
import com.example.bpa.modbus4j.OnRequestBack; | |||
import com.zgkxzx.modbus4And.ModbusFactory; | |||
import com.zgkxzx.modbus4And.ModbusMaster; | |||
//import com.zgkxzx.modbus4And.requset.ModbusParam; | |||
//import com.zgkxzx.modbus4And.requset.ModbusReq; | |||
//import com.zgkxzx.modbus4And.requset.OnRequestBack; | |||
//import com.serotonin.modbus4j.ModbusFactory; | |||
//import com.serotonin.modbus4j.ModbusMaster; | |||
//import com.serotonin.modbus4j.ip.IpParameters; | |||
public class ModbusTcpHelper { | |||
private static volatile ModbusTcpHelper mInstance; | |||
// public synchronized void release() { | |||
// super.release(); | |||
// minstance = null; | |||
// } | |||
public static ModbusTcpHelper getInstance() { | |||
if (mInstance == null) | |||
mInstance = new ModbusTcpHelper(); | |||
return mInstance; | |||
import com.licheedev.modbus4android.ModbusWorker; | |||
public class ModbusTcpHelper extends ModbusWorker { | |||
private static volatile ModbusTcpHelper instance = null; | |||
public static ModbusTcpHelper get() { | |||
ModbusTcpHelper manager = instance; | |||
if (manager == null) { | |||
synchronized (ModbusTcpHelper.class) { | |||
manager = instance; | |||
if (manager == null) { | |||
manager = new ModbusTcpHelper(); | |||
instance = manager; | |||
} | |||
} | |||
} | |||
return manager; | |||
} | |||
private ModbusMaster master = null; | |||
private ModbusFactory factory = new ModbusFactory(); | |||
public boolean Connect(String ip, int port) throws Exception { | |||
// IpParameters params = new IpParameters(); | |||
// params.setPort(port); | |||
// params.setHost(ip); | |||
// params.setEncapsulated(false); | |||
// master = factory.createTcpMaster(params, true); | |||
try { | |||
// master.setTimeout(1000);//超时时间 | |||
// master.setRetries(0);//重连次数 | |||
// master.init(); | |||
ModbusReq.getInstance().setParam(new ModbusParam() | |||
.setHost("192.168.1.14") | |||
.setPort(502) | |||
.setEncapsulated(false) | |||
.setKeepAlive(true) | |||
.setTimeout(2000) | |||
.setRetries(0)) | |||
.init(new OnRequestBack<String>() { | |||
@Override | |||
public void onSuccess(String s) { | |||
Log.i("TAG", "onSuccess " + s); | |||
} | |||
private ModbusTcpHelper() { | |||
} | |||
@Override | |||
public void onFailed(String msg) { | |||
Log.i("TAG", "onFailed " + msg); | |||
} | |||
}); | |||
/** | |||
* 释放整个ModbusManager,单例会被置null | |||
*/ | |||
public synchronized void release() { | |||
super.release(); | |||
instance = null; | |||
} | |||
} catch (Exception e) { | |||
Log.i("错误信息", e.getMessage()); | |||
return false; | |||
} | |||
return true; | |||
} | |||
} |
@@ -0,0 +1,202 @@ | |||
package com.example.bpa.app; | |||
import android.util.Log; | |||
import com.licheedev.modbus4android.ModbusCallback; | |||
import com.licheedev.modbus4android.ModbusParam; | |||
import com.licheedev.modbus4android.param.TcpParam; | |||
import com.serotonin.modbus4j.ModbusMaster; | |||
import com.serotonin.modbus4j.msg.ReadCoilsResponse; | |||
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; | |||
import com.serotonin.modbus4j.msg.WriteCoilResponse; | |||
import com.serotonin.modbus4j.msg.WriteRegistersResponse; | |||
import java.lang.reflect.Type; | |||
import java.nio.ByteBuffer; | |||
import java.nio.FloatBuffer; | |||
import java.nio.ShortBuffer; | |||
import java.util.Arrays; | |||
import java.util.LinkedList; | |||
import io.reactivex.SingleConverter; | |||
public class ModbusTcpServer { | |||
private static volatile ModbusTcpServer instance = null; | |||
public static ModbusTcpServer get() { | |||
ModbusTcpServer manager = instance; | |||
if (manager == null) { | |||
synchronized (ModbusTcpServer.class) { | |||
manager = instance; | |||
if (manager == null) { | |||
manager = new ModbusTcpServer(); | |||
instance = manager; | |||
} | |||
} | |||
} | |||
return manager; | |||
} | |||
private ModbusTcpServer() { | |||
} | |||
ModbusParam param; | |||
public void Connect(String host, int port) { | |||
param = TcpParam.create(host, port) | |||
.setTimeout(1000) | |||
.setRetries(0) | |||
.setEncapsulated(false) | |||
.setKeepAlive(true); | |||
ModbusTcpHelper.get().init(param, new ModbusCallback<ModbusMaster>() { | |||
@Override | |||
public void onSuccess(ModbusMaster modbusMaster) { | |||
Log.i("日志", "连接成功"); | |||
} | |||
@Override | |||
public void onFailure(Throwable tr) { | |||
Log.i("日志", "打开失败:" + tr.getMessage()); | |||
} | |||
@Override | |||
public void onFinally() { | |||
} | |||
}); | |||
} | |||
public void ReadShort(int Address, int length, IReadCallBack<short[]> callback) { | |||
ModbusTcpHelper.get().readHoldingRegisters(1, Address, length, new ModbusCallback<ReadHoldingRegistersResponse>() { | |||
@Override | |||
public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) { | |||
short[] data = readHoldingRegistersResponse.getShortData(); | |||
callback.onSuccess(data); | |||
} | |||
@Override | |||
public void onFailure(Throwable tr) { | |||
Log.i("Error", tr.getMessage()); | |||
} | |||
@Override | |||
public void onFinally() { | |||
} | |||
}); | |||
} | |||
public void ReadBool(int Address, int length, IReadCallBack<boolean[]> callback) { | |||
ModbusTcpHelper.get().readCoil(1, Address, length, new ModbusCallback<ReadCoilsResponse>() { | |||
@Override | |||
public void onSuccess(ReadCoilsResponse readCoilsResponse) { | |||
boolean[] data = readCoilsResponse.getBooleanData(); | |||
boolean[] result = Arrays.copyOfRange(data, 0, length); | |||
callback.onSuccess(result); | |||
} | |||
@Override | |||
public void onFailure(Throwable tr) { | |||
Log.i("Error", tr.getMessage()); | |||
} | |||
@Override | |||
public void onFinally() { | |||
} | |||
}); | |||
} | |||
public void ReadFloat(int Address, int length, IReadCallBack<float[]> callback) { | |||
ModbusTcpHelper.get().readHoldingRegisters(1, Address, length * 2, new ModbusCallback<ReadHoldingRegistersResponse>() { | |||
@Override | |||
public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) { | |||
byte[] data = readHoldingRegistersResponse.getData(); | |||
float[] tempValues = new float[length]; | |||
for (int i = 0; i < length; i++) { | |||
byte[] tempData = new byte[4]; | |||
for (int m = 0; m < 4; m++) { | |||
tempData[m] = data[i * 4 + m]; | |||
} | |||
tempValues[i] = ByteBuffer.wrap(tempData).getFloat(); | |||
} | |||
callback.onSuccess(tempValues); | |||
} | |||
@Override | |||
public void onFailure(Throwable tr) { | |||
Log.i("Error", tr.getMessage()); | |||
} | |||
@Override | |||
public void onFinally() { | |||
} | |||
}); | |||
} | |||
public void WriteShort(int Address, short Value) { | |||
short[] send = new short[1]; | |||
send[0] = Value; | |||
ModbusTcpHelper.get().writeRegisters(1, Address, send, new ModbusCallback<WriteRegistersResponse>() { | |||
@Override | |||
public void onSuccess(WriteRegistersResponse writeRegistersResponse) { | |||
Log.i("info", "Shorts 写入成功"); | |||
} | |||
@Override | |||
public void onFailure(Throwable tr) { | |||
Log.i("Error", tr.getMessage()); | |||
} | |||
@Override | |||
public void onFinally() { | |||
} | |||
}); | |||
} | |||
public void WriteBool(int Address, boolean Value) { | |||
ModbusTcpHelper.get().writeCoil(1, Address, Value, new ModbusCallback<WriteCoilResponse>() { | |||
@Override | |||
public void onSuccess(WriteCoilResponse writeCoilResponse) { | |||
Log.i("info", "Booleans 写入成功"); | |||
} | |||
@Override | |||
public void onFailure(Throwable tr) { | |||
Log.i("Error", tr.getMessage()); | |||
} | |||
@Override | |||
public void onFinally() { | |||
} | |||
}); | |||
} | |||
public void WriteFloat(int Address, float Value) { | |||
int intBits = Float.floatToRawIntBits(Value); | |||
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)}; | |||
ModbusTcpHelper.get().writeRegisters(1, Address, send, new ModbusCallback<WriteRegistersResponse>() { | |||
@Override | |||
public void onSuccess(WriteRegistersResponse writeRegistersResponse) { | |||
Log.i("info", "Floats 写入成功"); | |||
} | |||
@Override | |||
public void onFailure(Throwable tr) { | |||
Log.i("Error", tr.getMessage()); | |||
} | |||
@Override | |||
public void onFinally() { | |||
} | |||
}); | |||
} | |||
} |
@@ -1,138 +0,0 @@ | |||
package com.example.bpa.app; | |||
import com.example.bpa.config.ConfigName; | |||
import com.example.bpa.helper.MY; | |||
import com.example.bpa.helper.T; | |||
import com.example.bpa.modbus4j.ModbusParam; | |||
import com.example.bpa.modbus4j.ModbusReq; | |||
import com.example.bpa.modbus4j.OnRequestBack; | |||
import java.text.SimpleDateFormat; | |||
import java.util.Date; | |||
/** | |||
* PLC 控制 | |||
* add fengyoufu 20230414 | |||
*/ | |||
public class PLC { | |||
//region 变量 | |||
/** | |||
* Ip地址 | |||
*/ | |||
public String Host = "192.168.1.14"; | |||
/** | |||
* 端口 | |||
*/ | |||
public int Post = 502; | |||
/** | |||
* 是否已连接 | |||
*/ | |||
public boolean IsConnect = false; | |||
//endregion | |||
//region 私有 | |||
private static PLC mInstance; //实例变量设置私有,防止直接通过类名访问 | |||
public static synchronized PLC getInstance() { //静态同步方法作为唯一的实例对象获取方式 | |||
if (mInstance == null) { | |||
mInstance = new PLC(); | |||
} | |||
return mInstance; | |||
} | |||
private PLC() { //默认构造函数私有,防止类外直接new创建对象 | |||
} | |||
//endregion | |||
//region 外部调用事件 | |||
/** | |||
* 初始化Modbus | |||
*/ | |||
public void InitMobus() { | |||
ModbusReq.getInstance().setParam(new ModbusParam() | |||
.setHost(Host)//TCP PLC_IP地址 | |||
.setPort(Post)//端口默认502 | |||
.setEncapsulated(false) | |||
.setKeepAlive(true) | |||
.setTimeout(2000) | |||
.setRetries(0)) | |||
.init(new OnRequestBack<String>() { | |||
@Override | |||
public void onSuccess(String s) { | |||
T.show(ConfigName.getInstance().dishesCon, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":PLC连接成功!"); | |||
} | |||
@Override | |||
public void onFailed(String msg) { | |||
T.show(ConfigName.getInstance().dishesCon, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":PLC连接失败!" + msg); | |||
} | |||
}); | |||
} | |||
/** | |||
* 写PLC | |||
* | |||
* @param address | |||
* @param value | |||
*/ | |||
public void WritePlcData(String address, Object value) { | |||
byte slaveAddress = 1; | |||
int startAddress = 200;//起始地址 | |||
//startAddress=address; | |||
short[] shorts = new short[2]; | |||
shorts[0] = 100; | |||
shorts[1] = 200; | |||
//32位的float 拆成两个16位的short | |||
// float value = 1000000.52f; | |||
// int intBits = Float.floatToRawIntBits(value); | |||
// int[] send = new int[]{(intBits >>> 16) & 0xFFFF, intBits & 0xFFFF}; | |||
// | |||
// //注意高位低位处理 | |||
// short[] shorts = new short[2]; | |||
// shorts[0]=(short) send[1]; | |||
// shorts[1]=(short) send[0]; | |||
try { | |||
ModbusReq.getInstance().writeRegisters(new OnRequestBack<String>() { | |||
@Override | |||
public void onSuccess(String s) { | |||
} | |||
@Override | |||
public void onFailed(String msg) { | |||
} | |||
}, slaveAddress, startAddress, shorts); | |||
} catch (Exception e) { | |||
T.show(ConfigName.getInstance().dishesCon, "与PLC通信失败"); | |||
} | |||
} | |||
/** | |||
* 读PLC数据 | |||
* @param address | |||
*/ | |||
public void ReadPlcData(String address) { | |||
byte slaveAddress = 1; | |||
int startAddress = 200;//起始地址 | |||
try { | |||
ModbusReq.getInstance().readInputRegisters(new OnRequestBack<short[]>() { | |||
@Override | |||
public void onSuccess(short[] var1) { | |||
} | |||
@Override | |||
public void onFailed(String var1) { | |||
} | |||
}, slaveAddress, startAddress, 2); | |||
} catch (Exception e) { | |||
T.show(ConfigName.getInstance().dishesCon, "与PLC通信失败"); | |||
} | |||
} | |||
//endregion | |||
} |
@@ -1,52 +0,0 @@ | |||
package com.example.bpa.modbus4j; | |||
/** | |||
* Modebus 链接参数 | |||
*/ | |||
public class ModbusParam { | |||
public String host; | |||
public int port = 502; | |||
public boolean encapsulated; | |||
public int timeout = 500; | |||
public int retries = 2; | |||
public boolean keepAlive = true; | |||
public boolean connected = false; | |||
public ModbusParam() { | |||
} | |||
public ModbusParam setHost(String host) { | |||
this.host = host; | |||
return this; | |||
} | |||
public ModbusParam setPort(int port) { | |||
this.port = port; | |||
return this; | |||
} | |||
public ModbusParam setEncapsulated(boolean encapsulated) { | |||
this.encapsulated = encapsulated; | |||
return this; | |||
} | |||
public ModbusParam setTimeout(int timeout) { | |||
this.timeout = timeout; | |||
return this; | |||
} | |||
public ModbusParam setRetries(int retries) { | |||
this.retries = retries; | |||
return this; | |||
} | |||
public ModbusParam setConnected(boolean connected) { | |||
this.connected = connected; | |||
return this; | |||
} | |||
public ModbusParam setKeepAlive(boolean keepAlive) { | |||
this.keepAlive = keepAlive; | |||
return this; | |||
} | |||
} |
@@ -1,364 +0,0 @@ | |||
package com.example.bpa.modbus4j; | |||
import android.util.Log; | |||
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.ReadCoilsRequest; | |||
import com.serotonin.modbus4j.msg.ReadCoilsResponse; | |||
import com.serotonin.modbus4j.msg.ReadDiscreteInputsRequest; | |||
import com.serotonin.modbus4j.msg.ReadDiscreteInputsResponse; | |||
import com.serotonin.modbus4j.msg.ReadExceptionStatusRequest; | |||
import com.serotonin.modbus4j.msg.ReadExceptionStatusResponse; | |||
import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest; | |||
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; | |||
import com.serotonin.modbus4j.msg.ReadInputRegistersRequest; | |||
import com.serotonin.modbus4j.msg.ReadInputRegistersResponse; | |||
import com.serotonin.modbus4j.msg.ReportSlaveIdRequest; | |||
import com.serotonin.modbus4j.msg.ReportSlaveIdResponse; | |||
import com.serotonin.modbus4j.msg.WriteCoilRequest; | |||
import com.serotonin.modbus4j.msg.WriteCoilResponse; | |||
import com.serotonin.modbus4j.msg.WriteCoilsRequest; | |||
import com.serotonin.modbus4j.msg.WriteCoilsResponse; | |||
import com.serotonin.modbus4j.msg.WriteMaskRegisterRequest; | |||
import com.serotonin.modbus4j.msg.WriteMaskRegisterResponse; | |||
import com.serotonin.modbus4j.msg.WriteRegisterRequest; | |||
import com.serotonin.modbus4j.msg.WriteRegisterResponse; | |||
import com.serotonin.modbus4j.msg.WriteRegistersRequest; | |||
import com.serotonin.modbus4j.msg.WriteRegistersResponse; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
public class ModbusReq { | |||
private static final String TAG = ModbusReq.class.getSimpleName(); | |||
private static ModbusReq modbusReq; | |||
private ModbusMaster mModbusMaster; | |||
private ModbusParam modbusParam = new ModbusParam(); | |||
ExecutorService executorService = Executors.newFixedThreadPool(1); | |||
private boolean isInit = false; | |||
private ModbusReq() { | |||
} | |||
public static synchronized ModbusReq getInstance() { | |||
if (modbusReq == null) { | |||
modbusReq = new ModbusReq(); | |||
} | |||
return modbusReq; | |||
} | |||
public ModbusReq setParam(ModbusParam modbusParam) { | |||
this.modbusParam = modbusParam; | |||
return modbusReq; | |||
} | |||
public void init(final OnRequestBack<String> onRequestBack) { | |||
ModbusFactory mModbusFactory = new ModbusFactory(); | |||
IpParameters params = new IpParameters(); | |||
params.setHost(this.modbusParam.host); | |||
params.setPort(this.modbusParam.port); | |||
params.setEncapsulated(this.modbusParam.encapsulated); | |||
this.mModbusMaster = mModbusFactory.createTcpMaster(params, this.modbusParam.keepAlive); | |||
this.mModbusMaster.setRetries(this.modbusParam.retries); | |||
this.mModbusMaster.setTimeout(this.modbusParam.timeout); | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
ModbusReq.this.mModbusMaster.init(); | |||
} catch (ModbusInitException var2) { | |||
ModbusReq.this.mModbusMaster.destroy(); | |||
ModbusReq.this.isInit = false; | |||
Log.d(ModbusReq.TAG, "Modbus4Android init failed " + var2); | |||
onRequestBack.onFailed("Modbus4Android init failed "); | |||
return; | |||
} | |||
Log.d(ModbusReq.TAG, "Modbus4Android init success"); | |||
ModbusReq.this.isInit = true; | |||
onRequestBack.onSuccess("Modbus4Android init success"); | |||
} | |||
}); | |||
} | |||
public void destory() { | |||
modbusReq = null; | |||
this.mModbusMaster.destroy(); | |||
this.isInit = false; | |||
} | |||
public void readCoil(final OnRequestBack<boolean[]> onRequestBack, final int slaveId, final int start, final int len) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
ReadCoilsRequest request = new ReadCoilsRequest(slaveId, start, len); | |||
ReadCoilsResponse response = (ReadCoilsResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
boolean[] booleanData = response.getBooleanData(); | |||
boolean[] resultByte = new boolean[len]; | |||
System.arraycopy(booleanData, 0, resultByte, 0, len); | |||
onRequestBack.onSuccess(resultByte); | |||
} | |||
} catch (ModbusTransportException var5) { | |||
var5.printStackTrace(); | |||
onRequestBack.onFailed(var5.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void readDiscreteInput(final OnRequestBack<boolean[]> onRequestBack, final int slaveId, final int start, final int len) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
ReadDiscreteInputsRequest request = new ReadDiscreteInputsRequest(slaveId, start, len); | |||
ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
boolean[] booleanData = response.getBooleanData(); | |||
boolean[] resultByte = new boolean[len]; | |||
System.arraycopy(booleanData, 0, resultByte, 0, len); | |||
onRequestBack.onSuccess(resultByte); | |||
} | |||
} catch (ModbusTransportException var5) { | |||
var5.printStackTrace(); | |||
onRequestBack.onFailed(var5.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void readHoldingRegisters(final OnRequestBack<short[]> onRequestBack, final int slaveId, final int start, final int len) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, start, len); | |||
ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess(response.getShortData()); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void readInputRegisters(final OnRequestBack<short[]> onRequestBack, final int slaveId, final int start, final int len) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
ReadInputRegistersRequest request = new ReadInputRegistersRequest(slaveId, start, len); | |||
ReadInputRegistersResponse response = (ReadInputRegistersResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess(response.getShortData()); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void writeCoil(final OnRequestBack<String> onRequestBack, final int slaveId, final int offset, final boolean value) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
WriteCoilRequest request = new WriteCoilRequest(slaveId, offset, value); | |||
WriteCoilResponse response = (WriteCoilResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess("Success"); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void writeCoils(final OnRequestBack<String> onRequestBack, final int slaveId, final int start, final boolean[] values) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
WriteCoilsRequest request = new WriteCoilsRequest(slaveId, start, values); | |||
WriteCoilsResponse response = (WriteCoilsResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess("Success"); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void writeRegister(final OnRequestBack<String> onRequestBack, final int slaveId, final int offset, final int value) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
WriteRegisterRequest request = new WriteRegisterRequest(slaveId, offset, value); | |||
WriteRegisterResponse response = (WriteRegisterResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess("Success"); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void writeRegisters(final OnRequestBack<String> onRequestBack, final int slaveId, final int start, final short[] values) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values); | |||
WriteRegistersResponse response = (WriteRegistersResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess("Success"); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void readExceptionStatus(final OnRequestBack<Byte> onRequestBack, final int slaveId) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
ReadExceptionStatusRequest request = new ReadExceptionStatusRequest(slaveId); | |||
ReadExceptionStatusResponse response = (ReadExceptionStatusResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess(response.getExceptionStatus()); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void reportSlaveId(final OnRequestBack<byte[]> onRequestBack, final int slaveId) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
ReportSlaveIdRequest request = new ReportSlaveIdRequest(slaveId); | |||
ReportSlaveIdResponse response = (ReportSlaveIdResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess(response.getData()); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
public void writeMaskRegister(final OnRequestBack<String> onRequestBack, final int slaveId, final int offset, final int and, final int or) { | |||
if (!this.isInit) { | |||
onRequestBack.onFailed("Modbus master is not inited successfully..."); | |||
} else { | |||
this.executorService.submit(new Runnable() { | |||
public void run() { | |||
try { | |||
WriteMaskRegisterRequest request = new WriteMaskRegisterRequest(slaveId, offset, and, or); | |||
WriteMaskRegisterResponse response = (WriteMaskRegisterResponse)ModbusReq.this.mModbusMaster.send(request); | |||
if (response.isException()) { | |||
onRequestBack.onFailed(response.getExceptionMessage()); | |||
} else { | |||
onRequestBack.onSuccess("Success"); | |||
} | |||
} catch (ModbusTransportException var3) { | |||
var3.printStackTrace(); | |||
onRequestBack.onFailed(var3.toString()); | |||
} | |||
} | |||
}); | |||
} | |||
} | |||
} |
@@ -1,6 +0,0 @@ | |||
package com.example.bpa.modbus4j; | |||
public interface OnRequestBack<T> { | |||
void onSuccess(T var1); | |||
void onFailed(String var1); | |||
} |
@@ -2,7 +2,6 @@ package com.example.bpa.view.fragment; | |||
import androidx.annotation.NonNull; | |||
import androidx.annotation.Nullable; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
import androidx.fragment.app.Fragment; | |||
import android.os.Bundle; | |||
@@ -13,8 +12,8 @@ import android.view.ViewGroup; | |||
import android.widget.Button; | |||
import com.example.bpa.R; | |||
import com.example.bpa.app.ModbusTcpHelper; | |||
import com.example.bpa.app.PLC; | |||
import com.example.bpa.app.IReadCallBack; | |||
import com.example.bpa.app.ModbusTcpServer; | |||
/** | |||
* 系统设置界面 | |||
@@ -45,29 +44,61 @@ public class SystemSetFragment extends Fragment { | |||
@Override | |||
public void run() { | |||
try { | |||
ModbusTcpHelper.getInstance().Connect("192.168.1.14", 502); | |||
// PLC.getInstance().InitMobus(); | |||
ModbusTcpServer .get() .Connect("192.168.1.14",502) ; | |||
} catch (Exception e) { | |||
Log.i("错误日志", e.toString()); | |||
} | |||
} | |||
}).start(); | |||
Log.i("日志", "开始连接设备"); | |||
} | |||
}); | |||
ReadBut.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View view) { | |||
PLC.getInstance().ReadPlcData("400001"); | |||
ModbusTcpServer.get().ReadShort(0, 3, new IReadCallBack<short[]>() { | |||
@Override | |||
public void onSuccess(short[] shorts) { | |||
for (int i = 0; i < shorts.length; i++) { | |||
Log.i("Debug", "Shorts[" + i + "]=" + shorts[i]); | |||
} | |||
} | |||
}); | |||
ModbusTcpServer.get().ReadFloat(10, 3, new IReadCallBack<float[]>() { | |||
@Override | |||
public void onSuccess(float[] floats) { | |||
for (int i = 0; i < floats.length; i++) { | |||
Log.i("Debug", "Floats[" + i + "]=" + floats[i]); | |||
} | |||
} | |||
}); | |||
ModbusTcpServer.get().ReadBool(0, 3, new IReadCallBack<boolean[]>() { | |||
@Override | |||
public void onSuccess(boolean[] booleans) { | |||
for (int i = 0; i < booleans.length; i++) { | |||
Log.i("Debug", "Booleans[" + i + "]=" + booleans[i]); | |||
} | |||
} | |||
}); | |||
} | |||
}); | |||
WriteBut.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View view) { | |||
PLC.getInstance().WritePlcData("400001", 10); | |||
ModbusTcpServer.get().WriteBool(0, true); | |||
ModbusTcpServer.get().WriteBool(1, true); | |||
ModbusTcpServer.get().WriteBool(2, true); | |||
ModbusTcpServer.get().WriteShort(0, (short) 10); | |||
ModbusTcpServer.get().WriteShort(1, (short) 20); | |||
ModbusTcpServer.get().WriteShort(2, (short) 30); | |||
ModbusTcpServer.get().WriteFloat(10, 10.10f); | |||
ModbusTcpServer.get().WriteFloat(12, 20.20f); | |||
ModbusTcpServer.get().WriteFloat(14, 30.30f); | |||
} | |||
}); | |||
@@ -12,6 +12,7 @@ dependencyResolutionManagement { | |||
google() | |||
jcenter() | |||
mavenCentral() | |||
maven { url 'https://jitpack.io' } | |||
} | |||
} | |||
rootProject.name = "ChaBaiDao" | |||