I was able to establish a connection with the PLC to read data from it. Now there is one problem, and I have to write a method to modify the data from the PLC. To do this, I have to send two values to the PLC: int value and boolean value. I got int value resolved through classes from net.wimpi.modbus package. But when it comes to logical meaning, I have no idea what to do.
If someone had the same problem as I have now, could you please send me a link where I can find a solution or a link to a really good tutorial to solve my problem? Someone posted a couple of links in this question , but he sends me to textbooks that have little to do with communication with PLCs and how to process PLC data.
EDIT
I have established communication with the Modicon M340 controller, and for connection I use the classes of the net.wimpi.modbus package. I made a connection in my code through the ModbusTCPTransactionand classes TCPMasterConnection, and I read the values through the ReadMultipleRegistersRequestand classes ReadMultipleRegistersResponse.
The code I made to connect:
private InetAddress m_Address;
private ModbusTCPTransaction m_Transaction = null;
private TCPMasterConnection m_Connection = null;
int port = Modbus.DEFAULT_PORT;
private Activity activity;
public ModbusConnection(Activity activity, String ip)
{
this.activity = activity;
try {
m_Address = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void getTransaction(InetAddress inet) throws Exception
{
int port = Modbus.DEFAULT_PORT;
m_Connection = new TCPMasterConnection(inet);
m_Connection.setPort(port);
m_Connection.connect();
m_Transaction = new ModbusTCPTransaction(m_Connection);
}
, . int, String float , , :
private ReadMultipleRegistersResponse readValues(int offset, int count)
{
ReadMultipleRegistersRequest rReq = null;
ReadMultipleRegistersResponse rRes = null;
try {
rReq = new ReadMultipleRegistersRequest(offset, count);
m_Transaction.setRequest(rReq);
m_Transaction.execute();
rRes = (ReadMultipleRegistersResponse) m_Transaction.getResponse();
} catch (Exception e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: Exception");
}
return rRes;
}
2
, , . 4 , :
ReadCoilsRequest
ReadCoilsResponse
WriteMultipleCoilsRequest
WriteMultileCoilsResponse
, :
private ReadCoilsResponse readBytes(int offset, int count)
{
ReadCoilsRequest rReq = null;
ReadCoilsResponse rRes = null;
try {
rReq = new ReadCoilsRequest(offset, count);
m_Transaction.setRequest(rReq);
m_Transaction.execute();
rRes = (ReadCoilsResponse) m_Transaction.getResponse();
} catch (Exception e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: Exception");
}
return rRes;
}
public void writeBytes(int wordNumber, BitVector b) {
try {
WriteMultipleCoilsRequest wReq = null;
WriteMultipleCoilsResponse wRes = null;
wReq = new WriteMultipleCoilsRequest(211, b);
m_Transaction.setRequest(wReq);
m_Transaction.execute();
} catch (Exception e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: Exception");
}
}
, BitVector Coils:
public BitVector readBitVector(int offset, int count)
{
BitVector myBitVector;
ReadCoilsResponse rRes = readBytes(offset, count);
rRes = (ReadCoilsResponse) m_Transaction.getResponse();
myBitVector = rRes.getCoils();
return myBitVector;
}
, 1 0, BitVector net.wimpi.modbus.util :
test.setBit(2, true);
. , , plc, - .