Browse Source

PLC数据采集

master
pry 1 year ago
parent
commit
efd620a996
1 changed files with 67 additions and 0 deletions
  1. +67
    -0
      app/src/main/java/com/example/bpa/app/ModbusTcpServer.java

+ 67
- 0
app/src/main/java/com/example/bpa/app/ModbusTcpServer.java View File

@@ -287,4 +287,71 @@ public class ModbusTcpServer {
});
}

public void WriteShort(String Address, short Value) {
int add = GetAddress(Address);
if (add < 0) return;
short[] send = new short[1];
send[0] = Value;
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() {
@Override
public void onSuccess(WriteRegistersResponse writeRegistersResponse) {
MessageLog.ShowInfo("WriteShort onSuccess,Address=" + Address + ",Value=" + Value);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
}

@Override
public void onFinally() {

}
});
}

public void WriteBool(String Address, boolean Value) {
int add = GetAddress(Address);
if (add < 0) return;
ModbusTcpHelper.get().writeCoil(1, add, Value, new ModbusCallback<WriteCoilResponse>() {
@Override
public void onSuccess(WriteCoilResponse writeCoilResponse) {
MessageLog.ShowInfo("WriteBool onSuccess,Address=" + Address + ",Value=" + Value);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
}

@Override
public void onFinally() {

}
});
}

public void WriteFloat(String Address, float Value) {
int add = GetAddress(Address);
if (add < 0) return;
int intBits = Float.floatToRawIntBits(Value);
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)};
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() {
@Override
public void onSuccess(WriteRegistersResponse writeRegistersResponse) {
MessageLog.ShowInfo("WriteFloat onSuccess,Address=" + Address + ",Value=" + Value);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
}

@Override
public void onFinally() {

}
});
}

}

Loading…
Cancel
Save