private static void sendRequest(final short register) {

    final long currentTimeInMillis = System.currentTimeMillis();
    final long elapsed = currentTimeInMillis - lastUpdateInMillis;
    totalTimeMillis += elapsed;
    updatesReceived++;

    clearDataBuffer();

    short length;

    switch (register) {
      case REGISTER_4096_PLUS_SEVEN:
        length = 8;
        break;
      case REGISTER_4140_PLUS_SIX:
        length = 7;
        break;
      default:
        length = 16;
        break;
    }

    if (connected != null & connected.isAlive()) {
      connected.write(ModbusRTU.getRegister(SLAVE_ADDRESS, HOLDING_REGISTER, register, length));

      lastUpdateInMillis = currentTimeInMillis;
      lastRegister = register;
    }
  }
  private static void disconnect() {
    refreshHandler.removeCallbacks(RefreshRunnable);
    if (menuConnect != null) menuConnect.setTitle(R.string.menu_connect);

    try {
      if (connected != null && connected.isAlive()) {
        connected.cancel();
        connected.join();
      }
    } catch (Exception e) {
      // do nothing
    }
  }
  public void onTabSelected(Tab tab, FragmentTransaction ft) {

    ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);

    switch (tab.getPosition()) {
      case 0:
        ft.show(adaptiveFragment);
        break;
      case 1:
        ft.show(gaugesFragment);
        break;
      case 2:
        if (connected != null && connected.isAlive()) {
          progress = ProgressDialog.show(ctx, "Fuel Map", "Reading map values 0/512...");

          mapOffset = 0;
          mapMode = true;
          sendRequest(mapOffset);
        }

        ft.show(fuelFragment);
        break;
    }

    // we don't want to overwrite our pref if we're in onCreate
    if (lvDevices != null) {
      Editor edit = prefs.edit();
      edit.putInt("prefs_last_tab", tab.getPosition());
      edit.commit();
    }
  }
  @Override
  protected void onResume() {
    super.onResume();

    // initialize our preferences
    tempUomPref = Integer.parseInt(prefs.getString("prefs_uom_temp", "1"));
    afrNotEqualTargetPref = prefs.getBoolean("prefs_afrnottarget_pref", false);
    afrNotEqualTargetTolerance = prefs.getFloat("prefs_afrnottarget_tolerance_pref", 5f);
    waterTempPref = prefs.getBoolean("prefs_watertemp_pref", false);
    remoteName = prefs.getString("prefs_remote_name", "");
    remoteMacAddr = prefs.getString("prefs_remote_mac", "");
    autoConnect = prefs.getBoolean("prefs_auto_connect", false);

    // need to set gauge scale based on uom selected
    switch (tempUomPref) {
      case 0:
        minimumWaterTemp =
            prefs.getFloat("prefs_min_water_temp", AdaptivePreferences.MIN_WATER_TEMP_CELCIUS);
        maximumWaterTemp =
            prefs.getFloat("prefs_max_water_temp", AdaptivePreferences.MAX_WATER_TEMP_CELCIUS);

        iatNeedle.setPivotPoint(.5f);
        iatNeedle.setMinValue(0);
        iatNeedle.setMaxValue(100);
        iatNeedle.setMinDegrees(-180);
        iatNeedle.setMaxDegrees(90);

        imgIat.setImageResource(R.drawable.iatgauge_celcius);
        break;

      case 1:
        minimumWaterTemp =
            prefs.getFloat("prefs_min_water_temp", AdaptivePreferences.MIN_WATER_TEMP_FAHRENHEIT);
        maximumWaterTemp =
            prefs.getFloat("prefs_max_water_temp", AdaptivePreferences.MAX_WATER_TEMP_FAHRENHEIT);

        iatNeedle.setPivotPoint(.5f);
        iatNeedle.setMinValue(0);
        iatNeedle.setMaxValue(200);
        iatNeedle.setMinDegrees(-180);
        iatNeedle.setMaxDegrees(90);

        imgIat.setImageResource(R.drawable.iatgauge);
        break;
    }

    afrAlarmLogging = prefs.getBoolean("prefs_afr_alarm_logging", false);
    if (menuShareLog != null) {
      menuShareLog.setVisible(afrAlarmLogging && !afrAlarmLogItems.getItems().isEmpty());
    }

    // ensure both fragments are hidden
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.hide(adaptiveFragment);
    ft.hide(gaugesFragment);
    ft.hide(fuelFragment);
    ft.commit();

    ActionBar bar = getActionBar();
    bar.selectTab(bar.getTabAt(prefs.getInt("prefs_last_tab", 1)));

    // EVAN
    String action = getIntent().getAction();

    if (Intent.ACTION_MAIN.equals(action) || UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
      if (DEBUG_MODE) Log.d(TAG, "USB Device Attached");

      connected = UsbConnectedThread.checkConnectedUsbDevice(this, connectionHandler);

      if (connected != null) {
        lastUpdateInMillis = System.currentTimeMillis();
      }
    }
    // END EVAN

    if (connected != null && connected.isAlive()) {
      refreshHandler.postDelayed(RefreshRunnable, LONG_PAUSE);
    } else {
      if (autoConnect && remoteMacAddr.length() > 0) {
        disconnect();
        new Handler()
            .postDelayed(
                (new Runnable() {
                  public void run() {
                    connect(remoteName, remoteMacAddr);
                  }
                }),
                1000);
        //        		connect(remoteName,  remoteMacAddr);
      }
    }
  }