一般android系统的手机硬件上支持stm32串口硬件流控制吗

Copyright(c),All Rights Reserved
网络尖刀 版权所有 京ICP备号-13143人阅读
android 智能电视开发(9)
android(11)
系统服务实际上是调用了远程Bind对象进行操作
1:添加aidl文件到&framworks/base/core/java/android/hardware/ISerialManager.aidl//这个文件是需要自己现实的
2:编译脚本中添加申明
&&frameworks/base/Android.mk: & & core/java/android/hardware/ISerialManager.aidl \
3:Context文件中添加变量
frameworks/base/core/java/android/content/Context.java: & &public static final String SERIAL_SERVICE = &serial&;
4:添加调用远程服务的文件
& & & & & & & frameworks/base/services/java/com/android/server/&SerialService.java//这个文件是需要自己现实的&
& & & & & 到frameworks/base/services/java/com/android/server/SystemServer.java:
&例如:public class SerialService extends ISerialManager.Stub文件
& & & & & & & & Slog.i(TAG, &Serial Service&);
& & & & & & & & // Serial port support
& & & & & & & & serial = new SerialService(context);
& & & & & & & & ServiceManager.addService(Context.SERIAL_SERVICE, serial);
& & & & & & } catch (Throwable e) {
& & & & & & & & Slog.e(TAG, &Failure starting SerialService&, e);
& & & & & & }
5:添加注册服务到 frameworks/base/core/java/android/app/ContextImpl.java: &获取SerialManager
import android.hardware.SerialM
import android.hardware.ISerialM//这两个文件是需要自己现实的&
&构造方法中传入两个参数&public SerialManager(Context context, ISerialManager service) {
& & & & mContext =
& & & & mService =
& & & & registerService(SERIAL_SERVICE, new ServiceFetcher() {
& & & & & & & & public Object createService(ContextImpl ctx) {
& & & & & & & & & & IBinder b = ServiceManager.getService(SERIAL_SERVICE);
& & & & & & & & & & return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
& & & & & & & & }});
接下来就可以通过(SerialManager)getSystemService(Context.SERIAL_SERVICE);来获取SerialManager
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:74623次
排名:千里之外
原创:10篇
评论:21条210开发板Android系统串口程序 - 简书
下载简书移动应用
写了52287字,被210人关注,获得了412个喜欢
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);
System.loadLibrary("friendlyarm-hardware");
} catch (UnsatisfiedLinkError e) {
Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
部署完后,右键工程→fresh,可以看到如下图所示选中的四个文件:
接下来就可以新建我们的应用代码了。在代码开头导入 com.friendlyarm.AndroidSDK.HardwareControler就可以。
3、布局文件
工程比较简单,这里只需要一个布局文件即可。我只用了一个接收显示区,一个发送显示区和关闭、打开、发送、清空四个按钮。布局文件代码如下:
&LinearLayout xmlns:android="/apk/res/android"
xmlns:tools="/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" &
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" /&
&/LinearLayout&
&LinearLayout
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" /&
&RelativeLayout
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" /&
&/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()
public void onClick(View v)
fd = HardwareControler.openSerialPort("/dev/s3c2410_serial3",
openFlag =
Toast.makeText(getApplicationContext(),
getString(R.string.open_sucssess), Toast.LENGTH_SHORT)
openSerial.setEnabled(false);
closeSerial.setEnabled(true);
* 启动线程监听数据
listen = new Thread(new Runnable()
public void run()
while (openFlag)
int m = HardwareControler.select(fd, 2, 20);
String text = "";
if (m == 1)
while ((n = HardwareControler.read(fd, buf,
buf.length)) & 0)
Thread.sleep(90);
// 睡眠等待数据完全接收
} catch (InterruptedException e)
e.printStackTrace();
for (int i = 0; i & i++)
text += (char) buf[i];
Log.d("MC", "n:" + n);
Message message = Message.obtain();
message.obj =
revHandler.sendMessage(message);
System.out.println(Arrays.toString(buf));
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()
public void onClick(View v)
HardwareControler.close(fd);
openFlag =
openSerial.setEnabled(true);
closeSerial.setEnabled(false);
Toast.makeText(getApplicationContext(),
getString(R.string.close_sucssess), Toast.LENGTH_SHORT)
到这里,基本的功能已经全部实现,亲测之后对于功能上还算过关,界面比较粗糙。欢迎各位朋友对代码进行修正补缺,有什么问题也可以给我留言,一起探讨。
tiny210开发板用户手册下载:apk下载链接:GitHub源码下载链接:
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
Write the Code, Change the World, 改变世界从编程开始, 收集编程相关的干货
· 11020人关注
Android老鸟给新人的建议、资源。
更优质的原创内容,欢迎关注技术公众号,微信搜索:“Open软件开发小组”或者“open_dev”
· 10225人关注
机械 · 电子 · 嵌入式 · 制造业 · 想象力
· 3024人关注
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:通过FT4232芯片,Android系统的设备可以外扩串口
您好,欢迎来到61ic!
&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&&.&
您现在的位置:&&>>&&>>&&>>&&>>&&>>&资源信息
专 题 栏 目
最 新 调 查
&&&&下载中心用户访问调查?通过朋友推荐访问通过百度搜索引擎访问通过Google搜索引擎访问通过其他方式访问&&
通过FT4232芯片,Android系统的设备可以外扩串口
运行环境:&Win9x/NT/2000/XP/2003
文件大小:&860&K
软件等级:&★★★
软件类别:&
开 发 商:&
软件语言:&
相关链接:&&&
软件属性:&&&&&&&&&
下载次数:&本日:  &本周:&&     &&本月: & 总计:
授权方式:&
解压密码:&
软件添加:&审核:admin&录入:admin
添加时间:& 13:25:45
::下载地址::
::软件简介::
demo_source&&...........\.classpath&&...........\.project&&...........\.settings&&...........\.........\org.eclipse.jdt.core.prefs&&...........\.........\org.eclipse.ltk.core.refactoring.prefs&&...........\AndroidManifest.xml&&...........\doc&&...........\...\allclasses-frame.html&&...........\...\allclasses-noframe.html&&...........\...\com&&...........\...\...\ftdi&&...........\...\...\....\j2xx&&...........\...\...\....\....\class-use&&...........\...\...\....\....\.........\D2xxManager.D2xxException.html&&...........\...\...\....\....\.........\D2xxManager.DriverParameters.html&&...........\...\...\....\....\.........\D2xxManager.FtDeviceInfoListNode.html&&...........\...\...\....\....\.........\D2xxManager.html&&...........\...\...\....\....\.........\FT_Device.html&&...........\...\...\....\....\.........\FT_EEPROM.html&&...........\...\...\....\....\.........\FT_EEPROM_2232D.html&&...........\...\...\....\....\.........\FT_EEPROM_2232H.DRIVE_STRENGTH.html&&...........\...\...\....\....\.........\FT_EEPROM_2232H.html&&...........\...\...\....\....\.........\FT_EEPROM_232H.CBUS.html&&...........\...\...\....\....\.........\FT_EEPROM_232H.DRIVE_STRENGTH.html&&...........\...\...\....\....\.........\FT_EEPROM_232H.html&&...........\...\...\....\....\.........\FT_EEPROM_232R.CBUS.html&&...........\...\...\....\....\.........\FT_EEPROM_232R.html&&...........\...\...\....\....\.........\FT_EEPROM_245R.CBUS.html&&...........\...\...\....\....\.........\FT_EEPROM_245R.html&&...........\...\...\....\....\.........\FT_EEPROM_4232H.DRIVE_STRENGTH.html&&...........\...\...\....\....\.........\FT_EEPROM_4232H.html&&...........\...\...\....\....\.........\FT_EEPROM_X_Series.CBUS.html&&...........\...\...\....\....\.........\FT_EEPROM_X_Series.DRIVE_STRENGTH.html&&...........\...\...\....\....\.........\FT_EEPROM_X_Series.html&&...........\...\...\....\....\D2xxManager.D2xxException.html&&...........\...\...\....\....\D2xxManager.DriverParameters.html&&...........\...\...\....\....\D2xxManager.FtDeviceInfoListNode.html&&...........\...\...\....\....\D2xxManager.html&&...........\...\...\....\....\FT_Device.html&&...........\...\...\....\....\FT_EEPROM.html&&...........\...\...\....\....\FT_EEPROM_2232D.html&&...........\...\...\....\....\FT_EEPROM_2232H.DRIVE_STRENGTH.html&&...........\...\...\....\....\FT_EEPROM_2232H.html&&...........\...\...\....\....\FT_EEPROM_232H.CBUS.html&&...........\...\...\....\....\FT_EEPROM_232H.DRIVE_STRENGTH.html&&...........\...\...\....\....\FT_EEPROM_232H.html&&...........\...\...\....\....\FT_EEPROM_232R.CBUS.html&&...........\...\...\....\....\FT_EEPROM_232R.html&&...........\...\...\....\....\FT_EEPROM_245R.CBUS.html&&...........\...\...\....\....\FT_EEPROM_245R.html&&...........\...\...\....\....\FT_EEPROM_4232H.DRIVE_STRENGTH.html&&...........\...\...\....\....\FT_EEPROM_4232H.html&&...........\...\...\....\....\FT_EEPROM_X_Series.CBUS.html&&...........\...\...\....\....\FT_EEPROM_X_Series.DRIVE_STRENGTH.html&&...........\...\...\....\....\FT_EEPROM_X_Series.html&&...........\...\...\....\....\package-frame.html&&...........\...\...\....\....\package-summary.html&&...........\...\...\....\....\package-tree.html&&...........\...\...\....\....\package-use.html&&...........\...\constant-values.html&&...........\...\deprecated-list.html&&...........\...\help-doc.html&&...........\...\index-files&&...........\...\...........\index-1.html&&...........\...\...........\index-10.html&&...........\...\...........\index-11.html&&...........\...\...........\index-12.html&&...........\...\...........\index-13.html&&...........\...\...........\index-14.html&&...........\...\...........\index-15.html&&...........\...\...........\index-16.html&&...........\...\...........\index-17.html&&...........\...\...........\index-18.html&&...........\...\...........\index-19.html&&...........\...\...........\index-2.html&&...........\...\...........\index-3.html&&...........\...\...........\index-4.html&&...........\...\...........\index-5.html&&...........\...\...........\index-6.html&&...........\...\...........\index-7.html&&...........\...\...........\index-8.html&&...........\...\...........\index-9.html&&...........\...\index.html&&...........\...\overview-tree.html&&...........\...\package-list&&...........\...\resources&&...........\...\.........\background.gif&&...........\...\.........\tab.gif&&...........\...\.........\titlebar.gif&&...........\...\.........\titlebar_end.gif&&...........\...\serialized-form.html&&...........\...\stylesheet.css&&...........\ic_launcher-web.png&&...........\libs&&...........\....\android-support-v4.jar&&...........\....\armeabi&&...........\....\d2xx.jar&&...........\lint.xml&&...........\res
::相关软件::
没有相关资源
::下载说明::
*&为了达到最快的下载速度,推荐使用网际快车下载本站软件。 *&如果您发现该软件不能下载,请通知或点击【】,谢谢! *&未经本站明确许可,任何网站不得非法盗链及抄袭本站资源;如引用页面,请注明来自本站,谢谢您的支持!
&&&&&&网友评论:(评论内容只代表网友观点,与本站立场无关!)}

我要回帖

更多关于 计算机硬件系统 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信