RS232通信(Android)

时间:2022-09-23 06:24:20
一、 添加依赖
dependencies {
implementation 'com.github.kongqw:AndroidSerialPort:1.0.1'
} 二、 使用方法
package com.bug01.tryserial;

import com.kongqw.serialportlibrary.SerialPortManager;
import com.kongqw.serialportlibrary.listener.OnOpenSerialPortListener;
import com.kongqw.serialportlibrary.listener.OnSerialPortDataListener; import java.io.File; public class SerialUtil {
private SerialPortManager mSerialPortManager = new SerialPortManager();
private static SerialUtil instance = null; // 此处使用单例模式
public static SerialUtil getInstance() {
if (instance == null) {
synchronized (SerialUtil.class) {
if (instance == null) {
instance = new SerialUtil();
instance.mSerialPortManager.setOnSerialPortDataListener(instance.onSerialPortDataListener);
instance.mSerialPortManager.setOnOpenSerialPortListener(instance.onOpenSerialPortListener);
}
}
}
return instance;
} private OnSerialPortDataListener onSerialPortDataListener = new OnSerialPortDataListener() {
@Override
public void onDataReceived(byte[] bytes) {
System.out.println("收到了数据。");
} @Override
public void onDataSent(byte[] bytes) {
System.out.println("发送了数据。");
}
}; private OnOpenSerialPortListener onOpenSerialPortListener = new OnOpenSerialPortListener() {
@Override
public void onSuccess(File device) {
System.out.println("链接" + device.getName() +"成功");
} @Override
public void onFail(File device, Status status) {
System.out.println("链接" + device.getName() +"失败");
}
}; /**
* 链接串口
* @return
*/
public boolean Connect() {
return Connect("ttysWK1");
} /**
* 链接串口
* @param PortName 设备节点名称
* @return
*/
public boolean Connect(String PortName) {
return mSerialPortManager.openSerialPort(new File("dev/" + PortName), 9600);
} /**
* 写入数据
* @param val
*/
public void write(String val) {
mSerialPortManager.sendBytes(val.getBytes());
} public void write(byte[] val) {
mSerialPortManager.sendBytes(val);
}
}

后面我想了一下,有可能会有人不知道自己用的是哪个节点,那你就遍历出来,挨个测试一下。

遍历串口测试代码:

    public void trySerialTest() {
SerialPortFinder serialPortFinder = new SerialPortFinder();
SerialPortManager mSerialPortManager;
ArrayList<Device> devices = serialPortFinder.getDevices();
System.out.println("获取到节点数量为:" + devices.size());
Device device = null;
mSerialPortManager = new SerialPortManager(); mSerialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
@Override
public void onDataReceived(byte[] bytes) {
System.out.println("收到数据");
} @Override
public void onDataSent(byte[] bytes) {
}
}); mSerialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
@Override
public void onSuccess(File device) {
System.out.print("串口链接成功,节点为:");
System.out.println(device.getName());//打印节点ID
} @Override
public void onFail(File device, Status status) {
System.out.print("串口链接失败,节点为:");
System.out.println(device.getName() + status);
}
}); for (Device dev : devices
) {
boolean openSerialPort = mSerialPortManager.openSerialPort(dev.getFile(), 9600);
if (openSerialPort) {
mSerialPortManager.sendBytes("send something".getBytes()); try {
Thread.sleep(3000);//给点时间用来接收
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} System.out.println("结束测试");
mSerialPortManager.closeSerialPort();
}