最近由于项目需要,花了两天时间在Android平台下编写了一个串口助手软件。硬件平台是友善之臂的tiny210开发板。起初的想法是首先基于Linux驱动做一些修改,然后自行编写HAL层代码,最后编写Android应用程序调用HAL Stub来实现和串口通信。后来在网上看到友善之臂为Android系统操作硬件资源专门开发了一个库文件:libfriendlyarm-hardware.so,通过它我们可以很方便的操作串口。于是对这个文件深入了解了一下。

网上可以搜出很多关于这个文件用法的博客,但是全部都是同样的内容,也不知谁是原创。并且博客中的代码个人觉得有些地方略显多余,亲测了一下,串口程序会有明显的卡顿(也许是本人开发板硬件条件问题),于是自己按照官方的接口说明编写了一个程序。测试通过之后总算搞定,现在把我的过程和源代码分享给大家,希望能帮到大家。(工程源码GitHub地址见文章结尾处)

1、硬件连接

首先我们要用串口线连接PC和开发板,注意需要用交叉线相连。可以用友善自带Android系统中的串口助手测试一下,保证串口的线路通畅

2、部署库文件

这里按照配套光盘里的开发板用户手册4.2.1节介绍的方法部署(点击可下载)。

  1. 首先进入工程目录,在libs文件下新建armeabi文件夹,然后把libfriendlyrm-hardware.so文件放入此文件夹下。
  2. 然后在eclipse中src目录下新建一个包,包名为com.friendlyarm.AndroidSDK,并新建名为HardwareControler.java的文件,写入以下内容:

    package com.friendlyarm.AndroidSDK;import android.util.Logpublic class HardwareControler{ //Serial Port static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );      static public native int write(int fd, byte[] data); static public native int read(int fd, byte[] buf, int len); static public native int select(int fd, int sec, int usec); static public native void close(int fd); static {          try {                 System.loadLibrary("friendlyarm-hardware");                } catch (UnsatisfiedLinkError e) {                        Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");    }}}

    部署完后,右键工程→fresh,可以看到如下图所示选中的四个文件:


    部署完毕
  3. 接下来就可以新建我们的应用代码了。在代码开头导入 com.friendlyarm.AndroidSDK.HardwareControler就可以。

3、布局文件

工程比较简单,这里只需要一个布局文件即可。我只用了一个接收显示区,一个发送显示区和关闭、打开、发送、清空四个按钮。布局文件代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:baselineAligned="false"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".HardwareControler"     android:background="@android:color/holo_green_dark">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1"        android:orientation="vertical" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/receive_textview" />        <TextView            android:id="@+id/rev_tv"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="@android:color/darker_gray" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginLeft="10dp"        android:layout_weight="1"        android:orientation="vertical" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/send_edittext" />        <EditText            android:hint="@string/data_to_send"            android:id="@+id/send_et"            android:layout_width="match_parent"            android:layout_height="60dp"            android:background="@android:color/darker_gray" />        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:orientation="horizontal" >            <Button                android:id="@+id/openSerial_bt"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_margin="20dp"                android:text="@string/open_serial" />            <Button                android:id="@+id/closeSerial_bt"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_toRightOf="@id/openSerial_bt"                android:layout_alignTop="@id/openSerial_bt"                android:text="@string/close_serial"                android:enabled="false" />            <Button                android:id="@+id/sendSerial_bt"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignLeft="@id/openSerial_bt"                android:layout_below="@id/openSerial_bt"                android:text="@string/send_data" />            <Button                android:id="@+id/clear_bt"                android:layout_marginLeft="20dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignTop="@id/sendSerial_bt"                android:layout_toRightOf="@id/sendSerial_bt"                android:text="@string/clear_data" />        </RelativeLayout>    </LinearLayout></LinearLayout>

我使用的是7寸的横屏,大家可以按照自己的屏幕来适配不同的代码。

4、Java代码编写

首先需要了解库文件为我们提供的接口函数,文档里有详细的解释:

  • int openSerialPort( String devName, long baud, int dataBits, int stopBits )
    打开指定的串口设备,并返回文件描述符。
  • int write( int fd, byte[] data)
    向打开的设备或文件中写数据。
  • int read( int fd, byte[] buf, int len)
    从打开的设备或文件中读取数据。buf是接收缓存,len是需要接收的字节数,函数返回实际接收的字节数。
  • int select( int fd, int sec, int usec)
    查询打开的设备或文件是否有数据可读。
  • void close(int fd)
    关闭指定的文件描述符。

代码说明:
首先用户点击打开按键,此时会开启监听进程通过select函数不断监听是否有数据进来,如果有,则通过handler发送消息通知主线程显示。如下:

openSerial.setOnClickListener(new Button.OnClickListener()        {            @Override            public void onClick(View v)            {                fd = HardwareControler.openSerialPort("/dev/s3c2410_serial3",                        115200, 8, 1);                openFlag = true;                Toast.makeText(getApplicationContext(),                        getString(R.string.open_sucssess), Toast.LENGTH_SHORT)                        .show();                openSerial.setEnabled(false);                closeSerial.setEnabled(true);                /**                 * 启动线程监听数据                 */                listen = new Thread(new Runnable()                {                    @Override                    public void run()                    {                        while (openFlag)                        {                            int m = HardwareControler.select(fd, 2, 20);                            int n;                            String text = "";                            if (m == 1)                            {                                while ((n = HardwareControler.read(fd, buf,                                        buf.length)) > 0)                                {                                    try                                    {                                        Thread.sleep(90);                // 睡眠等待数据完全接收                                    } catch (InterruptedException e)                                    {                                        e.printStackTrace();                                    }                                    for (int i = 0; i < n; i++)                                    {                                        text += (char) buf[i];                                    }                                };                                Log.d("MC", "n:" + n);                                Message message = Message.obtain();                                message.obj = text;                                revHandler.sendMessage(message);                                System.out.println(Arrays.toString(buf));                            }                            try                            {                                Thread.sleep(1000);                            } catch (InterruptedException e)                            {                                e.printStackTrace();                            }                        }                    }                });                listen.start();            }        });

这里需要注意的是:在while反复判断read返回值时,需要有一个短暂的睡眠以等待数据全部进入串口驱动程序,否则会出现数据间断的情况。此时,用户可以随时通过发送按键讲发送框里的数据发送出去,代码如下:

public void SendSerial()    {        HardwareControler.write(fd, send_et.getText().toString().getBytes());    }/** * 发送数据 */    sendSerial.setOnClickListener(new Button.OnClickListener()    {        public void onClick(View v)        {            SendSerial();        }    });

最后关闭串口:

/*** 关闭串口 */    closeSerial.setOnClickListener(new Button.OnClickListener()    {        @Override        public void onClick(View v)        {            HardwareControler.close(fd);            openFlag = false;            openSerial.setEnabled(true);            closeSerial.setEnabled(false);            Toast.makeText(getApplicationContext(),                    getString(R.string.close_sucssess), Toast.LENGTH_SHORT)                    .show();        }    });

到这里,基本的功能已经全部实现,亲测之后对于功能上还算过关,界面比较粗糙。欢迎各位朋友对代码进行修正补缺,有什么问题也可以给我留言,一起探讨。

tiny210开发板用户手册下载:
http://yun.baidu.com/share/link?shareid=2019636114&uk=67973003
apk下载链接:
http://yun.baidu.com/share/link?shareid=2360935897&uk=67973003
GitHub源码下载链接:
https://github.com/hust-MC/Android-Serial.git

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 一款常用的 Squid 日志分析工具
  3. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  4. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  5. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  6. Android(安卓)重要数据目录
  7. [置顶] Android(安卓)View视图------Android如何创建一个view。
  8. Android(安卓)Launcher研究(四)-----------桌面应用快捷方式的开
  9. Android增加一个物理按键检测步骤

随机推荐

  1. mysql中find_in_set函数的基本使用方法
  2. 在IntelliJ IDEA中使用Java连接MySQL数据
  3. MySQL8.0中的降序索引
  4. 详解mysql中的存储引擎
  5. MySQL 两种恢复数据的方法
  6. MySQL 8.0 之索引跳跃扫描(Index Skip Sc
  7. mysql的登陆和退出命令格式
  8. MySql设置指定用户数据库查看查询权限
  9. MySQL group by和order by如何一起使用
  10. MySQL 8.0.19安装详细教程(windows 64位)