Android串口通信(基于Tiny6410平台)

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


友善之臂的Android系统有他们自己编写的一个串口通信程序,网上没有找到他的源代码,而且界面操作不在一个界面,不是很方便,这里我自己写了一个粗糙点的串口通信程序。
同样这里还是调用友善之臂的friendlyarm-hardware.so库文件。
在Android工程文件下面加入com.friendlyarm.androidSDK包,在其下添加HardwareControler.java。下面我把我做的截图发上来。
                                                  Android串口通信(基于Tiny6410平台)_第1张图片
主程序代码:
  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.     
  24.     TextView chooseserialPortView;
  25.     TextView choosebaudRateView;
  26.     TextView commucationView;
  27.     EditText editmsg;
  28.     private Button stopButton;
  29.     private Button sendButton;
  30.     private Spinner choose_serialport;
  31.     private Spinner choose_baudrate;
  32.     private ArrayAdapter<String> serialportAdapter;
  33.     private ArrayAdapter<String> baudrateAdaptera;
  34.     
  35.     private int fd= 0;
  36.     String thread= "readThread";
  37.     String choosed_serial = "/dev/s3c2410_serial2";
  38.     int choosed_buad = 19200;
  39.     byte[] buf=new byte[300];

  40.     /** Called when the activity is first created. */
  41.     @Override
  42.     public void onCreate(Bundle savedInstanceState)
  43.     {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.main);
  46.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
  47.         chooseserialPortView = (TextView)findViewById(R.id.choose_serialPort_text);
  48.         choose_serialport = (Spinner)findViewById(R.id.choose_seriaPort_spinner);
  49.         chooseserialPortView = (TextView)findViewById(R.id.choose_baudRate_text);
  50.         choose_baudrate = (Spinner)findViewById(R.id.choose_baudRate_spinner);
  51.                 
  52.         serialportAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,serial_port);//建立下拉控件的适配器
  53.         baudrateAdaptera = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,baud_rate);
  54.         serialportAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
  55.         baudrateAdaptera.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
  56.         choose_serialport.setAdapter(serialportAdapter);//连接控件和适配器
  57.         choose_baudrate.setAdapter(baudrateAdaptera);
  58.         choose_serialport.setSelection(2);
  59.         choose_baudrate.setSelection(2);
  60.         
  61.         choose_serialport.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
  62.         {
  63.             
  64.             public void onItemSelected(AdapterView<?> arg0,View arg1,
  65.                     int arg2,long arg3) {
  66.                 // TODO Auto-generated method stub
  67.                 choosed_serial = serial_port[arg2];
  68.             }

  69.             public void onNothingSelected(AdapterView<?> arg0){
  70.                 // TODO Auto-generated method stub
  71.                 
  72.             }
  73.             
  74.         });
  75.             
  76.         choose_baudrate.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
  77.         {
  78.             
  79.             public void onItemSelected(AdapterView<?> arg0,View arg1,
  80.                     int arg2,long arg3) {
  81.                 // TODO Auto-generated method stub
  82.                 choosed_buad = Integer.parseInt(baud_rate[arg2]);
  83.             }

  84.             public void onNothingSelected(AdapterView<?> arg0){
  85.                 // TODO Auto-generated method stub
  86.                 
  87.             }
  88.             
  89.         });
  90.         fd = HardwareControler.openSerialPort(choosed_serial,choosed_buad, 8, 1);//打开串口
  91.         if (fd!= -1) {
  92.             Toast.makeText(getApplicationContext(),getResources().getString(R.string.open_serial_success)+choosed_serial, 1).show();
  93.         } else{
  94.             Toast.makeText(this,getResources().getString(R.string.open_fail), 1).show();
  95.         }
  96.         stopButton = (Button)findViewById(R.id.stopButton);
  97.         stopButton.setOnClickListener(new ClickEvent());
  98.         
  99.         sendButton = (Button)findViewById(R.id.sendButton);//发送消息
  100.         sendButton.setOnClickListener(new OnClickListener(){
  101.             
  102.             public void onClick(View arg0){
  103.                 // TODO Auto-generated method stub
  104.                 HardwareControler.write(fd, editmsg.getText().toString().getBytes());
  105.                 commucationView.append(editmsg.getText()+"\n");
  106.             }
  107.         });

  108.         commucationView = (TextView)findViewById(R.id.commucation_window);
  109.         commucationView.setMovementMethod(ScrollingMovementMethod.getInstance());//让textview实现滚动
  110.         editmsg = (EditText)findViewById(R.id.editmsg);
  111.         
  112.         new readThread().start();//开始串口的监听线程
  113.         
  114.     }
  115.     
  116.     public class ClickEventimplements Button.OnClickListener//退出
  117.     {
  118.         public void onClick(View arg0){
  119.             // TODO Auto-generated method stub
  120.             android.os.Process.killProcess(android.os.Process.myPid());
  121.             System.exit(0);
  122.         }
  123.     }
  124.     Handler handler= new Handler(){
  125.         public void handleMessage(Message msg){
  126.             switch (msg.arg1){
  127.             case 0:
  128.                 int len = HardwareControler.read(fd, buf, 300);    
  129.                 String string = newString(buf, 0, len);
  130.                 commucationView.append(string+"\n");
  131.                 new readThread().start();//处理完消息后立即开启监听线程
  132.                 Log.d(thread,"接收到数据,新线程启动");
  133.                 break;
  134.             case 1:
  135.                 HardwareControler.setLedState(1, 0);
  136.                 new readThread().start();
  137. //                Log.d(thread,"没有数据,新线程启动");
  138.                 break;
  139.             default:
  140.                 break;
  141.             }
  142.         }
  143.     };
  144.     
  145.     class readThread extends Thread//读取串口信息线程
  146.     {        
  147.         public void run()
  148.         {
  149.             Message msg = new Message();
  150.             HardwareControler.setLedState(0, 0);
  151.             if (HardwareControler.select(fd,5, 0)==1){            
  152.                 msg.arg1 = 0;            
  153.             }
  154.             else {
  155.                 msg.arg1 =1;
  156.                 HardwareControler.setLedState(0, 1);
  157.             }
  158.             handler.sendMessage(msg);
  159.         }
  160.     }
  161. }
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

你可能感兴趣的:(thread,android,String,layout,dropdown,button)