Browse Source

添加下位设备操作的类,以及中英文字符串文件。

味魔方Android
Nah 11 months ago
parent
commit
1e67707b12
3 changed files with 1016 additions and 0 deletions
  1. +340
    -0
      app/src/main/java/com/bonait/bnframework/business/devicesControl/PLCControl.java
  2. +632
    -0
      app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java
  3. +44
    -0
      app/src/main/res/values-en/strings.xml

+ 340
- 0
app/src/main/java/com/bonait/bnframework/business/devicesControl/PLCControl.java View File

@@ -0,0 +1,340 @@
package com.bonait.bnframework.business.devicesControl;

import android.os.Handler;
import android.os.Looper;

import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS;
import com.bonait.bnframework.common.helper.DataFormat;
import com.bonait.bnframework.common.helper.I.IWriteCallBack;
import com.bonait.bnframework.common.modbus.ModbusRTUServer;
import com.bonait.bnframework.common.utils.ToastUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class PLCControl {
//region 单例模式
private static volatile PLCControl instance = null;

public static PLCControl get() {
PLCControl manager = instance;
if (manager == null) {
synchronized (PLCControl.class) {
manager = instance;
if (manager == null) {
manager = new PLCControl();
instance = manager;
}
}
}
return manager;
}
private PLCControl() {
}
//endregion

/**语音模块的从站地址,这个最好不改,设备端不好修改默认地址*/
private final int voiceAddress=1;
/**电子秤的从站地址*/
private final int scaleAddress=2;
/**PLC的从站地址*/
private final int PLCAddress=3;

//region 语音模块控制 , 从站地址:1

public void Speak(String content,IWriteCallBack callBack){
try {
if (!content.isEmpty() /*&& !ExecuteTheRecipe.isTimeOut*/) {
int address = 0;
short[] values=getGB2312(content);
ModbusRTUServer.get().WriteString(voiceAddress, address, values, callBack);
}
}
catch (Exception ex) {
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

//endregion

//region 电子秤控制, 从站地址:2

/**获取称当前重量、当前AD值、当前零位,文档建议150ms以上轮询一次*/
public int getCurrentWeight(){
//int readLength=3;
int[] result=new int[]{0};
try {
ModbusRTUServer.get().ReadInts(scaleAddress, 0, 1, DataFormat.BADC, val-> {
result[0]=val[0];
});
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}finally {
return result[0];
}
}

/**写入砝码重量*/
public void writeCounterWeight(short counterWeight,IWriteCallBack callBack){
try {
ModbusRTUServer.get().WriteShort(scaleAddress, 6,counterWeight,callBack);
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

/**去皮*/
public void tare(IWriteCallBack callBack){
try {
ModbusRTUServer.get().WriteShort(scaleAddress, 21,(short) 1,callBack);
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

/**恢复去皮*/
public void cancelTare(IWriteCallBack callBack){
try {
ModbusRTUServer.get().WriteShort(scaleAddress, 21,(short) 2,callBack);
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

/**零点校准,写入前需要先关闭写保护*/
public void zeroScale(IWriteCallBack callBack){
try {
ModbusRTUServer.get().WriteShort(scaleAddress, 22,(short) 1,callBack);
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

/**恢复出厂设置,写入前需要先关闭写保护*/
public void factoryReset(IWriteCallBack callBack){
try {
ModbusRTUServer.get().WriteShort(scaleAddress, 22,(short) 9,callBack);
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

/**打开写保护*/
public void openWriteProtect(IWriteCallBack callBack){
try {
ModbusRTUServer.get().WriteShort(scaleAddress, 23,(short) 0,callBack);
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

/**关闭写保护,零点校准、写入砝码重量、恢复出厂设置都需要先执行此操作*/
public void closeWriteProtect(IWriteCallBack callBack){
try {
ModbusRTUServer.get().WriteShort(scaleAddress, 23,(short) 1,callBack);
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}
}

//endregion

//region PLC控制 设备从站地址:3



/**复位PLC所有输出以及心跳,[如果输出更换了点位的话,无法复位所有电机]*/
public void initalPLC(IWriteCallBack callBack){
try {
short[] values = new short[14];

final boolean[] isSuccess={false,false,false};

int startAddress = ModbusRTUServer.GetAddress("D10");
ModbusRTUServer.get().WriteShorts(PLCAddress, startAddress, values, new IWriteCallBack() {
@Override
public void onSuccess() {
isSuccess[0]=true;
}

@Override
public void onFailure(String ErrorMsg) {

}
});

int timeoutFlag = ModbusRTUServer.GetAddress("M0");
ModbusRTUServer.get().WriteBool(PLCAddress, timeoutFlag, false, new IWriteCallBack() {
@Override
public void onSuccess() {
isSuccess[1]=true;
}

@Override
public void onFailure(String ErrorMsg) {

}
});

clrHeartbeat("心跳时间", new IWriteCallBack() {
@Override
public void onSuccess() {
isSuccess[2]=true;
}

@Override
public void onFailure(String ErrorMsg) {

}
});
if (callBack != null) {
if (isSuccess[0] && isSuccess[1] && isSuccess[2]){
callBack.onSuccess();
}else{
callBack.onFailure("Initial Failure");
}
}
} catch (Exception ex) {
if (callBack != null) {
callBack.onFailure(ex.getMessage());
}
}

}

/**打开电机,指定时间后关闭
* @param delayTime 关闭延迟时间,单位:ms
* */
public void openMotorAndClose(int index,int delayTime,IWriteCallBack callBack){
try {
openMotor(index, new IWriteCallBack() {
@Override
public void onSuccess() {
new Handler(Looper.getMainLooper()).postDelayed(()->{
closeMotor(index, new IWriteCallBack() {
@Override
public void onSuccess() {
callBack.onSuccess();
}

@Override
public void onFailure(String ErrorMsg) {
callBack.onFailure(ErrorMsg);
}
});
},delayTime);
}
@Override
public void onFailure(String ErrorMsg) {
callBack.onFailure(ErrorMsg);
}
});
} catch (Exception ex) {
ToastUtils.error("OpenMotorAndClose Exception:" + ex.getMessage());
}
}

/**打开电机*/
public void openMotor(int index, IWriteCallBack callBack) {
String name="通道"+index+"开关";
try {
if (ConfigName.getInstance().PLC_Address.containsKey(name)) {
BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name);
if (!plcaddress.address.isEmpty() && !ExecuteTheRecipe.isTimeOut) {
int address = ModbusRTUServer.GetAddress(plcaddress.address);
ModbusRTUServer.get().WriteShort(PLCAddress, address, (short)1, callBack);
}
}
} catch (Exception ex) {
ToastUtils.error("OpenMotor Exception:" + ex.getMessage());
}
}

/**关闭电机*/
public void closeMotor(int index, IWriteCallBack callBack) {
String name="通道"+index+"开关";
try {
if (ConfigName.getInstance().PLC_Address.containsKey(name)) {
BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name);
if (!plcaddress.address.isEmpty() && !ExecuteTheRecipe.isTimeOut) {
int address = ModbusRTUServer.GetAddress(plcaddress.address);
ModbusRTUServer.get().WriteShort(PLCAddress, address, (short)0, callBack);
}
}
} catch (Exception ex) {
ToastUtils.error("CloseMotor Exception:" + ex.getMessage());}
}

/**获取所有电机的状态,为true表示正在转,反之,为false。*/
public boolean[] getAllMotorStatus(){
//一共有14个电机
int length = 14;
boolean[] result = new boolean[length];
try {
BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get("通道1开关");
if (!plcaddress.address.isEmpty() && !ExecuteTheRecipe.isTimeOut) {
int startAddress = ModbusRTUServer.GetAddress(plcaddress.address);
ModbusRTUServer.get().ReadShorts(PLCAddress, startAddress, length, val -> {
for (int i = 0; i < length; i++) {
result[i] = val[i] > 0 ? true : false;
}
});
}
}catch (Exception ex){
ToastUtils.error("GetAllMotorStatus Exception:" + ex.getMessage());
}finally {
return result;
}
}

/**复位心跳*/
public void clrHeartbeat(String name,IWriteCallBack callBack){
try {
if (ConfigName.getInstance().PLC_Address.containsKey(name)) {
BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name);
if (!plcaddress.address.isEmpty()/* && !ExecuteTheRecipe.isTimeOut*/) {
int address = ModbusRTUServer.GetAddress(plcaddress.address);
ModbusRTUServer.get().WriteShort(PLCAddress , address ,(short) 0 , callBack);
}
}
} catch (Exception ex) {
ToastUtils.error("ClrHeartbeat Exception:" + ex.getMessage());
}
}

/**读取是否超时*/
public boolean isTimeOut(String name){
final boolean[] result = {true};
try{
if (ConfigName.getInstance().PLC_Address.containsKey(name)) {
BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name);
if (!plcaddress.address.isEmpty() /*&& ConfigName.getInstance().SerialIsConnect*/) {
ModbusRTUServer.get().ReadBools(PLCAddress, plcaddress.address, 1, val-> {
result[0] =val[0];
});
}
}
}catch (Exception ex){
ToastUtils.error("Exception message:" + ex.getMessage());
}finally {
return result[0];
}
}
//endregion



/**字符串转换为GB2312编码,需要测试,可能不对*/
public short[] getGB2312(String str){
try{
byte[] bytes=str.getBytes("gb2312");
short[] result=new short[bytes.length/2];
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer().get(result);
return result;
}catch (Exception e){
return null;
}
}
}

+ 632
- 0
app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java View File

@@ -0,0 +1,632 @@
package com.bonait.bnframework.common.modbus;

import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.helper.DataFormat;
import com.bonait.bnframework.common.helper.I.IReadCallBack;
import com.bonait.bnframework.common.helper.I.IWriteCallBack;
import com.bonait.bnframework.common.helper.MessageLog;
import com.licheedev.modbus4android.ModbusCallback;
import com.licheedev.modbus4android.ModbusParam;
import com.licheedev.modbus4android.ModbusRespException;
import com.licheedev.modbus4android.param.SerialParam;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ReadCoilsResponse;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;

public class ModbusRTUServer {
//region 单例模式
private static volatile ModbusRTUServer instance = null;

public static ModbusRTUServer get() {
ModbusRTUServer manager = instance;
if (manager == null) {
synchronized (ModbusRTUServer.class) {
manager = instance;
if (manager == null) {
manager = new ModbusRTUServer();
instance = manager;
}
}
}
return manager;
}
private ModbusRTUServer() {
}
//endregion


static ModbusParam param;

/**适用于三菱PLC,只有X,Y,M,D区*/
public static int GetAddress(String address){
if (address == null) return -1;
address = address.toUpperCase().trim();

try {
if (address.length() < 2) return Integer.parseInt(address);

String res = address.substring(1);
if (res == null) return -1;

int startAddress=Integer.parseInt(res) ;
//如果是M区地址。
if (address.contains("M")) {
return startAddress + 4000;
} else if (address.contains("D")) {
return startAddress;
}else if(address.contains("X")){
return Integer.parseInt(res,8);
}else if(address.contains("Y")){
return Integer.parseInt(res,8)+300;
}else{
return Integer.parseInt(address);
}
} catch (Exception e) {
return -1;
}
}


/** 连接PLC*/
public void ConnectPLC()
{
int baudRate=ConfigName.getInstance().SerialBaudRate;
int dataBits=ConfigName.getInstance().SerialDataBits;
int parity=ConfigName.getInstance().SerialParity;
int stopBits=ConfigName.getInstance().SerialStopBits;
//String com="COM0";

param = SerialParam.create("/dev/ttyCOM1", baudRate) // 串口地址和波特率
.setDataBits(dataBits) // 数据位
.setParity(parity) // 校验位
.setStopBits(stopBits) // 停止位
.setTimeout(3000).setRetries(0); // 不重试

ModbusTcpHelper.get().init(param, new ModbusCallback<ModbusMaster>() {
@Override
public void onSuccess(ModbusMaster modbusMaster) {
//ToastUtils.info("串口 ["+com+"] 连接成功");

//MessageLog.ShowInfo("串口 ["+com+"] 连接成功");
ConfigName.getInstance().SerialIsConnect = true;
ConfigData.getInstance().PLC_Init();
}

@Override
public void onFailure(Throwable tr) {
ConfigName.getInstance().SerialIsConnect = false;
//ToastUtils.info("串口 ["+com+"] 连接失败:" + tr.getMessage());
//MessageLog.ShowError("串口 ["+com+"] 连接失败:" + tr.getMessage());
}
@Override
public void onFinally() {
}
});
}


//region 数据类型转换
private Float BytesToFloat(byte[] buffers, DataFormat df) {
if (buffers.length == 4) {
byte[] bytes = new byte[4];
if (df == DataFormat.ABCD) {
bytes[0] = buffers[3];
bytes[1] = buffers[2];
bytes[2] = buffers[1];
bytes[3] = buffers[0];
} else if (df == DataFormat.CDAB) {
bytes[0] = buffers[1];
bytes[1] = buffers[0];
bytes[2] = buffers[3];
bytes[3] = buffers[2];
} else if (df == DataFormat.BADC) {
bytes[0] = buffers[2];
bytes[1] = buffers[3];
bytes[2] = buffers[0];
bytes[3] = buffers[1];
} else if (df == DataFormat.DCBA) {
bytes[0] = buffers[0];
bytes[1] = buffers[1];
bytes[2] = buffers[2];
bytes[3] = buffers[3];
}
return ByteBuffer.wrap(bytes).getFloat();
}
return 0.0f;
}

private Integer BytesToInt(byte[] buffers, DataFormat df) {
if (buffers.length == 4) {
byte[] bytes = new byte[4];
if (df == DataFormat.ABCD) {
bytes[0] = buffers[3];
bytes[1] = buffers[2];
bytes[2] = buffers[1];
bytes[3] = buffers[0];
} else if (df == DataFormat.CDAB) {
bytes[0] = buffers[1];
bytes[1] = buffers[0];
bytes[2] = buffers[3];
bytes[3] = buffers[2];
} else if (df == DataFormat.BADC) {
bytes[0] = buffers[2];
bytes[1] = buffers[3];
bytes[2] = buffers[0];
bytes[3] = buffers[1];
} else if (df == DataFormat.DCBA) {
bytes[0] = buffers[0];
bytes[1] = buffers[1];
bytes[2] = buffers[2];
bytes[3] = buffers[3];
}
return ByteBuffer.wrap(bytes).getInt();
}
return 0;
}

private byte[] IntToByte(int number) {
int temp = number;
byte[] b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8; // 向右移8位
}
return b;
}

private short[] IntToShorts(int value) {
short[] res = new short[2];
int temp = value;
byte[] b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8; // 向右移8位
}
for (int i = 0; i < res.length; i++) {
short s0 = (short) (b[i * 2] & 0xff);// 最低位
short s1 = (short) (b[i * 2 + 1] & 0xff);
s1 <<= 8;
res[i] = (short) (s0 | s1);
}
return res;
}

public static String GetString(short[] src, int start, int len) throws UnsupportedEncodingException {
short[] temp = new short[len];
for (int i = 0; i < len; i++) {
temp[i] = src[i + start];
}
byte[] bytesTemp = shorts2Bytes(temp);
for (int i = 0; i < bytesTemp.length; i++) {
byte b = bytesTemp[i];
}
String str = new String(bytesTemp, "UTF-8");
return str;
}

public static byte[] shorts2Bytes(short[] data) {
byte[] byteValue = new byte[data.length * 2];
for (int i = 0; i < data.length; i++) {
byteValue[i * 2] = (byte) (data[i] & 0xff);
byteValue[i * 2 + 1] = (byte) ((data[i] & 0xff00) >> 8);
}
return byteValue;
}
//endregion

//region 读取及写入方法

/**写入字符串*/
public void WriteString(int slaveAddress,String Address, int length, IWriteCallBack callback) {
int add = GetAddress(Address);
if (add < 0) return;
short[] send = new short[length];
for (short item : send) {
item = 0;
}
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, add, send);
if (callback != null)
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}

/**写入字符串*/
public void WriteString(int slaveAddress,int Address, short[] values, IWriteCallBack callback) {
if (slaveAddress<0) return;
if (Address < 0) return;
if (values ==null || values.length==0) return;
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, values);
if (callback != null)
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteString onFailure,Address=" + Address + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}


/**根据地址批量读取16位寄存器*/
public void ReadShorts(int slaveAddress,int Address, int length, IReadCallBack<short[]> callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
try {
ReadHoldingRegistersResponse res = ModbusTcpHelper.get()
.syncReadHoldingRegisters(slaveAddress, Address, length);
short[] data = res.getShortData();
if (data.length == length) {
if (callback != null) callback.onSuccess(data);
}
} catch (InterruptedException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void ReadBools(int slaveAddress,String Address, int length, IReadCallBack<boolean[]> callback) {
int add = GetAddress(Address);
if (add < 0) return;
try {
ReadCoilsResponse res = ModbusTcpHelper.get()
.syncReadCoil(slaveAddress, add, length);
boolean[] data = res.getBooleanData();
boolean[] result = Arrays.copyOfRange(data, 0, length);
if (result.length == length) {
if (callback != null) callback.onSuccess(result);
}
} catch (InterruptedException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}catch (Exception e){
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void ReadFloats(int slaveAddress,int Address, int length, IReadCallBack<float[]> callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
try {
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(slaveAddress, Address, length*2);
byte[] data = res.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] = BytesToFloat(tempData, DataFormat.BADC);
}
if (tempValues.length == length) {
if (callback != null) callback.onSuccess(tempValues);
}
} catch (InterruptedException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void ReadInts(int slaveAddress,int Address, int length,DataFormat dataFormat, IReadCallBack<int[]> callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
try {
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(slaveAddress, Address, length * 2);
byte[] data = res.getData();
int[] tempValues = new int[length];
if(data.length>=4)
{
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] = BytesToInt(tempData, dataFormat);
}
}else
{
MessageLog.ShowError("ReadInt onFailure,Address=" );

}
if (callback != null) callback.onSuccess(tempValues);
} catch (InterruptedException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void WriteShort(int slaveAddress,int Address, short Value, IWriteCallBack callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
short[] send = new short[1];
send[0] = Value;
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);
if (callback != null)
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}

public void WriteShorts(int slaveAddress,int Address, short[] Value, IWriteCallBack callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, Value);
if (callback != null)
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}

public void WriteBool(int slaveAddress,int Address, boolean Value, IWriteCallBack callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
try {
ModbusTcpHelper.get().syncWriteCoil(slaveAddress, Address, Value);
if (callback != null)
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}

public void WriteBools(int slaveAddress,int Address, boolean[] Value, IWriteCallBack callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
try {
ModbusTcpHelper.get().syncWriteCoils(slaveAddress, Address, Value);
if (callback != null)
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}
public void WriteFloat(int slaveAddress,int Address, float Value, IWriteCallBack callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
int intBits = Float.floatToRawIntBits(Value);
short[] send = IntToShorts(intBits);
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);

if (callback != null) callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}

public void WriteInt(int slaveAddress,int Address, int Value, IWriteCallBack callback) {
//int add = GetAddress(Address);
if (Address < 0) return;
short[] send = IntToShorts(Value);
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);
if (callback != null) callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
if (callback != null) callback.onFailure(e.toString());
}
}

public void WriteShort(int slaveAddress,int Address, short Value) {
//int add = GetAddress(Address);
if (Address < 0) return;
short[] send = new short[1];
send[0] = Value;
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

public void WriteBool(int slaveAddress,int Address, boolean Value) {
//int add = GetAddress(Address);
if (Address < 0) return;

try {
ModbusTcpHelper.get().syncWriteCoil(slaveAddress, Address, Value);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

public void WriteFloat(int slaveAddress,int Address, float Value) {
//int add = GetAddress(Address);
if (Address < 0) return;
int intBits = Float.floatToRawIntBits(Value);
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)};
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

public void WriteInt(int slaveAddress,int Address, int Value) {
//int add = GetAddress(Address);
if (Address < 0) return;
short[] send = IntToShorts(Value);
try {
ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

//endregion
}

+ 44
- 0
app/src/main/res/values-en/strings.xml View File

@@ -0,0 +1,44 @@
<resources>
<string name="app_name">菠萝智慧吧</string>
<string name="file_normal">normal</string>
<string name="file_all">all files</string>
<string name="empty_data">empty data</string>
<string name="not_available">sd card not avaliable</string>
<string name="already_select">selected:%s</string>
<string name="file_select_res">确定%s</string>
<string name="file_select_max">最多选择%d个文件</string>
<string name="file_select_res1">Save locally%s</string>


<string name="welcome_use">Welcome!</string>
<string name="user_account">Please enter username</string>
<string name="user_password">Please enter password</string>
<string name="remember_pwd">Remember you</string>
<string name="forget_pwd">Forget password?</string>
<string name="login">Login</string>


<string name="menu_func">Function</string>
<string name="menu_func_siloManagement">SilosManage</string>
<string name="menu_func_RecipeManagement">RecipeManage</string>
<string name="menu_func_LogManagement">LogManage</string>
<string name="menu_func_BaseDataManagement">BaseDataManage</string>
<string name="menu_func_RealTimeMonitorManagement">RealTimeMonitorManage</string>


<string name="app_initing">Software initialization in progress, please wait...</string>
<string name="func_one_button_full_pipe">Full Pipe</string>
<string name="func_one_button_full_tank">Set Full Tank</string>
<string name="func_one_button_clean">Clean Pipe</string>
<string name="func_one_button_setup">Set Silo Info</string>
<string name="func_msg_one">Click on the silo icon to configure or control the silo</string>
<string name="func_msg_two">A warning will be triggered when the material in the silo falls below the minimum level.</string>


<string name="serialBaudRate">SerialBaudRate:</string>
<string name="serialDataBits">SerialDataBits:</string>
<string name="serialParity">SerialParity:</string>
<string name="serialStopBits">SerialStopBits:</string>


</resources>

Loading…
Cancel
Save