简体   繁体   English

从Android发送布尔值到PLC

[英]Sending a boolean value to a PLC from Android

I was able to make a connection with a PLC to read data from it. 我能够与PLC建立连接以从中读取数据。 Now there's one problem and it is that I have to write a method to modify the data from the PLC. 现在有一个问题,那就是我必须编写一种方法来修改PLC中的数据。 To achieve this, I have to send two values to the PLC: an int value and a boolean value. 为此,我必须向PLC发送两个值:一个int值和一个布尔值。 I got the int value solved via the classes from the net.wimpi.modbus package. 我通过net.wimpi.modbus包中的类解决了int值的问题。 But when it comes to the boolean value I have no clue what to do. 但是当涉及到布尔值时,我不知道该怎么做。

If someone had the same problem as I do now, could you please send me a reference where I can find a solution or a link of a really good tutorial to solve my problem? 如果有人遇到与我现在同样的问题,能否请您发送给我参考,以找到解决方案或一个很好的教程链接来解决我的问题? Someone posted a couple of links in this question but it sends me to tutorials that doesn't have much to do with the communication with the PLC's and how to treat the data of the PLC. 有人在这个问题上发布了几个链接,但它使我进入了与PLC的通信以及如何处理PLC数据没有太大关系的教程。

EDIT 编辑

I made the connection with a Modicon M340 PLC, and for the connection I use the classes of the net.wimpi.modbus package. 我使用Modicon M340 PLC建立了连接,对于连接,我使用net.wimpi.modbus软件包的类。 I made the connection in my code through the classes ModbusTCPTransaction and TCPMasterConnection , and I read the values through the classes ReadMultipleRegistersRequest and ReadMultipleRegistersResponse . 我发过班,我的代码连接ModbusTCPTransactionTCPMasterConnection ,和我在班念值ReadMultipleRegistersRequestReadMultipleRegistersResponse

The code I made for the connection: 我为连接编写的代码:

    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);
}

And to read the values, I call the next code all the time. 为了读取值,我一直都在调用下一个代码。 I accomplished only reading and writing int, String and float values through words that I read from an offset declared on the PLC: 我只能通过从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;    
}

EDIT 2 编辑2

I think I accomplished what I wanted. 我想我完成了我想要的。 There are 4 classes that I use to read the coils: 我使用4个类来读取线圈:

ReadCoilsRequest ReadCoilsResponse WriteMultipleCoilsRequest WriteMultileCoilsResponse ReadCoilsRequest ReadCoilsResponse WriteMultipleCoilsRequest WriteMultileCoilsResponse

What I did is two methods to read and write coils into the PLC: 我所做的是两种将线圈读取和写入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");
    }
}

Also, I made a method to read BitVector variables using the Coils classes: 另外,我制作了一种使用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; 
}

After this, what I used to set the bit to 1 or 0 is using a native function from the BitVector class from the net.wimpi.modbus.util package in my code: 在此之后,我用来将位设置为1或0的方法是使用代码中net.wimpi.modbus.util包中BitVector类的本机函数:

test.setBit(2, true);

NOTE: It is important to remember that everytime that you want to read or write values to the plc, the best way to do it is opening a closing the connection to the PLC. 注意:重要的是要记住,每次要向PLC读取或写入值时,最好的方法是打开与PLC的连接。

My definitive answer is: you have to treat the registers as bits. 我的明确答案是:您必须将寄存器视为位。 So if you want to write the second bit of a register represented in an int value in your code, you will have to do something like this: 因此,如果要在代码中写入以int值表示的寄存器的第二位,则必须执行以下操作:

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

The second line modifies the bit I want to write in my PLC. 第二行修改了我要在PLC中写入的位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM