美文网首页
Android串口通讯

Android串口通讯

作者: 流水线上的程序员 | 来源:发表于2017-02-09 13:47 被阅读0次

     串口通讯的步骤:

    1.打开串口。

    2.串口处于监听状态

    3.想串口写入数据,串口接收到数据返回数据

    串口的一个基本结构:

    SerialPort类所在的包一定要和上图包名一直,因为串口通讯需要使用jni中的函数。

    SerialPort类:

    package android_serialport_api;

    import java.io.File;

    import java.io.FileDescriptor;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import com.fx.serialporttest.L;

    public class SerialPort {

    /*

    * Do not remove or rename the field mFd: it is used by native method

    * close();

    */

    private FileDescriptor mFd;

    private FileInputStream mFileInputStream;

    private FileOutputStream mFileOutputStream;

    /**

    * 构造方法

    * @param device 串口地址

    * @param baurate 波特率

    * @param flags

    * @throws IOException

    * @throws InterruptedException

    */

    public SerialPort(File device,int baudrate,int flags) {

    /*

    * 检测是否有访问权限

    */

    if(!device.canRead()||!device.canWrite()){

    //如果没有读写权限,尝试chmod命令这个文件

    L.tag("没有读写权限");

    Process su;

    try {

    su = Runtime.getRuntime().exec("/system/bin/su");//获取root读写权限

    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) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

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

    if(mFd==null){

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

    L.tag("native open return null");

    }

    mFileInputStream = new FileInputStream(mFd);

    mFileOutputStream = new FileOutputStream(mFd);

    }

    public FileInputStream getmFileInputStream() {

    return mFileInputStream;

    }

    public void setmFileInputStream(FileInputStream mFileInputStream) {

    this.mFileInputStream = mFileInputStream;

    }

    public FileOutputStream getmFileOutputStream() {

    return mFileOutputStream;

    }

    public void setmFileOutputStream(FileOutputStream mFileOutputStream) {

    this.mFileOutputStream = mFileOutputStream;

    }

    //JNI

    private native static FileDescriptor open(String path,int baudrate,int flags);

    public native void close();

    static {

    System.loadLibrary("serial_port");

    }

    }

    SerialPortFinger类:

    package android_serialport_api;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import com.fx.serialporttest.L;

    public  class SerialPortFinger {

    private static ReadThread readThread;

    private static FileInputStream mFileInputStream;

    private static FileOutputStream mFileOutputStream;

    static String path = "/dev/ttyS0";//设备主板的串口地址,地址有所不同

    public  void startListener(){

    SerialPort serialPort = new SerialPort(new File(path), 9600, 0);//9600是波特率,这个也是有所不同,具体要看设备

    mFileInputStream = serialPort.getmFileInputStream();

    mFileOutputStream = serialPort.getmFileOutputStream();//获取串口写入流

    readThread  = new ReadThread();

    readThread.start();//开启监听

    }

    /**

    * 发送指令到串口

    *

    * @param cmd

    * @return

    */

    public boolean sendCmds(String cmd) {

    boolean result = true;

    byte[] mBuffer = (cmd+"\r\n").getBytes();

    try {

    if (mFileOutputStream != null) {

    mFileOutputStream.write(mBuffer);

    } else {

    result = false;

    }

    } catch (IOException e) {

    e.printStackTrace();

    result = false;

    }

    return result;

    }

    static class ReadThread extends Thread{

    @Override

    public void run() {

    // TODO Auto-generated method stub

    super.run();

    int len;

    StringBuffer sb = new StringBuffer("");

    while(true){ //循环监听串口,读取返回的数据

    byte[] buffer = new byte[1024];

    if(mFileInputStream==null){

    return;

    }

    try {

    len = mFileInputStream.read(buffer);

    if(len>0){

    sb.append(new String(buffer, 0, len));

    }

    if(!sb.toString().equals(""))

    {

    L.tag(sb.toString());//收到串口的返回数据,在日志中打印出来

    }

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    L.tag("接受完成");

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:Android串口通讯

          本文链接:https://www.haomeiwen.com/subject/qzpiittx.html