android 串口通信
2020-12-26 本文已影响0人
小小痞子LIU
/**
* 串口操作类
*/
public class SerialPortUtil {
private String TAG = SerialPortUtil.class.getSimpleName();
private SerialPort mSerialPort;
private OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private String path = "dev/ttyS0";//端口号,也就是安卓平板电脑的串口名称
private int baudrate = 115200;//波特率
private static SerialPortUtil portUtil;
private OnDataReceiveListener onDataReceiveListener = null;
private boolean isStop = false;
public interface OnDataReceiveListener {
public void onDataReceive(byte[] buffer, int size);
}
//设置串口返回数据的监听
public void setOnDataReceiveListener(
OnDataReceiveListener dataReceiveListener) {
onDataReceiveListener = dataReceiveListener;
}
public static SerialPortUtil getInstance() {
if (null == portUtil) {
portUtil = new SerialPortUtil();
portUtil.onCreate();
}
return portUtil;
}
/**
* 初始化串口信息
*/
public void onCreate() {
try {
mSerialPort = new SerialPort(new File(path), baudrate, 0);
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream();
mReadThread = new ReadThread();
isStop = false;
mReadThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送指令到串口
*
* @param cmd
* @return
*/
public boolean sendCmds(String cmd) {
boolean result = true;
byte[] mBuffer = (cmd + "\r\n").getBytes();
//注意:我得项目中需要在每次发送后面加\r\n,大家根据项目项目做修改,也可以去掉,直接发送mBuffer
try {
if (mOutputStream != null) {
mOutputStream.write(mBuffer);
} else {
result = false;
}
} catch (IOException e) {
e.printStackTrace();
result = false;
}
return result;
}
public boolean sendBuffer(byte[] mBuffer) {
boolean result = true;
String tail = "\r\n";
byte[] tailBuffer = tail.getBytes();
byte[] mBufferTemp = new byte[mBuffer.length + tailBuffer.length];
System.arraycopy(mBuffer, 0, mBufferTemp, 0, mBuffer.length);
System.arraycopy(tailBuffer, 0, mBufferTemp, mBuffer.length, tailBuffer.length);
//注意:我得项目中需要在每次发送后面加\r\n,大家根据项目项目做修改,也可以去掉,直接发送mBuffer
try {
if (mOutputStream != null) {
mOutputStream.write(mBufferTemp);
} else {
result = false;
}
} catch (IOException e) {
e.printStackTrace();
result = false;
}
return result;
}
private class ReadThread extends Thread {
@Override
public void run() {
super.run();
while (!isStop && !isInterrupted()) {
int size;
try {
if (mInputStream == null)
return;
byte[] buffer = new byte[512];
size = mInputStream.read(buffer);
if (size > 0) {
// if(MyLog.isDyeLevel()){
// MyLog.log(TAG, MyLog.DYE_LOG_LEVEL, "length is:"+size+",data is:"+new String(buffer, 0, size));
// }
if (null != onDataReceiveListener) {
onDataReceiveListener.onDataReceive(buffer, size);
}
}
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
/**
* 关闭串口
*/
public void closeSerialPort() {
isStop = true;
if (mReadThread != null) {
mReadThread.interrupt();
}
if (mSerialPort != null) {
mSerialPort.close();
}
}
}
需要依赖下面的library库
链接: https://pan.baidu.com/s/1WdKZftBVVSnL51FicSmk3Q 提取码: 626b