友善之臂的Android系统有他们自己编写的一个串口通信程序,网上没有找到他的源代码,而且界面操作不在一个界面,不是很方便,这里我自己写了一个粗糙点的串口通信程序。
同样这里还是调用友善之臂的friendlyarm-hardware.so库文件。
在Android工程文件下面加入com.friendlyarm.androidSDK包,在其下添加HardwareControler.java。下面我把我做的截图发上来。
主程序代码:
- package geekle.lab;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.text.method.ScrollingMovementMethod;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.WindowManager;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.friendlyarm.AndroidSDK.HardwareControler;
- public class SerialPortActivity extends Activity
- {
- private static final String[] serial_port={"/dev/s3c2410_serial0","/dev/s3c2410_serial1","/dev/s3c2410_serial2"};
- private static final String[] baud_rate={"4800","9600","19200","115200"};
- TextView chooseserialPortView;
- TextView choosebaudRateView;
- TextView commucationView;
- EditText editmsg;
- private Button stopButton;
- private Button sendButton;
- private Spinner choose_serialport;
- private Spinner choose_baudrate;
- private ArrayAdapter<String> serialportAdapter;
- private ArrayAdapter<String> baudrateAdaptera;
- private int fd = 0;
- String thread = "readThread";
- String choosed_serial = "/dev/s3c2410_serial2";
- int choosed_buad = 19200;
- byte[] buf= new byte[300];
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
- chooseserialPortView = (TextView)findViewById(R.id.choose_serialPort_text);
- choose_serialport = (Spinner)findViewById(R.id.choose_seriaPort_spinner);
- chooseserialPortView = (TextView)findViewById(R.id.choose_baudRate_text);
- choose_baudrate = (Spinner)findViewById(R.id.choose_baudRate_spinner);
- serialportAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,serial_port);//建立下拉控件的适配器
- baudrateAdaptera = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,baud_rate);
- serialportAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
- baudrateAdaptera.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
- choose_serialport.setAdapter(serialportAdapter);//连接控件和适配器
- choose_baudrate.setAdapter(baudrateAdaptera);
- choose_serialport.setSelection(2);
- choose_baudrate.setSelection(2);
- choose_serialport.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
- {
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- choosed_serial = serial_port[arg2];
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- choose_baudrate.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
- {
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- choosed_buad = Integer.parseInt(baud_rate[arg2]);
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- fd = HardwareControler.openSerialPort(choosed_serial,choosed_buad, 8, 1);//打开串口
- if (fd != -1) {
- Toast.makeText(getApplicationContext(), getResources().getString(R.string.open_serial_success)+choosed_serial, 1).show();
- } else {
- Toast.makeText(this, getResources().getString(R.string.open_fail), 1).show();
- }
- stopButton = (Button)findViewById(R.id.stopButton);
- stopButton.setOnClickListener(new ClickEvent());
- sendButton = (Button)findViewById(R.id.sendButton);//发送消息
- sendButton.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- HardwareControler.write(fd, editmsg.getText().toString().getBytes());
- commucationView.append(editmsg.getText()+"\n");
- }
- });
- commucationView = (TextView)findViewById(R.id.commucation_window);
- commucationView.setMovementMethod(ScrollingMovementMethod.getInstance()); //让textview实现滚动
- editmsg = (EditText)findViewById(R.id.editmsg);
- new readThread().start();//开始串口的监听线程
- }
- public class ClickEvent implements Button.OnClickListener//退出
- {
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- android.os.Process.killProcess(android.os.Process.myPid());
- System.exit(0);
- }
- }
- Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.arg1) {
- case 0:
- int len = HardwareControler.read(fd, buf, 300);
- String string = new String(buf, 0, len);
- commucationView.append(string+"\n");
- new readThread().start();//处理完消息后立即开启监听线程
- Log.d(thread,"接收到数据,新线程启动");
- break;
- case 1:
- HardwareControler.setLedState(1, 0);
- new readThread().start();
- // Log.d(thread,"没有数据,新线程启动");
- break;
- default:
- break;
- }
- }
- };
- class readThread extends Thread//读取串口信息线程
- {
- public void run()
- {
- Message msg = new Message();
- HardwareControler.setLedState(0, 0);
- if (HardwareControler.select(fd,5, 0)==1) {
- msg.arg1 = 0;
- }
- else {
- msg.arg1 =1;
- HardwareControler.setLedState(0, 1);
- }
- handler.sendMessage(msg);
- }
- }
- }
main.xml代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/choose_serialPort_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@+string/chooseserialPort" />
- <Spinner
- android:id="@+id/choose_seriaPort_spinner"
- android:layout_width="wrap_content"
- android:layout_height="40dp" >
- </Spinner>
- <TextView
- android:id="@+id/choose_baudRate_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@+string/choosebaudRate" />
- <Spinner
- android:id="@+id/choose_baudRate_spinner"
- android:layout_width="wrap_content"
- android:layout_height="40dp" >
- </Spinner>
- <TextView
- android:id="@+id/commucation_window"
- android:layout_width="fill_parent"
- android:layout_height="190dp" >
- </TextView>
- <EditText
- android:id="@+id/editmsg"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="edit here" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/sendButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@+string/send" />
- <Button
- android:id="@+id/stopButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@string/stopButton" />
- </LinearLayout>
- </LinearLayout>
Android串口通信(基于Tiny6410平台)
时间: 2024-11-06 20:23:45