一、库的引用
前言
Android设备上使用串口谷歌在github上已经提供了例子,里面有jni的代码和串口api,具体工程地址如下:https://github.com/cepr/android-serialport-api ,官方给的sdk是eclipse工程,因此我们不能直接使用,我们需要把里面的SerialPort.c和SerialPort.h复制出来,放到我们Android Studio工程中重新编译和使用即可。
1、建工程要勾选Include C++ support
2、把SerialPort.c和SerialPort.h复制到cpp目录下,然后修改CMakeLists.txt文件给自己的.so库起个名字,在Java文件中可以通过它引用此库
3、把github上官方给的工程的这两个文件复制到我们项目中的android.serialport.api包下:
4、把SerialPort.c文件中的open和close方法名字改成:
JNIEXPORT jobject JNICALL Java_android_serialport_api_SerialPort_open(JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
JNIEXPORT void JNICALL Java_android_serialport_api_SerialPort_close(JNIEnv *env, jobject thiz)
5、最后把这两个文件中引用的库名改成刚才自己在CMakeLists.txt中起的名字即可,经过这四步库就成功移植到我们工程中了,这时候可以正常使用串口通信了。
二、串口通信
1、首先我们先看一下SerialPort.java中官方封装的几个方法:
/**
* Google官方代码
*
* 此类的作用为,JNI的调用,用来加载.so文件的
*
* 获取串口输入输出流
*/
public class SerialPort {
private static final String TAG = "SerialPort";
/*
* Do not remove or rename the field mFd: it is used
* close();
*/
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate, int flags)
throws SecurityException, IOException {
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
System.out.println(device.getAbsolutePath() + "==============================");
//开启串口,传入物理地址、波特率、flags值
mFd = open(device.getAbsolutePath(), baudrate, flags);
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
//获取串口的输入流
public InputStream getInputStream() {
return mFileInputStream;
}
//获取串口的输出流
public OutputStream getOutputStream() {
return mFileOutputStream;
}
// JNI调用,开启串口
private native static FileDescriptor open(String path, int baudrate, int flags);
//关闭串口
public native void close();
static {
System.out.println("==============================");
//加载库文件.so文件
System.loadLibrary("serial_port");
System.out.println("********************************");
}
}
通过上面官方代码可以看出提供了4个方法:
(1)获取串口输入流
public InputStream getInputStream();
(2)获取串口输出流
public OutputStream getOutputStream();
(3)打开串口
public native static FileDescriptor open(String path, int baudrate, int flags);
此方法有3个参数,第一个参数path硬件工程师会告诉你,例如:ttyS0、ttyS1,由于串口都是设备,因此他们都放在dev目录下
第二个参数是波特率,它是衡量通信速度的参数,指串口每秒可以传送bit的个数,如果不知道的话可以问问硬件工程师具体填多少
第三个参数就填0号了
(4)关闭串口
public native void close();
得到输入输出流之后我们就可以读写数据了,具体读写什么数据,那就要与硬件那边协定了
读:
// DataUtils是格式的转换,读出来是字节数组的格式,你自己可以转化为字符串
byte[] readData = new byte[1024];
int size = inputStream.read(readData);
String readString = DataUtils.ByteArrToHex(readData, 0, size);
写:
//示例代码,不能直接运行,我只是讲一下我的思路
byte[] sendData = DataUtils.HexToByteArr(data);
outputStream.write(sendData);
outputStream.flush();
2、我们再来看看SerialPortFinder.java
public class SerialPortFinder {
public class Driver {
public Driver(String name, String root) {
mDriverName = name;
mDeviceRoot = root;
}
private String mDriverName;
private String mDeviceRoot;
Vector mDevices = null;
public Vector getDevices() {
if (mDevices == null) {
mDevices = new Vector();
File dev = new File("/dev");
File[] files = dev.listFiles();
int i;
for (i=0; i mDrivers = null;
Vector getDrivers() throws IOException {
if (mDrivers == null) {
mDrivers = new Vector();
LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
String l;
while((l = r.readLine()) != null) {
// Issue 3:
// Since driver name may contain spaces, we do not extract driver name with split()
String drivername = l.substring(0, 0x15).trim();
String[] w = l.split(" +");
if ((w.length >= 5) && (w[w.length-1].equals("serial"))) {
Log.d(TAG, "Found new driver " + drivername + " on " + w[w.length-4]);
mDrivers.add(new Driver(drivername, w[w.length-4]));
}
}
r.close();
}
return mDrivers;
}
public String[] getAllDevices() {
Vector devices = new Vector();
// Parse each driver
Iterator itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getName();
String value = String.format("%s (%s)", device, driver.getName());
devices.add(value);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}
public String[] getAllDevicesPath() {
Vector devices = new Vector();
// Parse each driver
Iterator itdriv;
try {
itdriv = getDrivers().iterator();
while(itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator itdev = driver.getDevices().iterator();
while(itdev.hasNext()) {
String device = itdev.next().getAbsolutePath();
devices.add(device);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devices.toArray(new String[devices.size()]);
}
}
他提供了3个方法:
(1)Vector
读取 /proc/tty/drivers 里面的带有serial,也就是标识串口的地址,然后保存在一个集合里面
(2)public String[] getAllDevices();
(3)public String[] getAllDevicesPath();
能够获取所有的串口的具体地址,然后进行选择你需要的物理地址就行了。一般来说的话,串口地址为: /dev/ttyS2、/dev/ttyS1、/dev/ttyS0
3、我们可以封装一个串口的工具类SerialPortUtils.java
public class SerialPortUtil {
private SerialPort serialPort = null;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private ReceiveThread mReceiveThread = null;
private boolean isStart = false;
/**
* 打开串口,接收数据
* 通过串口,接收单片机发送来的数据
*/
public void openSerialPort() {
try {
serialPort = new SerialPort(new File("/dev/ttyS0"), 9600, 0);
//调用对象SerialPort方法,获取串口中"读和写"的数据流
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
isStart = true;
} catch (IOException e) {
e.printStackTrace();
}
getSerialPort();
}
/**
* 关闭串口
* 关闭串口中的输入输出流
*/
public void closeSerialPort() {
Log.i("test", "关闭串口");
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
isStart = false;
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送数据
* 通过串口,发送数据到单片机
*
* @param data 要发送的数据
*/
public void sendSerialPort(String data) {
try {
byte[] sendData = DataUtils.HexToByteArr(data);
outputStream.write(sendData);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getSerialPort() {
if (mReceiveThread == null) {
mReceiveThread = new ReceiveThread();
}
mReceiveThread.start();
}
/**
* 接收串口数据的线程
*/
private class ReceiveThread extends Thread {
@Override
public void run() {
super.run();
while (isStart) {
if (inputStream == null) {
return;
}
byte[] readData = new byte[1024];
try {
int size = inputStream.read(readData);
if (size > 0) {
String readString = DataUtils.ByteArrToHex(readData, 0, size);
EventBus.getDefault().post(readString);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}