基于android的串口开发板,210开发板Android系统串口程序

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

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

1、硬件连接

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

2、部署库文件

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

首先进入工程目录,在libs文件下新建armeabi文件夹,然后把libfriendlyrm-hardware.so文件放入此文件夹下。

然后在eclipse中src目录下新建一个包,包名为com.friendlyarm.AndroidSDK,并新建名为HardwareControler.java的文件,写入以下内容:

package com.friendlyarm.AndroidSDK;

import android.util.Log

public 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,可以看到如下图所示选中的四个文件:

f9632c14b308

部署完毕

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

3、布局文件

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

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">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/receive_textview" />

android:id="@+id/rev_tv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/darker_gray" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginLeft="10dp"

android:layout_weight="1"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/send_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" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="horizontal" >

android:id="@+id/openSerial_bt"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:text="@string/open_serial" />

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" />

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" />

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" />

我使用的是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();

}

});

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值