转自 http://blog.csdn.net/conanyang/article/details/7505934


友善之臂的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 SerialPortActivityextends Activity
  20. {
  21. private staticfinal String[] serial_port={"/dev/s3c2410_serial0","/dev/s3c2410_serial1","/dev/s3c2410_serial2"};
  22. private staticfinal 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 ClickEventimplements 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 = newString(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>
转自 :http://blog.chinaunix.net/uid-26877131-id-3173111.html

更多相关文章

  1. Handler: Service中使用Toast
  2. android 邮件开发(javax.mail)
  3. Android中的消息机制-源码分析
  4. 经典Android试题及答案
  5. Android(安卓)Studio的基本控件 图片框与进度条
  6. Android中Window的管理深入讲解
  7. Android设计模式系列(9)--SDK源码之适配器模式
  8. Android(安卓)源码系列之从源码的角度深入理解IntentService及Ha
  9. 【译】Android(安卓)Bluetooth

随机推荐

  1. 一道简约而不简单的算法题--数据流的中位
  2. GitHub 热门:Python 算法大全,Star 超过 2
  3. GitHub 热门:别再用 print 输出来调试代码
  4. 真香!GitHub 核心功能都免费开放了
  5. 一网打尽!二分查找解题模版与题型全面解析
  6. 数据结构与算法——最小生成树
  7. 钻漏洞,他用爬虫非法获取 1500 万余条个人
  8. 有效解决U盘文件变成exe格式的方法
  9. 几道和「黑洞照片」那种海量数据有关的算
  10. 2020 年 Python 开发者调查报告:PyCharm