关于Android串口编程求助

一条不更新的懒狗 2017-09-28 11:03:00
最近做android的串口开发,参考了网上的例子。和大家一样都是使用谷歌提供的串口实例。 http://code.google.com/p/android-serialport-api/ 导入后能正常的收发数据。

但是我做到多个Activity的时候,发现在接收数据的时候。接收数据的ReadThread不是当前Activity的ReadThread,而是前面的Activity的ReadThread。但是前面的Activity已经关闭了,所以造成数据丢失。请问如何解决:


我的Activity如下: 参考博客链接 http://blog.csdn.net/ckw474404603/article/details/37811499

public abstract class SerialPortActivity extends Activity {  

protected Application mApplication;
protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;

private class ReadThread extends Thread {

@Override
public void run() {
super.run();
while (!isInterrupted()) {
int size;
try {
byte[] buffer = new byte[64];
if (mInputStream == null) {
return;
}
size = mInputStream.read(buffer);
if (size > 0) {
onDataReceived(buffer, size);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}

private void DisplayError(int resourceId) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Error");
b.setMessage(resourceId);
b.setPositiveButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SerialPortActivity.this.finish();
}
});
b.show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (Application) getApplication();
try {
mSerialPort = mApplication.getSerialPort();
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream();

/* Create a receiving thread */
mReadThread = new ReadThread();
mReadThread.start();
} catch (SecurityException e) {
DisplayError(R.string.error_security);
} catch (IOException e) {
DisplayError(R.string.error_unknown);
} catch (InvalidParameterException e) {
DisplayError(R.string.error_configuration);
}
}

protected abstract void onDataReceived(final byte[] buffer, final int size);

@Override
protected void onDestroy() {
if (mReadThread != null)
mReadThread.interrupt();
mApplication.closeSerialPort();
mSerialPort = null;
super.onDestroy();
}
}
...全文
381 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 2 楼 alexxm_001 的回复:
SerialPortActivity.java中修改 protected void onDestroy() { if (mReadThread != null) { mReadThread.interrupt(); mReadThread.stop(); mReadThread=null; } mApplication.closeSerialPort(); mSerialPort = null; super.onDestroy(); }
Caused by: java.lang.UnsupportedOperationException at java.lang.Thread.stop(Thread.java:1052) at java.lang.Thread.stop(Thread.java:1042) at shangexi.baiheng.com.shangexi.base.SerialPortActivity.onStop(SerialPortActivity.java:130) 在stop这个地方异常了
  • 打赏
  • 举报
回复
Caused by: java.lang.UnsupportedOperationException at java.lang.Thread.stop(Thread.java:1052) at java.lang.Thread.stop(Thread.java:1042) at shangexi.baiheng.com.shangexi.base.SerialPortActivity.onStop(SerialPortActivity.java:130) 在stop这个地方异常了
辉_alexxm 2017-09-29
  • 打赏
  • 举报
回复
SerialPortActivity.java中修改 protected void onDestroy() { if (mReadThread != null) { mReadThread.interrupt(); mReadThread.stop(); mReadThread=null; } mApplication.closeSerialPort(); mSerialPort = null; super.onDestroy(); }
辉_alexxm 2017-09-29
  • 打赏
  • 举报
回复
用服务的方式试下,以前我是关闭线程的,会提示错误,问题出在read 上 ,一直不退出;
  • 打赏
  • 举报
回复
引用 5 楼 alexxm_001 的回复:
我找了下InputStream,换了一个方式,可以参考以下的内容 https://zhidao.baidu.com/question/577343889.html 我写的代码如下: while(!isInterrupted()) { int size; try { byte[] buffer = new byte[64]; if (mInputStream == null) return; int available=mInputStream.available(); if(available!=0) { size = mInputStream.read(buffer); if (size > 0) { onDataReceived(buffer, size); Log.v("SerialPortActivity", "ReadThread size:" + size); } } else { // Log.v("SerialPortActivity","available size:"+available); } } catch (IOException e) { e.printStackTrace(); return; } try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 这样要注意串口数据的串接问题,有问题时留意下!
感谢你的回答,我测试你的答案。 在sleep这一行发生了异常,不过被抓取了:
09-29 14:18:58.687 26036-26349/com.huruwo.sportthread W/System.err: java.lang.InterruptedException
09-29 14:18:58.687 26036-26349/com.huruwo.sportthread W/System.err:     at java.lang.VMThread.sleep(Native Method)
09-29 14:18:58.687 26036-26349/com.huruwo.sportthread W/System.err:     at java.lang.Thread.sleep(Thread.java:1013)
09-29 14:18:58.697 26036-26349/com.huruwo.sportthread W/System.err:     at java.lang.Thread.sleep(Thread.java:995)
09-29 14:18:58.697 26036-26349/com.huruwo.sportthread W/System.err:     at com.huruwo.sportthread.Data1Activity$myThread.run(Data1Activity.java:150)
09-29 14:18:58.697 26036-26349/com.huruwo.sportthread W/System.err: java.io.IOException: ioctl failed: EBADF (Bad file number)
09-29 14:18:58.697 26036-26349/com.huruwo.sportthread W/System.err:     at libcore.io.IoBridge.available(IoBridge.java:68)
09-29 14:18:58.707 26036-26349/com.huruwo.sportthread W/System.err:     at java.io.FileInputStream.available(FileInputStream.java:110)
09-29 14:18:58.707 26036-26349/com.huruwo.sportthread W/System.err:     at com.huruwo.sportthread.Data1Activity$myThread.run(Data1Activity.java:130)
09-29 14:18:58.707 26036-26349/com.huruwo.sportthread W/System.err: Caused by: libcore.io.ErrnoException: ioctl failed: EBADF (Bad file number)
09-29 14:18:58.717 26036-26349/com.huruwo.sportthread W/System.err:     at libcore.io.Posix.ioctlInt(Native Method)
09-29 14:18:58.717 26036-26349/com.huruwo.sportthread W/System.err:     at libcore.io.ForwardingOs.ioctlInt(ForwardingOs.java:84)
09-29 14:18:58.717 26036-26349/com.huruwo.sportthread W/System.err:     at libcore.io.IoBridge.available(IoBridge.java:52)
09-29 14:18:58.727 26036-26349/com.huruwo.sportthread W/System.err: 	... 2 more
但是数据算是正确的传到了
辉_alexxm 2017-09-29
  • 打赏
  • 举报
回复
我找了下InputStream,换了一个方式,可以参考以下的内容 https://zhidao.baidu.com/question/577343889.html 我写的代码如下: while(!isInterrupted()) { int size; try { byte[] buffer = new byte[64]; if (mInputStream == null) return; int available=mInputStream.available(); if(available!=0) { size = mInputStream.read(buffer); if (size > 0) { onDataReceived(buffer, size); Log.v("SerialPortActivity", "ReadThread size:" + size); } } else { // Log.v("SerialPortActivity","available size:"+available); } } catch (IOException e) { e.printStackTrace(); return; } try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 这样要注意串口数据的串接问题,有问题时留意下!

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