Android OBD Reader not going through all switch cases -
i working on android app modifies bluetooth chat example given eclipse create on-board diagnostic (obd) reader. code sends message vehicle's ecu , retrieves data use of switches.
now, problem is, reason when run program receive data 6 of cases , not receiving data rest of them.
the code kind of lengthy.
public void starttransmission() { sendmessage("01 00" + '\r'); } public void getdata(int messagenumber) { final textview tx = (textview) findviewbyid(r.id.txview2); switch (messagenumber) { case 1: sendmessage("01 0c" + '\r'); // rpm tx.settext("01 0c"); messagenumber++; break; case 2: sendmessage("01 0d" + '\r'); // mph tx.settext("01 0d"); messagenumber++; break; case 3: sendmessage("01 04" + '\r'); // engine load tx.settext("01 04"); messagenumber++; break; case 4: sendmessage("01 05" + '\r'); // coolant temperature tx.settext("01 05"); messagenumber++; break; case 5: sendmessage("01 0f" + '\r'); // intake temperature tx.settext("01 0f"); messagenumber++; break; case 6: sendmessage("at rv" + '\r'); // voltage tx.settext("at rv"); messagenumber++; break; case 7: sendmessage("01 06" + '\r'); // fuel trim bank 1 sensor 1 tx.settext("01 06"); messagenumber++; case 8: sendmessage("01 07" + '\r'); // fuel trim bank 1 sensor 2 tx.settext("01 07"); messagenumber++; case 9: sendmessage("01 0b" + '\r'); // intake manifold pressure tx.settext("01 0b"); messagenumber++; case 10: sendmessage("01 0e" + '\r'); // timing advance tx.settext("01 0e"); messagenumber++; case 11: sendmessage("01 11" + '\r'); // throttle position tx.settext("01 11"); messagenumber++; case 12: sendmessage("01 14" + '\r'); // oxygen bank 1 sensor 1 tx.settext("01 14"); messagenumber++; case 13: sendmessage("01 15" + '\r'); // oxygen bank 1 sensor 2 tx.settext("01 15"); messagenumber++; default: ; } } private void sendmessage(string message) { // check we're connected before trying if (mchatservice.getstate() != bluetoothchatservice.state_connected) { toast.maketext(this, r.string.not_connected, toast.length_short) .show(); return; }// end if // check there's send if (message.length() > 0) { // message bytes , tell bluetoothchatservice write byte[] send = message.getbytes(); mchatservice.write(send); // reset out string buffer 0 , clear edit text field moutstringbuffer.setlength(0); // moutedittext.settext(moutstringbuffer); }// end if }// end sendmessage method // handler gets information bluetoothchatservice private final handler mhandler = new handler() { @override public void handlemessage(message msg) { // <--------- initialize data display fields ---------->// /* pid = 0c */final textview rpm = (textview) findviewbyid(r.id.numrpm); /* pid = 0d */final textview mph = (textview) findviewbyid(r.id.numspeed); /* pid = 04 */final textview engineload = (textview) findviewbyid(r.id.numload); /* pid = 05 */final textview coolanttemperature = (textview) findviewbyid(r.id.numcoolant); /* pid = 0f */final textview intaketemperature = (textview) findviewbyid(r.id.numintakeair); /* pid = rv */final textview voltage = (textview) findviewbyid(r.id.numvoltage); /* pid = 06 */final textview fueltrimb1s1 = (textview) findviewbyid(r.id.numftb1s1); /* pid = 07 */final textview fueltrimb1s2 = (textview) findviewbyid(r.id.numftb1s2); /* pid = 0a */final textview intakemanifold = (textview) findviewbyid(r.id.nummanifold); /* pid = 0e */final textview timingadvance = (textview) findviewbyid(r.id.numtiming); /* pid = 11 */final textview throttlepos = (textview) findviewbyid(r.id.numthrottle); /* pid = 14 */final textview oxygenb1s1 = (textview) findviewbyid(r.id.numo2b1s1); /* pid = 15 */final textview oxygenb1s2 = (textview) findviewbyid(r.id.numo2b1s2); /* displays recieved string */final textview rx = (textview) findviewbyid(r.id.rxview2); string datarecieved; int value = 0; int value2 = 0; int pid = 0; 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 string buffer // string writemessage = new string(writebuf); // mconversationarrayadapter.add("me: " + writemessage); break; case message_read: byte[] readbuf = (byte[]) msg.obj; // construct string valid bytes in buffer string readmessage = new string(readbuf, 0, msg.arg1); // ------- added code 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(" "); if ((bytes[0] != null) && (bytes[1] != null)) { pid = integer.parseint(bytes[0].trim(), 16); value = integer.parseint(bytes[1].trim(), 16); } switch (pid) { case 15:// pid(0f): intake temperature value = value - 40; // formula intake temperature value = ((value * 9) / 5) + 32; // convert celsius // farenheit string displayintaketemp = string.valueof(value); intaketemperature.settext(displayintaketemp + " f"); break; // end intake temperature case case 4:// pid(04): engine load value = (value * 100) / 255; string displayengineload = string.valueof(value); engineload.settext(displayengineload + " %"); break; // end engine load case case 5:// pid(05): coolant temperature value = value - 40; value = ((value * 9) / 5) + 32; // convert deg f string displaycoolanttemp = string.valueof(value); coolanttemperature.settext(displaycoolanttemp); break; // end coolant temperature case case 12: // pid(0c): rpm int rpm_value = (value * 256) / 4; string displayrpm = string.valueof(rpm_value); rpm.settext(displayrpm); break; // end rpm case case 13:// pid(0d): mph value = (value * 5) / 8; // convert kph mph string displaymph = string.valueof(value); mph.settext(displaymph + " mph"); break; // end mph case case 6:// pid(06): fuel trim bank 1 sensor 1 value = (value - 128) * 100 / 128; string displayftb1s1 = string.valueof(value); fueltrimb1s1.settext(displayftb1s1); break; case 7:// pid(07): fuel trim bank 1 sensor 2 value = (value - 128) * 100 / 128; string displayftb1s2 = string.valueof(value); fueltrimb1s2.settext(displayftb1s2); break; case 11: // pid(0a): intake manifold pressure string displayintakemanifold = string.valueof(value); intakemanifold.settext(displayintakemanifold); break; case 14: // pid(0e): timing advance value = value / 2 - 64; string displaytimingadvance = string.valueof(value); timingadvance.settext(displaytimingadvance); break; case 17: // pid(11): throttle position value = value * 100 / 255; string displaythrottlepos = string.valueof(value); throttlepos.settext(displaythrottlepos); case 20: // pid(14): oxygen bank 1 sensor 1 string displayo2b1s1 = string.valueof(value); oxygenb1s1.settext(displayo2b1s1); break; case 21: // pid(14): oxygen bank 1 sensor 2 string displayo2b1s2 = string.valueof(value); oxygenb1s2.settext(displayo2b1s2); break; default: ; } } else if ((datarecieved != null) && (datarecieved .matches("\\s*[0-9a-fa-f]{1,2} [0-9a-fa-f]{2} [0-9a-fa-f]{2}\\s*\r?\n?"))) { datarecieved = datarecieved.trim(); string[] bytes = datarecieved.split(" "); if ((bytes[0] != null) && (bytes[1] != null) && (bytes[2] != null)) { pid = integer.parseint(bytes[0].trim(), 16); value = integer.parseint(bytes[1].trim(), 16); value2 = integer.parseint(bytes[2].trim(), 16); } // pid(0c): rpm if (pid == 12) { int rpm_value = ((value * 256) + value2) / 4; string displayrpm = string.valueof(rpm_value); rpm.settext(displayrpm + " rpm"); } else if ((pid == 1) || (pid == 65)) { switch (value) { case 15:// pid(0f): intake temperature value2 = value2 - 40; // formula intake air temp value2 = ((value2 * 9) / 5) + 32; // convert deg // f string displayintaketemp = string.valueof(value2); intaketemperature .settext(displayintaketemp + " °f"); break; // end intake temperature case case 4:// pid(04): engine load value2 = (value2 * 100) / 255; string displayengineload = string.valueof(value2); engineload.settext(displayengineload + " %"); break; // end engine load case case 5:// pid(05): coolant temperature value2 = value2 - 40; value2 = ((value2 * 9) / 5) + 32; // convert deg // f string displaycoolanttemp = string.valueof(value2); coolanttemperature.settext(displaycoolanttemp + " °f"); break; case 13:// pid(0d): mph value2 = (value2 * 5) / 8; // convert mph string displaymph = string.valueof(value2); mph.settext(displaymph + " mph"); break; // case 6:// pid(06): fuel trim bank 1 sensor 1 // // value = (value - 128) * 100 / 128; // // string displayftb1s1 = string.valueof(value); // fueltrimb1s1.settext(displayftb1s1 + " v"); // break; // // case 7:// pid(07): fuel trim bank 1 sensor 2 // // value = (value - 128) * 100 / 128; // // string displayftb1s2 = string.valueof(value); // fueltrimb1s2.settext(displayftb1s2 + " v"); // break; // // case 11: // pid(0a): intake manifold pressure // // string displayintakemanifold = string // .valueof(value); // intakemanifold.settext(displayintakemanifold // + " psi"); // break; // // case 14: // pid(0e): timing advance // // value = value / 2 - 64; // // string displaytimingadvance = string.valueof(value); // timingadvance.settext(displaytimingadvance + " °"); // break; // // case 17: // pid(11): throttle position // // value = value * 100 / 255; // // string displaythrottlepos = string.valueof(value); // throttlepos.settext(displaythrottlepos + " %"); // // case 20: // pid(14): oxygen bank 1 sensor 1 // // string displayo2b1s1 = string.valueof(value); // oxygenb1s1.settext(displayo2b1s1 + " v"); // break; // // case 21: // pid(14): oxygen bank 1 sensor 2 // // string displayo2b1s2 = string.valueof(value); // oxygenb1s2.settext(displayo2b1s2 + " v"); // break; default: ; } } } else if ((datarecieved != null) && (datarecieved .matches("\\s*[0-9]+(\\.[0-9]?)?v\\s*\r*\n*"))) { datarecieved = datarecieved.trim(); voltage.settext(datarecieved); } else if ((datarecieved != null) && (datarecieved .matches("\\s*[0-9]+(\\.[0-9]?)?v\\s*v\\s*>\\s*\r*\n*"))) { datarecieved = datarecieved.trim(); // string volt_number = datarecieved.substring(0, // datarecieved.length() - 1); voltage.settext(datarecieved); } else if ((datarecieved != null) && (datarecieved .matches("\\s*[ .a-za-z0-9\\?*>\r\n]*\\s*>\\s*\r*\n*"))) { if (message_number == 7) message_number = 1; getdata(message_number++); } break; } }
where think want focus on is:
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(" "); if ((bytes[0] != null) && (bytes[1] != null)) { pid = integer.parseint(bytes[0].trim(), 16); value = integer.parseint(bytes[1].trim(), 16); }
this code based off research have done , not sure string behind datarecieved.matches is. looks array , thinking parameters received causing switch cases not fire.
thanks input.
Comments
Post a Comment