Java 从Android向PLC发送布尔值
我能够与PLC建立连接,从中读取数据。现在有一个问题,那就是我必须写一个方法来修改来自PLC的数据。为了实现这一点,我必须向PLC发送两个值:一个int值和一个boolean值。我通过net.wimpi.modbus包中的类得到int值。但是当涉及到布尔值时,我不知道该怎么做 如果有人有和我现在一样的问题,你能给我一个参考,我可以在那里找到一个解决方案或一个非常好的教程链接来解决我的问题吗?有人在中发布了几个链接,但它将我发送到与PLC的通信以及如何处理PLC数据无关的教程 编辑 我与Modicon M340 PLC进行了连接,对于连接,我使用net.wimpi.modbus包的类。我通过类Java 从Android向PLC发送布尔值,java,android,boolean,plc,modbus,Java,Android,Boolean,Plc,Modbus,我能够与PLC建立连接,从中读取数据。现在有一个问题,那就是我必须写一个方法来修改来自PLC的数据。为了实现这一点,我必须向PLC发送两个值:一个int值和一个boolean值。我通过net.wimpi.modbus包中的类得到int值。但是当涉及到布尔值时,我不知道该怎么做 如果有人有和我现在一样的问题,你能给我一个参考,我可以在那里找到一个解决方案或一个非常好的教程链接来解决我的问题吗?有人在中发布了几个链接,但它将我发送到与PLC的通信以及如何处理PLC数据无关的教程 编辑 我与Modic
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
我想我实现了我想要的。我使用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类读取位向量变量的方法:
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的连接 我的最终答案是:必须将寄存器视为位。因此,如果要在代码中写入以int值表示的寄存器的第二位,则必须执行以下操作:
intValue = m_Data[1].getValue();
intValue = intValue | 2;
m_Data[1].setValue(intValue);
第二行修改了我想在PLC中写入的位。提示:您可以在问题的标题中留下“问题”。如果你把它贴在这里,这意味着有一些问题需要解决。你能告诉我们,你已经尝试了什么吗。那是什么?你是怎么接通的?这可能会影响处理布尔语的方式。@Fildor我刚刚编辑了我的问题并回答了你问的问题,meI通过找到了答案,我仍在检查如何做,但如果有人有更简单的方法,我将不胜感激!!!!