java - 从 Android 向 PLC 发送一个 boolean 值

标签 java android boolean plc modbus

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

如果有人和我现在遇到同样的问题,能否请您给我一个引用,我可以在哪里找到解决方案或一个非常好的教程的链接来解决我的问题?有人在 this question 中发布了几个链接但它把我带到了与 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);
}

为了读取值,我一直调用下一个代码。我只完成了通过从 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;    
}

编辑 2

我想我完成了我想要的。我使用 4 个类来读取线圈:

读取线圈请求 读取线圈响应 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");
    }
}

另外,我创建了一个使用 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; 
}

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

test.setBit(2, true);

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

最佳答案

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

intValue = m_Data[1].getValue();
intValue = intValue | 2;
m_Data[1].setValue(intValue);

第二行修改了我想写到我的PLC中的位。

关于java - 从 Android 向 PLC 发送一个 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560429/

相关文章:

java - Vaadin - 尽管我使用了 JavaScript 注释,但我的 JavaScript 库未加载

android - 如何在 Android Os 4.0 中构建默认浏览器应用程序

c# - 是否有进行冗长 boolean 值评估的原因?

java - 如何在mysql数据库中插入一次数据,然后不断更新? Java/安卓

java - 如何解决 java.lang.ClassNotFoundException : org. aspectj.lang.ProceedingJoinPoint 异常?

java - Tomcat Servlet 路径

java - 2020 年 11 月 1 日/ 'Account Hold' : is it mandatory to display an explanatory message to 'Account Hold' users?

java - ListAdapter 在滚动时显示已显示的项目

安卓日期选择器 : How to set value from DatePicker in EditText in this format dd-mm-yyyy

javascript - 在 React.js 中使用 handleClick 更改 boolean 值