我能夠與PLC建立連接以從中讀取數據。現在有一個問題,那就是我必須編寫一個方法來修改來自PLC的數據。爲了達到這個目的,我必須向PLC發送兩個值:一個int值和一個布爾值。我通過net.wimpi.modbus包中的類獲得了int值。但是當涉及到布爾值時,我不知道該怎麼做。從Android發送布爾值到PLC
如果有人遇到和我一樣的問題,請給我一個參考資料,我可以找到一個解決方案或一個非常好的教程鏈接來解決我的問題?有人在this question上發佈了一些鏈接,但它給了我一些教程,這些教程與PLC的通信以及如何處理PLC的數據無關。
EDIT
予製成帶有的Modicon M340 PLC的連接,以及用於連接I使用net.wimpi.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讀單詞浮動值:
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包在我的代碼位向量類:
test.setBit(2, true);
注意:重要的是要記住,每當你想要讀取或寫入PLC的值時,最好的方法是打開一個關閉與PLC的連接。
提示:你可以在你的問題標題上留下「問題與」。如果你在這裏發佈,這意味着有一些需要解決的問題。 –
你能告訴我們,你已經嘗試過了嗎?它是什麼PLC?你是如何建立聯繫的?這可能會影響處理布爾的方式。 – Fildor
@Fildor我剛剛編輯了我的問題並回答了你問我的問題 –