java从Android向PLC发送布尔值
我能够与PLC建立连接,从中读取数据。现在有一个问题,那就是我必须写一个方法来修改来自PLC的数据。为了实现这一点,我必须向PLC发送两个值:一个int值和一个boolean值。我通过网络上的类得到了int值。威皮。modbus软件包。但说到布尔值,我不知道该怎么办
如果有人有和我现在一样的问题,你能给我发一个参考资料,我可以在那里找到解决问题的方法,或者一个非常好的教程的链接来解决我的问题吗?有人在this question上发了几个链接,但它把我送到了与PLC的通信以及如何处理PLC数据无关的教程
编辑
我与Modicon M340 PLC建立了连接,对于连接,我使用网络的类。威皮。modbus软件包。我在代码中通过类ModbusTCPTransaction
和TCPMasterConnection
建立连接,并通过类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;
}
编辑2
我想我实现了我想要的。我用四节课来阅读线圈:
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类读取位向量变量的方法:
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的是使用网络中BitVector类的本机函数。威皮。modbus。我的代码中的util包:
test.setBit(2, true);
注意:重要的是要记住,每次您想要读取或写入plc的值时,最好的方法是打开或关闭与plc的连接
# 1 楼答案
我的最终答案是:你必须把寄存器当作比特。因此,如果要在代码中写入以int值表示的寄存器的第二位,必须执行以下操作:
第二行修改我想在PLC中写入的位