package com.xc.candyfloss.modbus;
import android.util.Log;
import android_serialport_api.SerialPort;
import com.xc.candyfloss.modbus.exception.ModbusError;
import com.xc.candyfloss.modbus.exception.ModbusErrorType;
import com.xc.candyfloss.modbus.utilities.ByteArrayReader;
import com.xc.candyfloss.modbus.utilities.ByteArrayWriter;
import com.xc.candyfloss.modbus.utilities.CRC16;
import com.xc.candyfloss.modbus.utilities.ThreadUtil;
import com.xc.candyfloss.modbus.utilities.TimeoutUtil;
import java.io.IOException;
import java.util.BitSet;
public class ModbusMaster {
private SerialPort port;
private int timeout;
public ModbusMaster(SerialPort arg2) {
super();
this.timeout = 2000;
this.port = arg2;
}
static SerialPort access$000(ModbusMaster arg0) {
return arg0.port;
}
public int[] execute(int arg17, int arg18, int arg19, int arg20, int arg21) throws IOException, ModbusError {
StringBuilder v3_2;
ModbusErrorType v2_2;
int[] v7_1;
ByteArrayReader v6_2;
byte[] v8_2;
int v13;
StringBuilder v4;
ModbusErrorType v3_1;
int v12;
int v11;
int v8;
int v7;
ByteArrayWriter v6_1;
ModbusMaster v1 = this;
int v0 = arg17;
int v2 = arg18;
int v3 = arg20;
__monitor_enter(this);
int v5 = arg19 - 1;
if(v0 >= 0) {
int v6 = 0xFF;
if(v0 <= v6) {
if(v5 >= 0 && v5 <= 0xFFFF) {
if(v3 >= 1 && v3 <= v6) {
try {
v6_1 = new ByteArrayWriter();
v6_1.writeInt8(v0);
v7 = 6;
v8 = 3;
int v9 = 4;
int v10 = 2;
v11 = 5;
v12 = 0;
if(v2 == 1 || v2 == v10) {
v6_1.writeInt8(v2);
v6_1.writeInt16(v5);
v6_1.writeInt16(v3);
v13 = (((int)Math.ceil((((double)v3)) * 0.1 / 8))) + 5;
label_71:
v7 = 1;
}
else {
if(v2 != v9) {
if(v2 == v8) {
}
else {
if(v2 != v11) {
if(v2 == v7) {
}
else {
v3_1 = ModbusErrorType.ModbusFunctionNotSupportedError;
v4 = new StringBuilder();
v4.append("Not support function ");
v4.append(v2);
throw new ModbusError(v3_1, v4.toString());
}
}
v13 = v2 != v11 || arg21 == 0 ? arg21 : 0xFF00;
v6_1.writeInt8(v2);
v6_1.writeInt16(v5);
v6_1.writeInt16(v13);
v13 = 8;
v7 = 0;
goto label_72;
}
}
v6_1.writeInt8(v2);
v6_1.writeInt16(v5);
v6_1.writeInt16(v3);
v13 = v3 * 2 + v11;
goto label_71;
}
label_72:
v6_1.writeInt16Reversal(CRC16.compute(v6_1.toByteArray()));
v1.port.getOutputStream().write(v6_1.toByteArray());
v6_1 = new ByteArrayWriter();
v8 = 150;
}
catch(Throwable v0_1) {
goto label_244;
}
try {
ThreadUtil.sleep(v8);
boolean[] v8_1 = new boolean[1];
boolean v14 = TimeoutUtil.execute(new Runnable(v8_1, v6_1, v13) {
public void run() {
int v0 = 0x40;
try {
byte[] v0_2 = new byte[v0];
while(!this.val$complete[0]) {
if(ModbusMaster.this.port.getInputStream().available() > 0) {
int v1 = ModbusMaster.this.port.getInputStream().read(v0_2, 0, v0_2.length);
if(v1 > 0) {
this.val$response.write(v0_2, 0, v1);
if(this.val$response.size() >= this.val$finalExpected_length) {
return;
}
}
}
ThreadUtil.sleep(1);
}
}
catch(Exception v0_1) {
v0_1.printStackTrace();
}
}
}, v1.timeout);
v8_1[0] = true;
v6_1.flush();
if(!v14) {
goto label_195;
}
v8_2 = v6_1.toByteArray();
}
catch(Throwable v0_1) {
goto label_206;
}
try {
v6_1.close();
if(v8_2 != null && v8_2.length == v13) {
v6_2 = new ByteArrayReader(v8_2);
v13 = v6_2.readInt8();
if(v13 != v0) {
goto label_173;
}
else if(v6_2.readInt8() <= 0x80) {
if(v7 != 0) {
v0 = v6_2.readInt8();
v7 = v8_2.length - v11;
if(v0 == v7) {
}
else {
v3_1 = ModbusErrorType.ModbusInvalidResponseError;
throw new ModbusError(v3_1, String.format("Byte count is %d while actual number of bytes is %d. ", Integer.valueOf(v0), Integer.valueOf(v7)));
}
}
else {
v0 = 0;
}
v7_1 = new int[v3];
if(v2 == 1 || v2 == v10) {
byte[] v2_1 = new byte[v0];
for(v5 = 0; v5 < v0; ++v5) {
v2_1[v5] = ((byte)v6_2.readInt8());
}
BitSet v0_2 = BitSet.valueOf(v2_1);
for(v2 = 0; true; ++v2) {
if(v2 >= v3) {
goto label_167;
}
v5 = v0_2.get(v2) ? 1 : 0;
v7_1[v2] = v5;
}
}
else {
if(v2 != v9) {
if(v2 == 3) {
}
else {
if(v2 != v11 && v2 != 6) {
goto label_167;
}
v7_1[0] = v6_2.readInt16();
goto label_167;
}
}
while(true) {
if(v12 >= v3) {
goto label_167;
}
v0 = v6_2.readInt16();
v7_1[v12] = v0 <= 0x7FFF || v5 == 643 ? v0 : v0 - 0x10000;
++v12;
}
}
goto label_167;
}
else {
goto label_169;
}
}
goto label_184;
}
catch(Throwable v0_1) {
goto label_244;
}
label_167:
__monitor_exit(this);
return v7_1;
try {
label_169:
throw new ModbusError(v6_2.readInt8());
label_173:
v3_1 = ModbusErrorType.ModbusInvalidResponseError;
throw new ModbusError(v3_1, String.format("Response slave %d is different from request slave %d", Integer.valueOf(v13), Integer.valueOf(arg17)));
label_184:
v2_2 = ModbusErrorType.ModbusInvalidResponseError;
v3_2 = new StringBuilder();
v3_2.append("Response length is invalid ");
v3_2.append(v8_2.length);
throw new ModbusError(v2_2, v3_2.toString());
}
catch(Throwable v0_1) {
goto label_244;
}
try {
label_195:
throw new ModbusError(ModbusErrorType.ModbusTimeoutError, String.format("Timeout of %d ms.", Integer.valueOf(v1.timeout)));
}
catch(Throwable v0_1) {
label_206:
Throwable v2_3 = v0_1;
try {
v6_1.close();
goto label_212;
}
catch(Throwable v0_1) {
Throwable v3_3 = v0_1;
try {
v2_3.addSuppressed(v3_3);
label_212:
throw v2_3;
label_213:
v2_2 = ModbusErrorType.ModbusInvalidArgumentError;
v4 = new StringBuilder();
v4.append("Invalid quantity_of_x ");
v4.append(v3);
throw new ModbusError(v2_2, v4.toString());
label_223:
v2_2 = ModbusErrorType.ModbusInvalidArgumentError;
v3_2 = new StringBuilder();
v3_2.append("Invalid starting_address ");
v3_2.append(v5);
throw new ModbusError(v2_2, v3_2.toString());
label_233:
v3_1 = ModbusErrorType.ModbusInvalidArgumentError;
v4 = new StringBuilder();
v4.append("Invalid slave ");
v4.append(v0);
throw new ModbusError(v3_1, v4.toString());
}
catch(Throwable v0_1) {
label_244:
__monitor_exit(this);
throw v0_1;
}
}
}
}
goto label_213;
}
goto label_223;
}
}
goto label_233;
}
public boolean readCoil(int arg2, int arg3) throws IOException, ModbusError {
int[] v2 = this.readCoils(arg2, arg3, 1);
boolean v3 = false;
if(v2[0] > 0) {
v3 = true;
}
return v3;
}
public int[] readCoils(int arg7, int arg8, int arg9) throws IOException, ModbusError {
return this.execute(arg7, 1, arg8, arg9, 0);
}
public int readHoldingRegister(int arg2, int arg3) throws IOException, ModbusError {
return this.readHoldingRegisters(arg2, arg3, 1)[0];
}
public int[] readHoldingRegisters(int arg7, int arg8, int arg9) throws IOException, ModbusError {
return this.execute(arg7, 3, arg8, arg9, 0);
}
public boolean readInput(int arg2, int arg3) throws IOException, ModbusError {
int[] v2 = this.readInputs(arg2, arg3, 1);
boolean v3 = false;
if(v2[0] > 0) {
v3 = true;
}
return v3;
}
public int readInputRegister(int arg2, int arg3) throws IOException, ModbusError {
return this.readInputRegisters(arg2, arg3, 1)[0];
}
public int[] readInputRegisters(int arg7, int arg8, int arg9) throws IOException, ModbusError {
return this.execute(arg7, 4, arg8, arg9, 0);
}
public int[] readInputs(int arg7, int arg8, int arg9) throws IOException, ModbusError {
return this.execute(arg7, 2, arg8, arg9, 0);
}
public void setTimeout(int arg1) {
this.timeout = arg1;
}
public void writeSingleCoil(int arg7, int arg8, boolean arg9) throws IOException, ModbusError {
this.execute(arg7, 5, arg8, 1, arg9);
}
public void writeSingleRegister(int arg9, int arg10, int arg11) throws IOException, ModbusError {
Log.e("writeSingleRegister", "address:" + arg10 + "--value:" + arg11);
this.execute(arg9, 6, arg10, 1, arg11);
}
}
请修正代码
最新发布