我能够与PLC Build 连接以从中读取数据 . 现在有一个问题,那就是我必须编写一个方法来修改PLC中的数据 . 为此,我必须向PLC发送两个值:int值和布尔值 . 我通过net.wimpi.modbus包中的类解决了int值 . 但是当谈到布尔值时,我不知道该怎么做 .
如果有人遇到和我现在一样的问题,你能不能给我一个参考资料,在那里我可以找到一个解决方案或一个非常好的教程链接来解决我的问题?有人在this question中发布了几个链接,但是它向我发送了没有't have much to do with the communication with the PLC'的教程以及如何处理PLC的数据 .
EDIT
我与Modicon M340 PLC Build 了连接,对于连接,我使用了net.wimpi.modbus包的类 . 我通过类 ModbusTCPTransaction
和 TCPMasterConnection
在我的代码中 Build 了连接,并通过类 ReadMultipleRegistersRequest
和 ReadMultipleRegistersResponse
读取了值 .
我为连接编写的代码:
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;
}
EDIT 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");
}
}
另外,我使用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类的本机函数:
test.setBit(2, true);
注意:重要的是要记住,每次要读取或写入plc的值时,最好的方法是打开关闭与PLC的连接 .
1 回答
我的确定答案是:你必须将寄存器视为位 . 因此,如果要在代码中写入int值中表示的寄存器的第二位,则必须执行以下操作:
第二行修改了我想在PLC中写入的位 .