-1

My application gets serial USB input with FTDI chip, I managed to do it. As I press a button I get 3 input float type numbers displayed on my app. Here is the onClick:

public void onReadClick(View view) throws UnsupportedEncodingException {

          // [FTDriver] Create Read Buffer
            byte[] rbuf = new byte[27];   // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte

           // [FTDriver] Read from USB Serial
            len = mSerial.read(rbuf);

            for(i=0; i<len; i++) {
                                  mText1.append((char) rbuf[i]);
                                 }

            Text = mText1.toString();
            String[] Splited = Text.split("#");


            Long r = Long.parseLong(Splited[1], 16);   
            Float f = Float.intBitsToFloat(r.intValue()); 
            d1 = f.toString(); 
            val.setText(d1);


            Long r2 = Long.parseLong(Splited[2], 16);   
            Float f2 = Float.intBitsToFloat(r2.intValue()); 
            d2 = f2.toString(); 
            val2.setText(d2);


            Long r3 = Long.parseLong(Splited[3], 16);   
            Float f3 = Float.intBitsToFloat(r3.intValue()); 
            d3 = f3.toString(); 
            val3.setText(d3);



            Monitor.setText(Splited[1]);
            Monitor2.setText(Splited[2]);
            Monitor3.setText(Splited[3]);
  }

It was very good until I wanted to make my application do this steps outside of the button onClick, so i take it outside (I'm really new with java) :

package com.application.i;


import java.io.UnsupportedEncodingException;

import jp.ksksue.driver.serial.FTDriver;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FTDI extends Activity{


    // [FTDriver] Object
    FTDriver mSerial;

    // [FTDriver] Permission String
    private static final String ACTION_USB_PERMISSION = "jp.ksksue.tutorial.USB_PERMISSION";

    Button btnRead;
    TextView Monitor,Monitor2,Monitor3;
    TextView val,val2,val3;
    TextView indicator;
    StringBuffer mText1 = new StringBuffer();
    int i,len;
    String Text=null;
    boolean doIT = true;

    String number=null,number2=null,number3=null, hadar1=null, hadar2=null, hadar3=null, d1=null, d2=null, d3=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.ftdisub);

    btnRead = (Button) findViewById(R.id.btnRead);
    Monitor = (TextView)findViewById(R.id.onitor);
    Monitor2 = (TextView)findViewById(R.id.onitor2);
    Monitor3 = (TextView)findViewById(R.id.onitor3);
    val = (TextView)findViewById(R.id.dec);
    val2 = (TextView)findViewById(R.id.dec2);
    val3 = (TextView)findViewById(R.id.dec3);
    indicator = (TextView)findViewById(R.id.indicator);
    // [FTDriver] Create Instance
    mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));



    // [FTDriver] setPermissionIntent() before begin()
    PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    mSerial.setPermissionIntent(permissionIntent);

    // [FTDriver] Open USB Serial
    if(mSerial.begin(FTDriver.BAUD115200)) {
                                            btnRead.setEnabled(true);

                                            Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
                                            } else {
                                                    Toast.makeText(this, "cannot connect", Toast.LENGTH_SHORT).show();
                                                    }




    }

    @Override 
    protected void onResume() {
        if (doIT==true){

            // [FTDriver] Create Read Buffer
            byte[] rbuf = new byte[27];   // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte

           // [FTDriver] Read from USB Serial
            len = mSerial.read(rbuf);

            for(i=0; i<len; i++) {
                                  mText1.append((char) rbuf[i]);
                                 }

            Text = mText1.toString();
            String[] Splited = Text.split("#");


            Long r = Long.parseLong(Splited[1], 16);   
            Float f = Float.intBitsToFloat(r.intValue()); 
            d1 = f.toString(); 
            val.setText(d1);


            Long r2 = Long.parseLong(Splited[2], 16);   
            Float f2 = Float.intBitsToFloat(r2.intValue()); 
            d2 = f2.toString(); 
            val2.setText(d2);


            Long r3 = Long.parseLong(Splited[3], 16);   
            Float f3 = Float.intBitsToFloat(r3.intValue()); 
            d3 = f3.toString(); 
            val3.setText(d3);



            Monitor.setText(Splited[1]);
            Monitor2.setText(Splited[2]);
            Monitor3.setText(Splited[3]);
            doIT=false;

        }


    };

    @Override
    public void onDestroy() {
                            super.onDestroy();
                            // [FTDriver] Close USB Serial
                            mSerial.end();
                            }

    public void onReadClick(View view) throws UnsupportedEncodingException {

       doIT=true;






      }
   }

I just want to read it once without the Button, and for each additive time next, as i press the button the application will do the process again. The main thing is that i want it outside of the button, especially to do it once as FTDI activity is being activated.

The FTDI activity is activated as I click a button on my application main screen.

problem: When i click the FTDI button on the main screen it should start FTDI activity and display 3 float numbers, instead the application is finished with error and close message.

LogCat:

