我正在使用终端仿真器库来创建终端,然后使用它将通过串行输入的数据发送到串行设备。并不是说我有这样的工作,我想在屏幕上创建一些其他图形界面,也许只是一个文本框,它在终端中显示用户结果的简要版本以及终端中的完整版本。他们只需看一眼就可以更快地查看结果。我想知道如何最好地解析它。当我发送一个命令时,一个字节数组被发回给我,其中包含这样的数据作为一组结果的示例:
Interface IP-Address Status Protocol
vlan1 192.168.1.1 up up
Fa0/1i unassigned administratively down down
Fa0/1o unassigned down down
Fa1/0 unassigned down down
Fa1/1 unassigned down down
Fa1/2 unassigned down down
Fa1/3 unassigned down down
Fa1/4 unassigned down down
Fa1/5 unassigned down down
Fa1/6 unassigned down down
Fa1/7 unassigned down down
Fa1/8 unassigned up up
Fa1/9 unassigned down down
Fa1/10 unassigned up up
Fa1/11 unassigned down down
Gi0 unassigned up up
switch#
我将如何解析这个?我想要类似“接口 vlan1 的 IP 为 192.1.1.168”或“接口 Fa1/8 的 IP 地址未分配”之类的内容。为用户显示,只列出最相关的数据。我会说类似的话:
if (dataReceived.charAt(i) == 'v'
&& dataReceived.charAt(i + 1) == 'l'
&& dataReceived.charAt(i + 2) == 'a'
&& dataReceived.charAt(i + 3) == 'n')
&& dataReceived.charAt(i + 4) == '1')
&& etc //check i is in bounds, check is the next non-whitespace character a number or a u somehow and so on
{
//do things here
//print out "The interface vlan1 is up with an IP of 192.1.1.168"
}
这种方式看起来很乏味,检查每个字节,查看下一个字节是什么等等。然而,这是最好的/正确的方法吗?
编辑:这是我目前拥有的一个样本,我在哪里接收数据,以及我在哪里测试某些数据:
public void onDataReceived(int id, byte[] data) {
dataReceived = new String(data);
try {
dataReceivedByte = dataReceived.getBytes("ASCII");
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
statusBool = true;
Log.d(TAG, "in data received " + dataReceived);
((MyBAIsWrapper) bis).renew(data);
runOnUiThread(new Runnable(){
@Override
public void run() {
}});
viewHandler.post(updateView);
}
Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
@Override
public void run() {
mEmulatorView.invalidate();
if (statusBool == true) {
for (int i = 1; i < dataReceived.length() - 1; i++) {
if (dataReceived.charAt(i) == '>') {
Log.d(TAG, "found >");
deviceStatus = 0;
}
if (dataReceived.charAt(i) == '#'
&& dataReceived.charAt(i - 1) != ')') {
Log.d(TAG, "found #");
deviceStatus = 1;
}
if ((i + 1) <= (dataReceived.length())
&& dataReceived.charAt(i) == ')'
&& dataReceived.charAt(i + 1) == '#') {
Log.d(TAG, "found config )#");
deviceStatus = 2;
}
}
statusBool = false;
viewHandler.postDelayed(updateView, 1000);
}
}
};