在编写读写串口的代码之前有这样的条件:
1.Tiny210搭载Android2.3.1系统;
2.友善之臂的非开源的libfriendlyarm-hardware.so文件已导入工程(见用户手册,这里不再雷述);
在tiny210的用户手册中提供了这些方法的API,但是对于新接触这块的朋友来说,能够顺利的写出串口的开/关、读数据、写数据也是需要一点时间去详细查资料的,这里,我用了差不多一上午的时间在板子上写了这样一个android程序,是可行的(读数据因PC端发送数据目前存在技术问题,但读取null数据测试是通过的).
废话不多说了,首先看一下效果:
下面就是主要的代码:
//需要说明的主要有两点:
// 1.android线程与java的有一些区别,run方法要调用handler才能操作activity,这个与
Android中主、子线程的职责有关,详细的请查看一下它们的机制; //这些是半年前弄得了,那时候还非常不了解这些机制,愚笨处见谅。
2.bf即调用open方法时返回的文件描述符,在调用read、write、close时的第一个参数即是这个串口的描述.
Java代码:
package cn.xjtuse.joim.activity;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.friendlyarm.AndroidSDK.HardwareControler;
public class MainActicity extends Activity {
/**
* 类名:cn.xjtuse.joim.activity.MainActicity
* 描述: android上串口的通信Demo;;
* 编写者:JOIM
* 创建时间:2012.10 13:40
* 修改说明:
*
* */
/** Called when the activity is first created. */
private static int fd = 0;
private byte[] serialReceiveBuf = new byte[100];
private static boolean readPortInfo = false;
TextView tvTime;
Button btnOpen, btnClose, btnRead, btnWrite, btnOenPWM, btnClosePWM;
int state = 0;
Handler sendHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnOpen = (Button) findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(listener);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(listener);
btnClose.setEnabled(false);
btnRead = (Button) findViewById(R.id.btnRead);
btnRead.setBackgroundColor(Color.RED);
btnRead.setOnClickListener(listener);
btnWrite = (Button) findViewById(R.id.btnWrite);
btnWrite.setOnClickListener(listener);
btnOenPWM = (Button) findViewById(R.id.btnOpenPWM);
btnOenPWM.setOnClickListener(listener);
btnClosePWM = (Button) findViewById(R.id.btnClosePWM);
btnClosePWM.setOnClickListener(listener);
tvTime = (TextView) findViewById(R.id.tvTime);
Date nowDate = new Date();
tvTime.setText(nowDate.toString());
new Thread(timerefreshThread).start();
/*
* 打开一个串口启动线程轮询串口设备,判断是否有数据到来
*
* 调用read接口读取数据;调用write接口向串口中写入和数据;
*
* 串口使用完毕后,调用close来关闭串口
*/
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Button btn = (Button) arg0;
switch (btn.getId()) {
case R.id.btnOpen:
if (state == 1) {
Toast.makeText(MainActicity.this, "串口已打开,不必重复打开.",
Toast.LENGTH_LONG).show();
}
while (state == 0) {
// 打开/dev/s3c2410_serial1端口:
fd = HardwareControler.openSerialPort(
"/dev/s3c2410_serial1", 115200, 8, 1);
if (fd != -1) {
state = 1;
Toast.makeText(MainActicity.this, "串口已打开",
Toast.LENGTH_LONG).show();
// lacHandler.post(receivePostDataRunnable);
} else {
;
}
}
btnClose.setEnabled(true);
btnOpen.setEnabled(false);
break;
case R.id.btnClose:
HardwareControler.close(fd);
state = 0;
Toast.makeText(MainActicity.this, "串口已关闭!", Toast.LENGTH_LONG)
.show();
btnClose.setEnabled(false);
btnOpen.setEnabled(true);
break;
case R.id.btnRead:
// 读取端口中发出来的数据
if (readPortInfo == false) {
// 开启接收线程监听。
btnRead.setBackgroundColor(Color.GREEN);
new Thread(ReadSerialPortThread).start();
} else {
btnRead.setBackgroundColor(Color.RED);
}
readPortInfo = !readPortInfo;
break;
case R.id.btnWrite:
// 向端口中写入数据.
new Thread(writeToSerialPortThread).start();
break;
case R.id.btnOpenPWM:
HardwareControler.PWMPlay(DEFAULT_KEYS_DIALER);
break;
case R.id.btnClosePWM:
HardwareControler.PWMStop();
break;
}
}
};
private Thread ReadSerialPortThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (readPortInfo) {
Message message = new Message();
readFromSerialPortHandler.sendMessage(message);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
private Handler readFromSerialPortHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] b = new byte[256];
if (HardwareControler.select(fd, 5, 0) == 1) {
HardwareControler.read(fd, b, b.length);
String readFromSerialPort = new String(b, 0, b.length);
Toast.makeText(MainActicity.this, "读到数据:" + readFromSerialPort,
Toast.LENGTH_LONG).show();
} else {
}
}
};
private Thread writeToSerialPortThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
writeToSerialPortHandler.sendMessage(message);
}
});
private Handler writeToSerialPortHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String writeToSerialPort = "Hello,World!";
byte[] b = new byte[1024];
try {
b = writeToSerialPort.getBytes("gbk");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.write(fd, b) != -1) {
Toast.makeText(MainActicity.this, "写入成功", Toast.LENGTH_LONG)
.show();
// HardwareControler.close(1);
} else {
Toast.makeText(MainActicity.this, "写入失败", Toast.LENGTH_LONG)
.show();
}
}
};
/*
* 时间刷新的线程.
*/
private Thread timerefreshThread = new Thread(new Runnable() {
@Override
public void run() {
Date nowDate = new Date();
tvTime.setText(nowDate.toString());
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}
});
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
Date nowDate = new Date();
tvTime.setText(nowDate.toString());
break;
default:
break;
}
}
};
Handler lacHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
Runnable receivePostDataRunnable = new Runnable() {
int err = 0;
String s;
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
err = HardwareControler.select(fd, 1, 0);
if (err == 1)// 有数据可读
{
HardwareControler.read(fd, serialReceiveBuf, 10);
s = new String(serialReceiveBuf);
System.out.println("接收到串口数据" + s);
} else if (err == 0) // 无数据可读
{
System.out.println("没有接收到串口数据");
} else // 出错
{
System.out.println("接收到串口数据出错");
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
} new Thread(writeToSerialPortThread).start();
break;
case R.id.btnOpenPWM:
HardwareControler.PWMPlay(DEFAULT_KEYS_DIALER);
break;
case R.id.btnClosePWM:
HardwareControler.PWMStop();
break;
}
}
};
private Thread ReadSerialPortThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (readPortInfo) {
Message message = new Message();
readFromSerialPortHandler.sendMessage(message);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
private Handler readFromSerialPortHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] b = new byte[256];
if (HardwareControler.select(fd, 5, 0) == 1) {
HardwareControler.read(fd, b, b.length);
String readFromSerialPort = new String(b, 0, b.length);
Toast.makeText(MainActicity.this, "读到数据:" + readFromSerialPort,
Toast.LENGTH_LONG).show();
} else {
}
}
};
private Thread writeToSerialPortThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
writeToSerialPortHandler.sendMessage(message);
}
});
private Handler writeToSerialPortHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String writeToSerialPort = "Hello,World!";
byte[] b = new byte[1024];
try {
b = writeToSerialPort.getBytes("gbk");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.write(fd, b) != -1) {
Toast.makeText(MainActicity.this, "写入成功", Toast.LENGTH_LONG)
.show();
// HardwareControler.close(1);
} else {
Toast.makeText(MainActicity.this, "写入失败", Toast.LENGTH_LONG)
.show();
}
}
};
/*
* 时间刷新的线程.
*/
private Thread timerefreshThread = new Thread(new Runnable() {
@Override
public void run() {
Date nowDate = new Date();
tvTime.setText(nowDate.toString());
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}
});
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
Date nowDate = new Date();
tvTime.setText(nowDate.toString());
break;
default:
break;
}
}
};
Handler lacHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
Runnable receivePostDataRunnable = new Runnable() {
int err = 0;
String s;
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
err = HardwareControler.select(fd, 1, 0);
if (err == 1)// 有数据可读
{
HardwareControler.read(fd, serialReceiveBuf, 10);
s = new String(serialReceiveBuf);
System.out.println("接收到串口数据" + s);
} else if (err == 0) // 无数据可读
{
System.out.println("没有接收到串口数据");
} else // 出错
{
System.out.println("接收到串口数据出错");
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
}
工程文件下载链接在: http://pan.baidu.com/share/link?shareid=228735&uk=3356814091 ,需要的朋友可以去下。