我正在使用OBD-ii Reader。
我使用基本的蓝牙聊天与OBD-ii设备进行通信,使用正则表达式捕获回复并在文本视图中显示它们。
这是我发送PID代码以请求数据的地方:
public void getData(int messagenumber) {
final TextView TX = (TextView) findViewById(R.id.TXView2);
switch(messagenumber) {
case 1:
sendMessage("01 2F" + '\r'); //get Fuel %
TX.setText("01 2F");
Log.d("Case1: ", String.valueOf(messagenumber));
messagenumber++;
Log.d("Case1: ", String.valueOf(messagenumber));
break;
case 2:
sendMessage("01 31" + '\r'); //get Mileage
TX.setText("01 31");
Log.d("Case2: ", String.valueOf(messagenumber));
messagenumber++;
Log.d("Case2: ", String.valueOf(messagenumber));
break;
case 3:
sendMessage("01 0C" + '\r'); //get RPM
TX.setText("01 0C");
Log.d("Case3: ", String.valueOf(messagenumber));
messagenumber++;
Log.d("Case3: ", String.valueOf(messagenumber));
break;
default: ;
}
}
然后在这里获取数据:
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
Log.d("BYTES: ", readMessage);
// ------- ADDED CODE FOR OBD -------- //
dataRecieved = readMessage;
RX.setText(dataRecieved);
if((dataRecieved != null) && (dataRecieved.matches("\\s*[0-9A-Fa-f]{2} [0-9A-Fa-f]{2}\\s*\r?\n?" ))) {
dataRecieved = dataRecieved.trim();
String[] bytes = dataRecieved.split(" ");
Log.d("DR 1val: ",dataRecieved + "--!");
if((bytes[0] != null)&&(bytes[1] != null)) {
PID = Integer.parseInt(bytes[0].trim(), 16);
value = Integer.parseInt(bytes[1].trim(), 16);
}
if (PID == 47) {
/*case 47: //PID(2F): Fuel % */
int fuelValue = ((value*100)/255);
String displayFuel = String.valueOf("PID: " + PID + "Val: " + value + "fuel: " + fuelValue + " %");
Fuel.setText(displayFuel);
/*break;
default: ;*/
}
}
else if((dataRecieved != null) && (dataRecieved.matches("\\s*[0-9A-Fa-f]{2} [0-9A-Fa-f]{2} [0-9A-Fa-f]{2}\\s*\r?\n?" ))) {
dataRecieved = dataRecieved.trim();
String[] bytes = dataRecieved.split(" ");
Log.d("DR 2vals: ",dataRecieved + "--!");
if((bytes[0] != null)&&(bytes[1] != null)&&(bytes[2] != null)) {
PID = Integer.parseInt(bytes[0].trim(), 16);
//if (PID == 12){
value1 = Integer.parseInt(bytes[1].trim(), 16);
value2 = Integer.parseInt(bytes[2].trim(), 16);
/*}
else if (PID == 49){
//mileVal = Integer.parseInt(bytes[1].trim(), 16);
}*/
}
//PID(0C): RPM
if (PID == 12) {
int RPM_value = ((value1*256)+value2)/4;
String displayRPM = String.valueOf("PID: " + PID + "A: " + value1 + " B: " + value2 + "RPM: " + RPM_value);
Throttle.setText(displayRPM);
}
else if (PID == 49) {
//PID(31): Distance Travelled (A*256)+B
int miles = (int) (((value1*256)+value2)*0.62137);
//String displayDistance = String.valueOf(miles + " miles");
String displayDistance = String.valueOf("PID: " + PID + "A: " + value1 + " B: " + value2 + "Miles: " + miles);
Distance.setText(displayDistance);
}
}
然后我使用这个正则表达式来听取>这意味着OBD已完成处理命令,然后应该返回到开始并再次请求PID代码。
else if((dataRecieved != null) && (dataRecieved.matches("\\s*[ .A-Za-z0-9\\?*>\r\n]*\\s*>\\s*\r*\n*" ))) {
if(message_number == 4){
message_number = 1;
}
getData(message_number++);
}
当我运行应用程序时,需要很长时间才能开始给我任何值,但它们是准确的。我有从OBD收到的数据的日志。然而,我的日志中出现了一些我不明白的东西。我还注意到显示dataRecieved的textView会显示未知字符并显示STOPPED,然后冻结并在一段时间后再次启动。 这是一些日志...
D/TextLayoutCache(5146): Cache value 0x520da3b8 deleted, size = 136
V/BluetoothSocket.cpp(5146): readNative
D/TextLayoutCache(5146): Cache value 0x4dbd5e00 deleted, size = 176
V/BluetoothSocket.cpp(5146): readNative
V/BluetoothSocket.cpp(5146): readNative
D/TextLayoutCache(5146): Cache value 0x4dfead50 deleted, size = 240
D/TextLayoutCache(5146): Cache value 0x520dcb10 deleted, size = 240
V/BluetoothSocket.cpp(5146): readNative
D/BYTES:(5146): C
D/BYTES:(5146): 4
D/BYTES:(5146): 4
D/BYTES:(5146): 4
答案 0 :(得分:0)
这些网站将帮助您开始使用正则表达式:
http://www.rexegg.com/regex-quickstart.html
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
当试图理解任何代码时,将其分解为更小的步骤:
dataRecieved = readMessage;
这只是将dataRecieved设置为等于readMessage
\ s代表白色空间,是一个“角色类”
0-9是范围,A-F也是a-f。请注意,它们使它与大小写匹配。
当看到这个\ s * [.A-Za-z0-9 \注意到之间有什么\ 正则表达式通常包含您搜索条件和范围的区域。
if((dataRecieved != null) && (dataRecieved.matches("\\s*[0-9A-Fa-f]{2} [0-9A-Fa-f]{2}\\s*\r?\n?" ))) {
此行询问有条件的'if'并且有&&因此,对于整个条件,两者都必须为true,否则返回false。
还有更多但在我继续之前:
我建议此时编辑您的问题并澄清您的理解,然后询问有关代码特定部分的问题。我没有在你的问题中听到你对这个代码的哪些部分感到困惑,所以在没有解释整个问题的情况下很难回答你的问题。
我希望这个答案可以帮助你开始