05-15 11:04:46.855: E/AndroidRuntime(9598): FATAL EXCEPTION: main
05-15 11:04:46.855: E/AndroidRuntime(9598): java.lang.RuntimeException: Unable to resume activity {com.application.i/com.application.i.FTDI}: java.lang.NullPointerException
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.os.Looper.loop(Looper.java:137)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.main(ActivityThread.java:4898)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at java.lang.reflect.Method.invoke(Method.java:511)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at dalvik.system.NativeStart.main(Native Method)
05-15 11:04:46.855: E/AndroidRuntime(9598): Caused by: java.lang.NullPointerException
05-15 11:04:46.855: E/AndroidRuntime(9598):     at jp.ksksue.driver.serial.FTDriver.read(FTDriver.java:271)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at jp.ksksue.driver.serial.FTDriver.read(FTDriver.java:257)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at com.application.i.FTDI.onResume(FTDI.java:83)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1188)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.Activity.performResume(Activity.java:5280)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
05-15 11:04:46.855: E/AndroidRuntime(9598):     ... 12 more

P.S : this FTDI activity works fine:

public class FTDI extends Activity{


    // [FTDriver] Object
    FTDriver mSerial;

    // [FTDriver] Permission String
    private static final String ACTION_USB_PERMISSION = "jp.ksksue.tutorial.USB_PERMISSION";

    Button btnRead;
    TextView Monitor,Monitor2,Monitor3;
    TextView val,val2,val3;
    TextView indicator;
    StringBuffer mText1 = new StringBuffer();
    int i,len;
    String Text=null;
    boolean doIT = true;

    String number=null,number2=null,number3=null, hadar1=null, hadar2=null, hadar3=null, d1=null, d2=null, d3=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.ftdisub);

    btnRead = (Button) findViewById(R.id.btnRead);
    Monitor = (TextView)findViewById(R.id.onitor);
    Monitor2 = (TextView)findViewById(R.id.onitor2);
    Monitor3 = (TextView)findViewById(R.id.onitor3);
    val = (TextView)findViewById(R.id.dec);
    val2 = (TextView)findViewById(R.id.dec2);
    val3 = (TextView)findViewById(R.id.dec3);
    indicator = (TextView)findViewById(R.id.indicator);
    // [FTDriver] Create Instance
    mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));



    // [FTDriver] setPermissionIntent() before begin()
    PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    mSerial.setPermissionIntent(permissionIntent);

    // [FTDriver] Open USB Serial
    if(mSerial.begin(FTDriver.BAUD115200)) {
                                            btnRead.setEnabled(true);

                                            Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
                                            } else {
                                                    Toast.makeText(this, "cannot connect", Toast.LENGTH_SHORT).show();
                                                    }




    }



    @Override
    public void onDestroy() {
                            super.onDestroy();
                            // [FTDriver] Close USB Serial
                            mSerial.end();
                            }

    public void onReadClick(View view) throws UnsupportedEncodingException {





           // [FTDriver] Create Read Buffer
           byte[] rbuf = new byte[27];   // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte

          // [FTDriver] Read from USB Serial
           len = mSerial.read(rbuf);

           for(i=0; i<len; i++) {
                                 mText1.append((char) rbuf[i]);
                                }

           Text = mText1.toString();
           String[] Splited = Text.split("#");


           Long r = Long.parseLong(Splited[1], 16);   
           Float f = Float.intBitsToFloat(r.intValue()); 
           d1 = f.toString(); 
           val.setText(d1);


           Long r2 = Long.parseLong(Splited[2], 16);   
           Float f2 = Float.intBitsToFloat(r2.intValue()); 
           d2 = f2.toString(); 
           val2.setText(d2);


           Long r3 = Long.parseLong(Splited[3], 16);   
           Float f3 = Float.intBitsToFloat(r3.intValue()); 
           d3 = f3.toString(); 
           val3.setText(d3);



           Monitor.setText(Splited[1]);
           Monitor2.setText(Splited[2]);
           Monitor3.setText(Splited[3]);




      }
   }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Digol
  • 390
  • 1
  • 15
  • In `onResume()` your first sentence `if (doIT==true){` will false. Because its value is true when you click on button So when app starts `onResume() `will be called at that time `doIT` will be false because we can't perform click on button at that time. – Piyush May 15 '15 at 08:49
  • I delete the 'if' statement and i got the same closing message – Digol May 15 '15 at 09:05
  • You need to initialize this `mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));` in `onResume()` instead of `onCreate()` – Piyush May 15 '15 at 09:07
  • ok i did that, took all the lines from 'mSerial = new ...' to line 'if(mSerial.begin(FTDriver.BAUD115200))' includint the IF content, the application did not finished, it displays my 'TextView's and do not display my float numbers in the 'TextViews' – Digol May 15 '15 at 09:15
  • I did a breakpoint on mSerial and it didn't get there at all – Digol May 15 '15 at 09:20

1 Answers1

0

You have to check the life cycle of your activity. I guess your mSerial is not initialized when the on resume method is called and try to do the stuff in on start

Mann
  • 661
  • 5
  • 9
  • mSerial is initialized at the top, it worked fine with the onClick. and i changed onResume to onStart and it didn't worked. do you have another idea please? – Digol May 15 '15 at 08:27
  • You have a null pointer crash so something is definitely not initialized – Mann May 15 '15 at 08:29
  • Debug the app with some breakpoints and you will see where the null pointer occur – Mann May 15 '15 at 08:31
  • is it possible that when i use the onReadClick the pointer is initialized? – Digol May 15 '15 at 08:32
  • i see that it cant get to a breakpoint at the activity, and if i brackpoint len = mSerial.read(rbuf); as i use stepOver it displays "source not found" – Digol May 15 '15 at 08:33
  • I added the FTDI activity that works fine under LogCat – Digol May 15 '15 at 08:39