0

我目前正在编写一个在 Android 上与 Arduino Board 交互的应用程序。我可以很好地将串行数据发送到我的 Arduino,但是当我开始尝试接收数据时,我的应用程序在启动时崩溃了。

我目前已将其设置为文本字段从用户那里获取数据,然后发送到 Arduino,Arduino 执行一项功能。

对于我的接收,我在启动时创建了一个新线程,以不断监听来自 Arduino 的数据。每当执行此线程中的代码时,它就会崩溃。

我使用这个库作为我的基础:https ://github.com/mik3y/usb-serial-for-android

以下是此应用程序提供的其他功能的所有相关代码,省略的代码是未实现的:

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



import com.hoho.android.usbserial.driver.UsbSerialDriver;


import android.hardware.usb.UsbManager;
import android.content.Intent;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.util.Log;
import com.hoho.android.usbserial.driver.UsbSerialProber;




     public class MainActivity extends Activity implements SensorEventListener, LocationListener{

private EditText sendTextField;
    TextView uartRX;

 public Handler Handler; 


UsbManager manager;



/** Called to create application activity*/
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    uartRX = (TextView)findViewById(R.id.rxUART);

    sendTextField = (EditText) findViewById(R.id.sendTextField);






    Handler = new Handler() { 
        @Override public void handleMessage(Message msg) { 
        String text = (String)msg.obj; 
        uartRX.append(text); 




        } 
        };


        Thread t = new Thread(new Runnable() {
             public void run()
             {
                 Message msg = new Message(); 
                    msg.obj = "Start Serial Listen\n"; 
                    Handler.sendMessage(msg);
                 rxUART();
             }

            public void rxUART()
            {

                manager = (UsbManager) getSystemService(Context.USB_SERVICE);
                UsbSerialDriver rxDriver = UsbSerialProber.acquire(manager);
                if (rxDriver != null) {
                      try {
                          rxDriver.open();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }


                int byteSize = 0;
                String data = "";
                while(true)
                {
                byte[] arr = null;
                try {
                    rxDriver.setBaudRate(115200);
                    rxDriver.read(arr, byteSize);
                    data = new String(arr);
                    Message msg = new Message(); 
                    msg.obj = data; 
                    Handler.sendMessage(msg);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            }

    });
    t.start();

    }


public void sendUART(String data)
{
    manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    // Find the first available driver.
    UsbSerialDriver driver = UsbSerialProber.acquire(manager);
    if (driver != null) {
          try {
            driver.open();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
          try {
            driver.setBaudRate(115200);

            byte buffer[] = new byte[16];
            int numBytesRead = driver.read(buffer, 1000);
            Log.d(TAG, "Read " + numBytesRead + " bytes.");


            byte temp[] = data.getBytes();

            driver.write(temp, 16);

          } catch (IOException e) {
            // Deal with error.
          } finally {
            try {
                driver.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
          } 
        }



}
   public void onClickSendBtn(View v)
    {
       String data = "";
       data = sendTextField.getText().toString();
       sendUART(data);
       sendTextField.setText(" ");
    } 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}







  }

谁能帮我弄清楚为什么应用程序在线程中创建的代码上崩溃?

注意:代码不在线程中时也会崩溃。代码最初是由 sendUART() 调用以检查响应的函数。也没有 while(true)。

日志猫:

   11-02 09:49:22.988: E/AndroidRuntime(22652): FATAL EXCEPTION: Thread-964
   11-02 09:49:22.988: E/AndroidRuntime(22652): java.lang.NullPointerException
   11-02 09:49:22.988: E/AndroidRuntime(22652):     at       com.example.sensortest.MainActivity$2.rxUART(MainActivity.java:143)
   11-02 09:49:22.988: E/AndroidRuntime(22652):     at com.example.sensortest.MainActivity$2.run(MainActivity.java:119)
   11-02 09:49:22.988: E/AndroidRuntime(22652):     at java.lang.Thread.run(Thread.java:856)

引用的行号是:

           Thread t = new Thread(new Runnable() {
             public void run()
             {
                 Message msg = new Message(); 
                    msg.obj = "Start Serial Listen\n"; 
                    Handler.sendMessage(msg);
                 rxUART();
             }

            public void rxUART()
            {

                manager = (UsbManager) getSystemService(Context.USB_SERVICE);
                UsbSerialDriver rxDriver = UsbSerialProber.acquire(manager);
                if (rxDriver != null) {
                      try {
                          rxDriver.open();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }


                int byteSize = 0;
                String data = "";
                while(true)
                {
                byte[] arr = null;
                try {
                    rxDriver.setBaudRate(115200);
                    rxDriver.read(arr, byteSize);
                    data = new String(arr);
                    Message msg = new Message(); 
                    msg.obj = data; 
                    Handler.sendMessage(msg);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            }

    });
    t.start();
4

1 回答 1

1

您没有正确处理此案rxDriver == nullrxUart()

if()语句仅跳过open()调用,但while()执行循环并rxDriver导致NULLNPE。顺便说一句,您在`sendUART() 中遇到了同样的问题。

于 2013-11-02T18:23:01.113 回答