A
A
Alexey Mukhin2018-11-25 14:57:51
Java
Alexey Mukhin, 2018-11-25 14:57:51

How to set bit values ​​with WriteMultipleCoilsRequest?

I use the Android Studio software environment, the jamod library, the Modbus protocol. I can't set multiple bits using WriteMultipleCoilsRequest and I can't figure out how to form and use BitVector for this. For example, at address 40 I have an 8-bit Coils, its bits need to be written down, tell me how to do this? program text:

public class My_WriteMultipleCoils extends AsyncTask<Void, Void, Void> {
private Exception exception;
BitVector bv = new BitVector(8);

protected Void doInBackground(Void... Params) {
    try {
        addr = InetAddress.getByName(MainActivity.str_adr);
        int port = Modbus.DEFAULT_PORT;
        con = new TCPMasterConnection(addr);
        con.setPort(port);
        con.connect();

        WriteMultipleCoilsRequest m_WriteMultipleCoilsRequest = new WriteMultipleCoilsRequest();
        ModbusTCPTransaction m_Transaction = new ModbusTCPTransaction(con);

        bv.setBit(0,false);
        bv.setBit(1,false);
        bv.setBit(2,false);
        bv.setBit(3,true);
        bv.setBit(4,true);
        bv.setBit(5,true);
        bv.setBit(6,true);
        bv.setBit(7,true);

        m_WriteMultipleCoilsRequest.setUnitID(15);
        m_WriteMultipleCoilsRequest.setReference(40);
        m_WriteMultipleCoilsRequest.setCoils(bv);
        m_Transaction.setRequest(m_WriteMultipleCoilsRequest);
        m_Transaction.execute();

        if (con.isConnected())
            con.close();

    } catch (Exception e) {
        Log.d("TAG.Log", e.toString());
        return null;
    }
    return null;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question