Android串口通信开发例子

Home / Android 百晓生 2020-5-9 2385

啥是串口

串口即串行接口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。串行接口 (Serial Interface) 是指数据一位一位地顺序传送,其特点是通信线路简单,只要一对传输线就可以实现双向通信(可以直接利用电话线作为传输线),从而大大降低了成本,特别适用于远距离通信,但传送速度较慢。

在Android中,我们可以调用Unix的动态连接库(.so扩展名文件)来集成串口通信,这种调用的方式称为JNI(Java Native Interface,即Java本地接口)。

使用说明,简单几步英文版

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.kongqw:AndroidSerialPort:1.0.1'
}

查看串口

SerialPortFinder serialPortFinder = new SerialPortFinder();
ArrayList<Device> devices = serialPortFinder.getDevices();

打开串口

mSerialPortManager = new SerialPortManager();

添加打开串口监听

mSerialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
    @Override
    public void onSuccess(File device) {
        
    }
    @Override
    public void onFail(File device, Status status) {
    }
});

添加数据通信监听

mSerialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
    @Override
    public void onDataReceived(byte[] bytes) {
        
    }
    @Override
    public void onDataSent(byte[] bytes) {
    }
});

打开串口

参数1:串口

参数2:波特率

返回:串口打开是否成功

boolean openSerialPort = mSerialPortManager.openSerialPort(device.getFile(), 115200);

发送数据

参数:发送数据 byte[]

返回:发送是否成功

boolean sendBytes = mSerialPortManager.sendBytes(sendContentBytes);

关闭串口

mSerialPortManager.closeSerialPort();

PS:传输协议需自行封装


其它例子(不推荐,供参考):

首先我们初始化SerialPortFinder,并创建SerialPort,打开串口:

mSerialPortFinder = new SerialPortFinder();
// 得到所有设备文件地址的数组
// 实际上该操作并不需要,这里只是示例打印出所有的设备信息
String[] entryValues = mSerialPortFinder.getAllDevicesPath();  
try {
    // 打开/dev/ttyUSB0路径设备的串口
    mSerialPort = new SerialPort(new File("/dev/ttyUSB0"), 9600, 0);
    
} catch (IOException e) {
    System.out.println("找不到该设备文件");
}

这样,我们可以从SerialPort对象中得到输入流,并开启一个子线程进行读取该设备传入的串口数据:

final InputStream inputStream = mSerialPort.getInputStream();
/* 开启一个线程进行读取 */
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            byte[] buffer = new byte[1024];
            int size = inputStream.read(buffer);
            byte[] readBytes = new byte[size];
            System.arraycopy(buffer, 0, readBytes, 0, size);
            System.out.println("received data => " + new String(readBytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

发送数据时,我们只需要从SerialPort对象中得到OutputStream对象,写入字节数组数据即可:

// 开启子线程进行发送数据
new Thread(new Runnable() {
    @Override
    public void run() {
    
        String content = "Hello World";
        byte[] bytes = content.getBytes();
        OutputStream out = mSerialPort.getOutputStream();
        // 写入数据
        try {
            out.write(bytes);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();


本文链接:https://www.it72.com/12617.htm

上传的附件:
推荐阅读
最新回复 (0)
返回