求大神解释。。。串口接收返回数据的问题???接收不到返回数据

qq_38862638 2017-06-16 11:25:36
在IntentService里写的俩方法,
发送方法是可以的
但是怎么接收返回数据???
接收方法一直收不到
广播发出去了,但没有数据去更新UI

//发送方法
private void send(){
//启动定时器,2秒后执行,每2秒执行一次
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
try{
byte[] b = {0,1,0,0};
mOutputStream.write(b,0,3);
}catch(Exception e){
e.printStackTrace();
}
}
};
timer.schedule(task,2000,2000);
}

//接收具体方法的
private void rec(){
//启动定时器,2秒后执行,每2秒执行一次
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
int size;
byte[] buffer = new byte[1024];
if(mInputStream == null){
return;
}
try {
size = mInputStream.read(buffer);
if(size > 0){
String recinfo = new String(buffer,0,size);
//将返回的数据通过广播发送
Intent intent = new Intent();
intent.setAction(MainActivity.TEMPSERVICE);
intent.putExtra("count",recinfo);
sendBroadcast(intent);
}
} catch (IOException e) {
e.printStackTrace();
}

}
};
timer.schedule(task,2000,2000);
}
...全文
680 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
chickenmay 2017-06-19
  • 打赏
  • 举报
回复
解决了吗? 。。。。
qq_38862638 2017-06-19
  • 打赏
  • 举报
回复
引用 12 楼 qq_35001400 的回复:
解决了吗? 。。。。


qq_38862638 2017-06-19
  • 打赏
  • 举报
回复
引用 12 楼 qq_35001400 的回复:
解决了吗? 。。。。
没有啊
santan 2017-06-18
  • 打赏
  • 举报
回复
猜测是in、out用native返回的fd构造造成工作不正常,建议write和read直接在jni里实现
gd6321374 2017-06-18
  • 打赏
  • 举报
