Tiny210搭載Android2.3.1系統,Android程序實現對板子上的串口的讀寫。首先將友善之臂的非開源的libfriendlyarm-hardware.so文件導入工程:在源碼文件夾下新建libs文件夾,在裡面新建armeabi文件夾,拷貝libfriendlyarm-hardware.so到armeabi裡面,然後在eclipse的工程目錄上右擊點Refresh;然後添加HardwareControler.java:新建名為com.friendlyarm.AndroidSDK的Package包,新建HardwareControler.java文件,然後將代碼拷進去;最後寫自己的Android應用程序。注意:路徑不能有一點錯誤,因為友善之臂提供的是靜態庫,所以必須按照他們給定的路徑來。
下面是讀寫串口的程序。
工程目錄為:
Java主程序代碼:
package com.android.zxw;
import java.util.Arrays;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml.Encoding;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.friendlyarm.AndroidSDK.HardwareControler;
public class Zxw2Activity extends Activity {
private TextView fdText;
private EditText senddata;
private EditText data1;
private Button openSerial;
private Button closeSerial;
private Button sendSerial;
private int fd;
Handler handler=new Handler();
Handler sendHandler=new Handler();
Runnable runnable=new Runnable(){
@Override
public void run()
{
ReadSerial();
//handler.postDelayed(this, 3000);
handler.post(this);
}
};
Runnable sendrunnable=new Runnable(){
@Override
public void run()
{
SendSerial();
//handler.postDelayed(this, 3000);
handler.post(this);
}
};
/**
*接收函數
*/
public void ReadSerial(){
//data1.setText(null);
//System.out.println(fd);
int m=HardwareControler.select(fd,2,20);
// System.out.println("a");
//System.out.println(m);
if(m==1)
{
byte[] buf =new byte[10];
try {
Thread.sleep(90);
} catch(InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
int n=HardwareControler.read(fd, buf, buf.length);
// System.out.println("b");
//System.out.println(n);
System.out.println(Arrays.toString(buf));
data1.setText(data1.getText() + " " + Arrays.toString(buf));
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fdText=(TextView) findViewById(R.id.fdText);
senddata=(EditText) findViewById(R.id.data);
data1=(EditText) findViewById(R.id.data1);
openSerial = (Button) findViewById(R.id.openSerial);
closeSerial = (Button) findViewById(R.id.closeSerial);
sendSerial = (Button) findViewById(R.id.sendSerial);
/**
* 軟件運行就直接打開串口
*/
//TODO Auto-generated method stub
fd=HardwareControler.openSerialPort("/dev/s3c2410_serial3",115200,8,1);
fdText.setText(Integer.toString(fd));
handler.post(runnable);
sendHandler.post(sendrunnable);
// System.out.println("啟動串口線程");
fdText.setText(fdText.getText() + "打開線程");
closeSerial.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
handler.removeCallbacks(runnable);
HardwareControler.close(fd);
fdText.setText("關閉串口");
}
});
/**
* 發送按鈕
*/
sendSerial.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
SendSerial();
}
});
}
/**
* 發送函數,直接調用友善之臂提供的函數接口
* 我這裡將(EditText)senddata中的內容變字符串再變bytes[]
* 接收到的結果有點不對
*/
public void SendSerial(){
HardwareControler.write(fd,senddata.gettext().toString().getBytes());
}
}
接收發送正常。