友善之臂的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. DailyWallpaper v1.02 released

    上次忘了写软件说明,先补上一个. 软件说明: 每天定时(暂定上午11点)下载美国国家地理网站的photo of the day图片作为桌面壁纸.下载图片会以日期为名称保存在C:\DailyWallpa ...

  2. curl raise 信号出core

    在使用c++多线程使用libcurl抓取网页时,遇到程序随机core掉的情况,gdb 一下出错信息有这么一条:longjmp causes uninitialized stack frame. 在网上 ...

  3. windowsphone 瀑布流&ui虚拟化

    瀑布流已经有点年代了吧,不过wp上还真是挺少资料的.今天抽空把自己之前搞过的东西写出来,避免大家重复劳动. 一.简单的瀑布流排版加入ui虚拟化. 最近看了 段博琼  ui虚拟化的一篇博文,链接:htt ...

  4. 【风马一族_Java】在某个范围内,找出具有水仙花特征的数字

    打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如: 153是一个"水仙花数",因为153=1的三 ...

  5. 惠普M1005打印机无法自动进纸的问题

    惠普M1005打印机无法自动进纸的问题 问题起因 其实我也不太清楚是什么起因,前一天用的好好的惠普M1005打印机,在打印时没有直接打印,会弹出一个提示对话框,同时打印机显示屏上显示“load tra ...

  6. mysql数据库表格导出为excel表格

    在本地数据库中操作如下: 由于excel表格的编码是GBK,所以导出时要加一个设置字符编码: select * from 某个表 into outfile 'd:/文件名.xls' CHARACTER ...

  7. C# 发邮件类可发送附件

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...

  8. MVC 5.0(or5.0↓) Ajax.BeginForm 异步上传附件问题,答案是不能的!

    MVC 5.0(or5.0↓)  Ajax.BeginForm 异步上传附件问题,答案是不能的! (请注意我这里说的异步!) 来看一下下面这段一步提交file的代码 //前台 .cshtml 文件 & ...

  9. 删除或清空具有外键约束的表数据报-ERROR 1701 (42000)

    OS:  centos 6.3 DB:5.5.14 mysql> select database();+------------+| database() |+------------+| sa ...

  10. Linux磁盘与文件系统管理

    df df(disk free) 功能说明:显示磁盘的相关信息.语 法:df [-ahHiklmPT][--block-size=<区块大小>][-t <文件系统类型>][-x ...