Modbus協(xié)議在工業(yè)現(xiàn)場使用比較頻繁,為方便手機調(diào)試,在android上做了個ModbusTcp主站功能。
1:使用說明
作為主站,需要連接從站,因此需要配置從站IP、端口以及協(xié)議中從站地址。
連接成功后這些配置就不能修改。
偏移是需要讀取或者寫入的起始地址。
長度讀取或者寫入的總長度。
后邊的長度按鍵是,為寫入準(zhǔn)備輸入數(shù)據(jù)區(qū)數(shù)據(jù)。一般步驟先設(shè)定需要寫入的長度,當(dāng)然根據(jù)實際情況可以同時設(shè)置偏移量;在點擊長度,就會在寫入?yún)^(qū)生成需要寫入長度項目,然后設(shè)定需要寫入的值。
讀取按鍵,按設(shè)定的地址和長度以及功能讀取器從站數(shù)據(jù),讀回來的數(shù)據(jù),如果是是3、4功能號的,選擇了十六進制,就按十六進制展示;否則按十進制展示。
寫入按鍵,從偏移量作為基地址寫入從站。
清理按鍵是清理寫入?yún)^(qū)內(nèi)輸入的內(nèi)容,清理不會寫入到從站。
“----->”傳遞按鍵,在有些情況下,可以直接把讀取的實際傳遞到寫入?yún)^(qū),然后修改部分?jǐn)?shù)據(jù)后,直接寫入到從站。這樣就不需要逐條輸入,方便操作。
2:程序說明
頁面上在讀取展示區(qū)和寫入輸入?yún)^(qū)是兩個ListView組件。讀區(qū)的item是兩個TextView,寫入輸入?yún)^(qū)是一個TextView和一個EditText。
ModbusTcp功能直接使用的是Modbus4Android的jar包。核心功能代碼如下
private void modbusInit(String ip, int port) {
ModbusReq.getInstance().setParam(new ModbusParam()
.setHost(ip)
.setPort(port)
.setEncapsulated(false)
.setKeepAlive(true)
.setTimeout(2000)
.setRetries(0))
.init(new OnRequestBack() {
@Override
public void onSuccess(String s) {
Log.d(TAG, "onSuccess " + s);
Intent i = new Intent("notifyText");
i.putExtra("act", "100");
i.putExtra("status", true);
i.putExtra("Message", "連接成功");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
@Override
public void onFailed(String msg) {
Log.d(TAG, "onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "0");
i.putExtra("status", false);
i.putExtra("Message", "連接失敗");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
});
}
public void readCoil(int slaveid, int start, int len) {
ModbusReq.getInstance().readCoil(new OnRequestBack() {
@Override
public void onSuccess(boolean[] booleen) {
Log.d(TAG, "readCoil onSuccess " + Arrays.toString(booleen));
Intent i = new Intent("notifyText");
i.putExtra("act", "1");
i.putExtra("status", true);
i.putExtra("Message", Arrays.toString(booleen));
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
@Override
public void onFailed(String msg) {
Log.e(TAG, "readCoil onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "1");
i.putExtra("status", false);
i.putExtra("Message", "讀取線圈失敗");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}, slaveid, start, len);
}
public void readDiscreteInput(int slaveid, int start, int len) {
ModbusReq.getInstance().readDiscreteInput(new OnRequestBack() {
@Override
public void onSuccess(boolean[] booleen) {
Log.d(TAG, "readDiscreteInput onSuccess " + Arrays.toString(booleen));
Intent i = new Intent("notifyText");
i.putExtra("act", "2");
i.putExtra("status", true);
i.putExtra("Message", Arrays.toString(booleen));
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
@Override
public void onFailed(String msg) {
Log.e(TAG, "readDiscreteInput onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "2");
i.putExtra("status", false);
i.putExtra("Message", "讀取線圈失敗");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}, slaveid, start, len);
}
public void readHoldingRegisters(int slaveid, int start, int len) {
//readHoldingRegisters
ModbusReq.getInstance().readHoldingRegisters(new OnRequestBack() {
@Override
public void onSuccess(short[] data) {
Log.d(TAG, "readHoldingRegisters onSuccess " + Arrays.toString(data));
Intent i = new Intent("notifyText");
i.putExtra("act", "3");
i.putExtra("status", true);
i.putExtra("Message", Arrays.toString(data));
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
@Override
public void onFailed(String msg) {
Log.e(TAG, "readHoldingRegisters onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "3");
i.putExtra("status", false);
i.putExtra("Message", "讀取線圈失敗");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}, slaveid, start, len);
}
public void readInputRegisters(int slaveid, int start, int len) {
ModbusReq.getInstance().readInputRegisters(new OnRequestBack() {
@Override
public void onSuccess(short[] data) {
Log.d(TAG, "readInputRegisters onSuccess " + Arrays.toString(data));
Intent i = new Intent("notifyText");
i.putExtra("act", "4");
i.putExtra("status", true);
i.putExtra("Message", Arrays.toString(data));
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
@Override
public void onFailed(String msg) {
Log.e(TAG, "readInputRegisters onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "4");
i.putExtra("status", false);
i.putExtra("Message", "讀取線圈失敗");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}, slaveid, start, len);
}
public void writeCoil(int slaveid, int start, boolean value) {
ModbusReq.getInstance().writeCoil(new OnRequestBack() {
@Override
public void onSuccess(String s) {
Log.e(TAG, "writeCoil onSuccess " + s);
}
@Override
public void onFailed(String msg) {
Log.e(TAG, "writeCoil onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "5");
i.putExtra("status", false);
i.putExtra("Message", "msg");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}, slaveid, start, value);
}
public void writeRegister(int slaveid, int start, int value) {
ModbusReq.getInstance().writeRegister(new OnRequestBack() {
@Override
public void onSuccess(String s) {
Log.e(TAG, "writeRegister onSuccess " + s);
}
@Override
public void onFailed(String msg) {
Log.e(TAG, "writeRegister onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "6");
i.putExtra("status", false);
i.putExtra("Message", "msg");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}, slaveid, start, value);
}
public void writeRegisters(int slaveid, int start, short[] value) {
ModbusReq.getInstance().writeRegisters(new OnRequestBack() {
@Override
public void onSuccess(String s) {
Log.e(TAG, "writeRegisters onSuccess " + s);
}
@Override
public void onFailed(String msg) {
Log.e(TAG, "writeRegisters onFailed " + msg);
Intent i = new Intent("notifyText");
i.putExtra("act", "16");
i.putExtra("status", false);
i.putExtra("Message", "msg");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}, slaveid, start, value);
}
讀函數(shù)把成功和失敗都用廣播播放出去。
寫只把失敗廣播出去。
3:測試效果
程序功能基本滿足現(xiàn)場測試需要,方便隨時聯(lián)調(diào)。在測試過程中出現(xiàn)程序閃退,經(jīng)跟蹤,閃退異常原因“java.lang.IndexOutOfBoundsException: Index: 99, Size: 10”,是ListView的adapter在更新數(shù)據(jù)時,老數(shù)據(jù)操作未完成引起。經(jīng)修改程序后,未出現(xiàn)閃退情況。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