从Android向PLC发送布尔值

Mar*_*nez 6 java android boolean plc modbus

我能够与PLC建立连接以从中读取数据.现在有一个问题,那就是我必须编写一个方法来修改PLC中的数据.为此,我必须向PLC发送两个值:int值和布尔值.我通过net.wimpi.modbus包中的类解决了int值.但是当谈到布尔值时,我不知道该怎么做.

如果有人遇到和我现在一样的问题,你能不能给我一个参考资料,在那里我可以找到一个解决方案或一个非常好的教程链接来解决我的问题?有人在这个问题上贴了几个链接,但是它发给我的教程与PLC的通信以及如何处理PLC的数据没什么关系.

编辑

我与Modicon M340 PLC建立了连接,对于连接,我使用了net.wimpi.modbus包的类.我发过班,我的代码的连接ModbusTCPTransactionTCPMasterConnection,和我通过阅读类的价值观ReadMultipleRegistersRequestReadMultipleRegistersResponse.

我为连接编写的代码:

    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) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // the slave'saddress
}

public void getTransaction(InetAddress inet) throws Exception
{
    /*Variables for the reading of the PLC data*/
    int port = Modbus.DEFAULT_PORT;

    /*Data initialization for the reading of the PLC data*/
    m_Connection = new TCPMasterConnection(inet);
    m_Connection.setPort(port);
    m_Connection.connect();
    m_Transaction = new ModbusTCPTransaction(m_Connection);
}
Run Code Online (Sandbox Code Playgroud)

为了读取值,我会一直调用下一个代码.我只通过从PLC上声明的偏移读取的单词读取和写入int,String和float值:

    private ReadMultipleRegistersResponse readValues(int offset, int count) 
{
    ReadMultipleRegistersRequest rReq = null; // the request
    ReadMultipleRegistersResponse rRes = null; // the response

    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;    
}
Run Code Online (Sandbox Code Playgroud)

编辑2

我想我完成了我想要的东西.我用4个类读取线圈:

ReadCoilsRequest ReadCoilsResponse WriteMultipleCoilsRequest WriteMultileCoilsResponse

我做的是两种读取和写入PLC的线圈的方法:

    private ReadCoilsResponse readBytes(int offset, int count) 
{
    ReadCoilsRequest rReq = null; // the request
    ReadCoilsResponse rRes = null; // the response

    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");
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,我使用Coils类创建了一个读取BitVector变量的方法:

    public BitVector readBitVector(int offset, int count) 
{
    BitVector myBitVector;  

    ReadCoilsResponse rRes = readBytes(offset, count);
    rRes = (ReadCoilsResponse) m_Transaction.getResponse();
    myBitVector = rRes.getCoils();

    return myBitVector; 
}
Run Code Online (Sandbox Code Playgroud)

在此之后,我用于将该位设置为1或0的是在我的代码中使用来自net.wimpi.modbus.util包中的BitVector类的本机函数:

test.setBit(2, true);
Run Code Online (Sandbox Code Playgroud)

注意:重要的是要记住,每次要读取或写入plc的值时,最好的方法是打开关闭与PLC的连接.

Mar*_*nez 2

我的明确答案是:您必须将寄存器视为位。因此,如果您想在代码中写入以 int 值表示的寄存器的第二位,则必须执行以下操作:

intValue = m_Data[1].getValue();
intValue = intValue | 2;
m_Data[1].setValue(intValue);
Run Code Online (Sandbox Code Playgroud)

第二行修改我想要写入 PLC 的位。