以下程式用於DMA-6410L 開發平台,其步驟如下:
1. 新增新的activity元件。
2. 修改activityMain.java,更換執行元件。
3. 新增AIDL檔案。
4. 新增COM Service程式。
5. 修改activity程式。
6. 修改Manifest檔案。
7. 安裝及測試
UartIPCactivity.java程式列表:
package tw.com.dmatek.dma6410xp.ipc;
import java.util.Timer;
import java.util.TimerTask;
import tw.com.dmatek.dma6410xp.R;
import tw.com.dmatek.dma6410xp.uart.Linuxc;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class UartIPCactivity extends Activity {
TextView view1,view2,view;
Spinner spinner1;
String receiveMsg;
String Msg,str="liwei",ReStr;
EditText editmsg;
Button myButton0,myButton1,myButton2,myButton3,myButton4;
public int num=1;
public int fd,set,i=-1,ss=0;
int p;
private static final String[] countriesStr =
{ "B1200", "B2400", "B4800", "B9600","B19200","B38400","B57600","B115200","B230400","B921600"};
private Spinner mySpinner;
private ArrayAdapteradapter;
private IUartService uartService=null;
private Button.OnClickListener myButton0_listener=new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
if(uartService.openUart(1)>=0)
setTitle("ipc open device sucess!");
else
setTitle("ipc open device false!");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
view1.setText("The current COM1");
}
};
private Button.OnClickListener myButton1_listener=new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if(uartService.openUart(2)>=0)
setTitle("ipc open device sucess!");
else
setTitle("ipc open device false!");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
view1.setText("The current COM2");
}
};
private Button.OnClickListener myButton2_listener=new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view.setText(null);
}
};
private Button.OnClickListener myButton3_listener=new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// timer.cancel();
try {
uartService.closeUart(0);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}
};
private Button.OnClickListener myButton4_listener=new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Msg=editmsg.getText().toString();
try {
uartService.sendMsgUart(Msg);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private ServiceConnection mConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
uartService = IUartService.Stub.asInterface(arg1);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
uartService = null;
}
};
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (uartService!=null) {
try {
ReStr=uartService.receiveMsgUart();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ReStr!=null)
{
view.append(ReStr);
view.append("\n");
ReStr=null;
}
}
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.uart);
timer.schedule(task, 1000, 1000);
setTitle("IPC Testing");
bindService(new Intent(UartIPCactivity.this, UartService.class), mConnection , Context.BIND_AUTO_CREATE);
view1=(TextView)findViewById(R.id.uart_textview);
view=(TextView)findViewById(R.id.uart_view);
editmsg=(EditText)findViewById(R.id.uart_edit);
editmsg.setWidth(200);
mySpinner=(Spinner)findViewById(R.id.uart_mode);
myButton0=(Button)findViewById(R.id.uart_button0);
myButton1=(Button)findViewById(R.id.uart_button1);
myButton2=(Button)findViewById(R.id.uart_button2);
myButton3=(Button)findViewById(R.id.uart_button3);
myButton4=(Button)findViewById(R.id.uart_button4);
myButton0.setOnClickListener(myButton0_listener);
myButton1.setOnClickListener(myButton1_listener);
myButton2.setOnClickListener(myButton2_listener);
myButton3.setOnClickListener(myButton3_listener);
myButton4.setOnClickListener(myButton4_listener);
adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, countriesStr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView arg0, View arg1, int arg2,long arg3)
{
try {
if(uartService!=null) uartService.setUart(arg2);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
arg0.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView arg0)
{
// TODO Auto-generated method stub
}
});
}
}
UartService.java程式列表:
package tw.com.dmatek.dma6410xp.ipc;
import tw.com.dmatek.dma6410xp.uart.Linuxc;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class UartService extends Service {
int fd = -1;
int ss = 3;
private final IUartService.Stub mBinder = new IUartService.Stub() {
@Override
public int setUart(int buad) throws RemoteException {
// TODO Auto-generated method stub
ss=buad;
return Linuxc.setUart(ss);
}
@Override
public int openUart(int port) throws RemoteException {
// TODO Auto-generated method stub
if(fd>=0) Linuxc.closeUart(0);
fd=Linuxc.openUart(port);
if(fd>=0)
{
Linuxc.setUart(ss);
}
return fd;
}
@Override
public void closeUart(int port) throws RemoteException {
// TODO Auto-generated method stub
Linuxc.closeUart(port);
}
@Override
public int sendMsgUart(String message) throws RemoteException {
// TODO Auto-generated method stub
return Linuxc.sendMsgUart(message);
}
@Override
public String receiveMsgUart() throws RemoteException {
// TODO Auto-generated method stub
return Linuxc.receiveMsgUart();
}
};
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
}
IUartService.aidl程式列表如下:
package tw.com.dmatek.dma6410xp.ipc;
interface IUartService{
int openUart(int buad);
void closeUart(int port);
int setUart(int port);
int sendMsgUart(String message);
String receiveMsgUart();
}
沒有留言:
張貼留言