回复
引用 4 楼 qq_35001400 的回复:
流程看到了 你打个断点看看 send()和 rec();这两个方法,走到哪里没有走了 我没有你那个SerialPort类 运行不了
大神,小弟,我用模拟器操作电脑串口,使用下面这段代码 public class readThread extends Thread { @Override public void run() { Log.d(TAG, "打开接收线程"); super.run(); byte[] recBuffer = new byte[2048]; while(!isInterrupted()) { try { int availlLength = inputStream.available(); if(availlLength > 0) { Log.d(TAG, "availlLength = "+availlLength); } int recLength = inputStream.read(); if(recLength != -1) { byte[] data = new byte[recLength]; System.arraycopy(recBuffer, 0, data, 0, recLength); onDataReceiveListener.onReceive(data); Log.d(TAG, "接收到数据"); } try { Thread.sleep(10);// 延时50ms } catch (InterruptedException e1) { e1.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } } } 每次只能接收8字节数据呢???????我是用串口调试助手发送10字节或则9字节数据,程序立马死掉不能执行,连异常都不报。 请问是用这种方式接收串口数据是不是每次只能接收8字节数据,如果超过8字节数据该怎么办呢??????? 而且inputstrem,read(), 不是分包接收的,所以我只要发送超过8字节数据程序就会死掉。 我在网上找遍了,还是没有找到相关资料。。。。。。。。能不能给小弟一点提示呢??? 先谢谢了。。。。。。。。。。。。。。。。。。。。。
qq_38862638 2017-06-17
  • 打赏
  • 举报
回复
引用 7 楼 qq_35001400 的回复:
System.loadLibrary("serial_port"); 你这个lib是不是加载写错了方法?libserial_port 改成这样子


大神,看看我的文件结构
qq_38862638 2017-06-17
  • 打赏
  • 举报
回复
依然没解决,那个值啥都没有。。。。。求大神
chickenmay 2017-06-16
  • 打赏
  • 举报
回复
System.loadLibrary("serial_port"); 你这个lib是不是加载写错了方法?libserial_port 改成这样子
qq_38862638 2017-06-16
  • 打赏
  • 举报
回复
引用 4 楼 qq_35001400 的回复:
流程看到了 你打个断点看看 send()和 rec();这两个方法,走到哪里没有走了 我没有你那个SerialPort类 运行不了

package android_serialport_api;

import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialPort {

	private static final String TAG = "SerialPort";
	private FileDescriptor mFd;
	private FileInputStream mFileInputStream;
	private FileOutputStream mFileOutputStream;

	public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {

		//检查访问权限,如果没有读写权限,进行文件操作,修改文件访问权限
		if (!device.canRead() || !device.canWrite()) {
			try {
				//通过挂在到linux的方式,修改文件的操作权限
				Process su = Runtime.getRuntime().exec("/system/xbin/su");
				String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";
				su.getOutputStream().write(cmd.getBytes());

				if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
					throw new SecurityException();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new SecurityException();
			}
		}

		mFd = open(device.getAbsolutePath(), baudrate, flags);

		if (mFd == null) {
			Log.e(TAG, "native open returns null");
			throw new IOException();
		}

		mFileInputStream = new FileInputStream(mFd);
		mFileOutputStream = new FileOutputStream(mFd);
	}

	// Getters and setters
	public InputStream getInputStream() {
		return mFileInputStream;
	}

	public OutputStream getOutputStream() {
		return mFileOutputStream;
	}


	// JNI(调用java本地接口,实现串口的打开和关闭)
/**串口有五个重要的参数:串口设备名,波特率,检验位,数据位,停止位
 其中检验位一般默认位NONE,数据位一般默认为8,停止位默认为1*/
	/**
	 * @param path     串口设备的据对路径
	 * @param baudrate 波特率
	 * @param flags    校验位
	 */
	private native static FileDescriptor open(String path, int baudrate, int flags);

	public native void close();

	static {//加载jni下的C文件库
		System.loadLibrary("serial_port");
	}
}

chickenmay 2017-06-16
  • 打赏
  • 举报
回复
引用 4 楼 qq_35001400 的回复:
流程看到了 你打个断点看看 send()和 rec();这两个方法,走到哪里没有走了 我没有你那个SerialPort类 运行不了
你这个rec方法里面异步任务有点问题,好像直接没有走 看看是不是写错了
chickenmay 2017-06-16
  • 打赏
  • 举报
回复
流程看到了 你打个断点看看 send()和 rec();这两个方法,走到哪里没有走了 我没有你那个SerialPort类 运行不了
qq_38862638 2017-06-16
  • 打赏
  • 举报
回复
再来一遍完整的,菜鸟代码,见笑见笑!!!

package com.daogukeji.testday01.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

import com.daogukeji.testday01.R;
import com.daogukeji.testday01.service.tempIntentService;

public class MainActivity extends AppCompatActivity {
    public static final String TEMPSERVICE = "com.daogukeji.testday01.service.tempIntentService";//广播标志
    TextView text1;
    //温湿度广播接收器
    private BroadcastReceiver tempReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(TEMPSERVICE)){
                text1.setText("温度:"+intent.getExtras().getString("count")+"度");
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text1 = (TextView) findViewById(R.id.text1);

        //启动服务
        IntentFilter tempFilter = new IntentFilter();//意图过滤器
        tempFilter.addAction(TEMPSERVICE);//添加
        registerReceiver(tempReceiver,tempFilter);//注册广播
        Intent tempIntent = new Intent(MainActivity.this,tempIntentService.class);//发送意图
        startService(tempIntent);//启动服务
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        unregisterReceiver(tempReceiver);//注销广播接收器
    }
}


package com.daogukeji.testday01.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.daogukeji.testday01.activity.MainActivity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import android_serialport_api.SerialPort;

/**
 * Created by dell on 2017/6/16.
 * 温湿度数据类
 */

public class tempIntentService extends IntentService{
    private String TAG = "tempIntentService";//
    private Timer timer;//定时器
    private TimerTask task;
    private int count;

    //串口相关参数
    private SerialPort mSerialPort;
    protected InputStream mInputStream;
    protected OutputStream mOutputStream;
    private String prot = "ttyS3";
    private int baudrate = 9600;

    //无参构造
    public tempIntentService(){
        super("temtIntentService");
    }
    //有参构造
    public tempIntentService(String name){
        super(name);
    }

    //onHandleIntent执行耗时任务
    @Override
    public void onHandleIntent(Intent intent){
        if(null != intent){//若有意图
            OpenSerial();
            send();
            rec();
        }
    }
    //打开串口方法
    private void OpenSerial() {
        try {
            mSerialPort = new SerialPort(new File("/dev/" + prot), baudrate,
                    0);
            mInputStream = mSerialPort.getInputStream();
            mOutputStream = mSerialPort.getOutputStream();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //发送方法
    private void send(){
        //启动定时器,2秒后执行,每2秒执行一次
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                try{
                    byte[] b = {0,1,0,0};
                    mOutputStream.write(b,0,3);

                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        };
        timer.schedule(task,2000,2000);
    }

    //接收具体方法的
    private void rec(){
        //启动定时器,2秒后执行,每2秒执行一次
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                int size;
                byte[] buffer = new byte[1024];
                if(mInputStream == null){
                    return;
                }
                try {
                    size = mInputStream.read(buffer);
                    if(size > 0){
                        String recinfo = new String(buffer,0,size);

                        //将返回的数据通过广播发送
                        Intent intent = new Intent();
                        intent.setAction(MainActivity.TEMPSERVICE);
                        intent.putExtra("count",recinfo);
                        sendBroadcast(intent);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        timer.schedule(task,2000,2000);
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

qq_38862638 2017-06-16
  • 打赏
  • 举报
回复
引用 1 楼 qq_35001400 的回复:
rec方法在哪里调用的? 你贴的这个代码看不出来 广播注册没有,你打断点调式看看方法怎么走的 没有走就是哪个环节少了
rec在onHandleIntent调用的,广播已在MainActivity中注册过,用广播发普通字符串可以更新UI,但是这个返回的串口数据获取不到
chickenmay 2017-06-16
  • 打赏
  • 举报
回复
rec方法在哪里调用的? 你贴的这个代码看不出来 广播注册没有,你打断点调式看看方法怎么走的 没有走就是哪个环节少了

80,471

社区成员

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

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