友善之臂的Android系统有他们自己编写的一个串口通信程序,网上没有找到他的源代码,而且界面操作不在一个界面,不是很方便,这里我自己写了一个粗糙点的串口通信程序。

同样这里还是调用友善之臂的friendlyarm-hardware.so库文件。
在Android工程文件下面加入com.friendlyarm.androidSDK包,在其下添加HardwareControler.java。下面我把我做的截图发上来。
                                                 
主程序代码:
  1. package geekle.lab;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.text.method.ScrollingMovementMethod;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.WindowManager;
  11. import android.widget.AdapterView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.Spinner;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import com.friendlyarm.AndroidSDK.HardwareControler;
  19. public class SerialPortActivity extends Activity
  20. {
  21. private static final String[] serial_port={"/dev/s3c2410_serial0","/dev/s3c2410_serial1","/dev/s3c2410_serial2"};
  22. private static final String[] baud_rate={"4800","9600","19200","115200"};
  23. TextView chooseserialPortView;
  24. TextView choosebaudRateView;
  25. TextView commucationView;
  26. EditText editmsg;
  27. private Button stopButton;
  28. private Button sendButton;
  29. private Spinner choose_serialport;
  30. private Spinner choose_baudrate;
  31. private ArrayAdapter<String> serialportAdapter;
  32. private ArrayAdapter<String> baudrateAdaptera;
  33. private int fd = 0;
  34. String thread = "readThread";
  35. String choosed_serial = "/dev/s3c2410_serial2";
  36. int choosed_buad = 19200;
  37. byte[] buf= new byte[300];
  38. /** Called when the activity is first created. */
  39. @Override
  40. public void onCreate(Bundle savedInstanceState)
  41. {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.main);
  44. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
  45. chooseserialPortView = (TextView)findViewById(R.id.choose_serialPort_text);
  46. choose_serialport = (Spinner)findViewById(R.id.choose_seriaPort_spinner);
  47. chooseserialPortView = (TextView)findViewById(R.id.choose_baudRate_text);
  48. choose_baudrate = (Spinner)findViewById(R.id.choose_baudRate_spinner);
  49. serialportAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,serial_port);//建立下拉控件的适配器
  50. baudrateAdaptera = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,baud_rate);
  51. serialportAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
  52. baudrateAdaptera.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
  53. choose_serialport.setAdapter(serialportAdapter);//连接控件和适配器
  54. choose_baudrate.setAdapter(baudrateAdaptera);
  55. choose_serialport.setSelection(2);
  56. choose_baudrate.setSelection(2);
  57. choose_serialport.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
  58. {
  59. public void onItemSelected(AdapterView<?> arg0, View arg1,
  60. int arg2, long arg3) {
  61. // TODO Auto-generated method stub
  62. choosed_serial = serial_port[arg2];
  63. }
  64. public void onNothingSelected(AdapterView<?> arg0) {
  65. // TODO Auto-generated method stub
  66. }
  67. });
  68. choose_baudrate.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
  69. {
  70. public void onItemSelected(AdapterView<?> arg0, View arg1,
  71. int arg2, long arg3) {
  72. // TODO Auto-generated method stub
  73. choosed_buad = Integer.parseInt(baud_rate[arg2]);
  74. }
  75. public void onNothingSelected(AdapterView<?> arg0) {
  76. // TODO Auto-generated method stub
  77. }
  78. });
  79. fd = HardwareControler.openSerialPort(choosed_serial,choosed_buad, 8, 1);//打开串口
  80. if (fd != -1) {
  81. Toast.makeText(getApplicationContext(), getResources().getString(R.string.open_serial_success)+choosed_serial, 1).show();
  82. } else {
  83. Toast.makeText(this, getResources().getString(R.string.open_fail), 1).show();
  84. }
  85. stopButton = (Button)findViewById(R.id.stopButton);
  86. stopButton.setOnClickListener(new ClickEvent());
  87. sendButton = (Button)findViewById(R.id.sendButton);//发送消息
  88. sendButton.setOnClickListener(new OnClickListener() {
  89. public void onClick(View arg0) {
  90. // TODO Auto-generated method stub
  91. HardwareControler.write(fd, editmsg.getText().toString().getBytes());
  92. commucationView.append(editmsg.getText()+"\n");
  93. }
  94. });
  95. commucationView = (TextView)findViewById(R.id.commucation_window);
  96. commucationView.setMovementMethod(ScrollingMovementMethod.getInstance()); //让textview实现滚动
  97. editmsg = (EditText)findViewById(R.id.editmsg);
  98. new readThread().start();//开始串口的监听线程
  99. }
  100. public class ClickEvent implements Button.OnClickListener//退出
  101. {
  102. public void onClick(View arg0) {
  103. // TODO Auto-generated method stub
  104. android.os.Process.killProcess(android.os.Process.myPid());
  105. System.exit(0);
  106. }
  107. }
  108. Handler handler = new Handler() {
  109. public void handleMessage(Message msg) {
  110. switch (msg.arg1) {
  111. case 0:
  112. int len = HardwareControler.read(fd, buf, 300);
  113. String string = new String(buf, 0, len);
  114. commucationView.append(string+"\n");
  115. new readThread().start();//处理完消息后立即开启监听线程
  116. Log.d(thread,"接收到数据,新线程启动");
  117. break;
  118. case 1:
  119. HardwareControler.setLedState(1, 0);
  120. new readThread().start();
  121. //                Log.d(thread,"没有数据,新线程启动");
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. };
  128. class readThread extends Thread//读取串口信息线程
  129. {
  130. public void run()
  131. {
  132. Message msg = new Message();
  133. HardwareControler.setLedState(0, 0);
  134. if (HardwareControler.select(fd,5, 0)==1) {
  135. msg.arg1 = 0;
  136. }
  137. else {
  138. msg.arg1 =1;
  139. HardwareControler.setLedState(0, 1);
  140. }
  141. handler.sendMessage(msg);
  142. }
  143. }
  144. }
main.xml代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/choose_serialPort_text"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@+string/chooseserialPort" />
  11. <Spinner
  12. android:id="@+id/choose_seriaPort_spinner"
  13. android:layout_width="wrap_content"
  14. android:layout_height="40dp" >
  15. </Spinner>
  16. <TextView
  17. android:id="@+id/choose_baudRate_text"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="@+string/choosebaudRate" />
  21. <Spinner
  22. android:id="@+id/choose_baudRate_spinner"
  23. android:layout_width="wrap_content"
  24. android:layout_height="40dp" >
  25. </Spinner>
  26. <TextView
  27. android:id="@+id/commucation_window"
  28. android:layout_width="fill_parent"
  29. android:layout_height="190dp" >
  30. </TextView>
  31. <EditText
  32. android:id="@+id/editmsg"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:hint="edit here" />
  36. <LinearLayout
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. android:layout_weight="1"
  40. android:orientation="horizontal" >
  41. <Button
  42. android:id="@+id/sendButton"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_weight="1"
  46. android:text="@+string/send" />
  47. <Button
  48. android:id="@+id/stopButton"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_weight="1"
  52. android:text="@string/stopButton" />
  53. </LinearLayout>
  54. </LinearLayout>
 
 

Android串口通信(基于Tiny6410平台)的更多相关文章

  1. Android串口通信

    前段时间因为工作需要研究了一下android的串口通信,网上有很多讲串口通信的文章,我在做的时候也参考了很多文章,现在就将我学习过程中的一些心得分享给大家,希望可以帮助大家在学习的时候少走一些弯路,有 ...

  2. Java串口通信--------基于RXTX (附带资源地址)

    最近帮老师做了一个小项目,一个牧场公司想用传感器收集一些环境信息,记录到数据库里去,然后加以分析查看.这里面和传感器通信用到了串口通信,我也是接触了一下,把用到的东西分享出来. 准备工作: RXTX: ...

  3. Android串口通信(Android Studio)

    gilhub上已有开源项目: https://github.com/cepr/android-serialport-api 可以直接使用

  4. Android串口开发

    参考资料: https://www.jianshu.com/p/9249ed03e745 GitHUb地址: https://github.com/AIlll/AndroidSerialPort An ...

  5. 基于FPGA的红外遥控解码与PC串口通信

    基于FPGA的红外遥控解码与PC串口通信 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<电子设计EDA>的课程设计作业(呵呵,这个月都拿 ...

  6. 【毕业设计】基于Android的家校互动平台开发(内含完整代码和所有文档)——爱吖校推(你关注的,我们才推)

    ☆ 写在前面 之前答应大家的毕业答辩之后把所有文档贡献出来,现在答辩已过,LZ信守承诺,把所有文档开源到了GitHub(这个地址包含所有的代码和文档以及PPT,外层为简单的代码).还望喜欢的朋友们,不 ...

  7. Android 串口蓝牙通信开发Java版本

    Android串口BLE蓝牙通信Java版 0. 导语 Qt on Android 蓝牙通信开发 我们都知道,在物联网中,BLE蓝牙是通信设备的关键设备.在传统的物联网应用中,无线WIFI.蓝牙和Zi ...

  8. VS2008基于对话框的MFC上位机串口通信(C++实现)简单例程

    首先,在 vs2008 环境下创建 MFC 运用程序 设置项目名称为 ComTest(这个地方随意命名,根据个人习惯),点击确定后,点击下一步 出现如下界面 选择"基于对话框"模式 ...

  9. android 串口开发第二篇:利用jni实现android和串口通信

    一:串口通信简介 由于串口开发涉及到jni,所以开发环境需要支持ndk开发,如果未配置ndk配置的朋友,或者对jni不熟悉的朋友,请查看上一篇文章,android 串口开发第一篇:搭建ndk开发环境以 ...

随机推荐

  1. 7款经典炫酷的HTML5/jQuery动画应用示例及源码

    jQuery是一款普遍受前端开发者欢迎的Javascript框架,但是开发者貌似更关注jQuery开发的插件,海量的jQuery插件让前端开发者非常方便.HTML5的加入让jQuery这个家族更加丰富 ...

  2. Http和Socket连接区别

    相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过自己的浅显理解能对初学者有所帮助. 1.TCP连接 要想明白Socket连接,先要明白TCP连接.手机能够使用联 ...

  3. MTD技术介绍

    MTD(Memory Technology device)是用于访问memory设备(ROM.Flash)的Linux子系统,在Linux中引入这一层的主要目的是为了更加简单的添加新的Memory存储 ...

  4. centos系统下安装使用composer教程

    Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 "packages" ...

  5. 下拉刷新--第三方开源--PullToRefresh

    效果预览图: 下载地址:https://github.com/chrisbanes/Android-PullToRefresh activity_main.xml: <RelativeLayou ...

  6. Ant之build.xml

    转载地址:http://www.cnblogs.com/clarkchen/archive/2011/03/10/1980194.html http://lisongqiu168.iteye.com/ ...

  7. Spark菜鸟学习营Day5 分布式程序开发

    Spark菜鸟学习营Day5 分布式程序开发 这一章会和我们前面进行的需求分析进行呼应,完成程序的开发. 开发步骤 分布式系统开发是一个复杂的过程,对于复杂过程,我们需要分解为简单步骤的组合. 针对每 ...

  8. 11g RAC R2 体系结构---Grid

    基于agent的管理方式 从oracle 11.2开始出现了多用户的概念,oracle开始使用一组多线程的daemon来同时支持多个用户的使用.管理资源,这些daemon叫做Agent.这些Agent ...

  9. 使用Sass优雅并高效的实现CSS中的垂直水平居中(附带Flex布局,CSS3+SASS完美版)

    实现css水平垂直居中的方法有很多,在这里我简单的说下四种比较常用的方法: 1.使用CSS3中的Flex布局 对于flex,我们要了解的是它是一个display的属性,而且必须要给他的父元素设置fle ...

  10. springMVC之事务配置(问题来源:为什么数据保存不了)

    参考文章:http://www.cnblogs.com/leiOOlei/p/3725911.html 自己的亲身体会,来源问题this.sessionFactory.getCurrentSessio ...