android 串口收到的没有规则的数据 组成一包完整的数据 代码示例
时间: 2024-01-22 21:17:24 浏览: 42
处理没有规律的数据并组成一包完整的数据的过程通常称为“数据帧同步”。以下是一个处理串口数据的示例代码:
```
public class SerialPortUtil {
private SerialPort mSerialPort;
private OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private OnDataReceiveListener onDataReceiveListener = null;
private boolean isStop = false;
public SerialPortUtil() {
try {
mSerialPort = new SerialPort(new File("/dev/ttyS1"), 9600, 0);
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream();
mReadThread = new ReadThread();
isStop = false;
mReadThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String data) {
try {
mOutputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void setOnDataReceiveListener(OnDataReceiveListener dataReceiveListener) {
onDataReceiveListener = dataReceiveListener;
}
public void closeSerialPort() {
isStop = true;
if (mReadThread != null) {
mReadThread.interrupt();
mReadThread = null;
}
if (mSerialPort != null) {
mSerialPort.close();
mSerialPort = null;
}
}
public interface OnDataReceiveListener {
void onDataReceive(String data);
}
private class ReadThread extends Thread {
@Override
public void run() {
super.run();
while (!isStop && !isInterrupted()) {
int size;
try {
byte[] buffer = new byte[64];
if (mInputStream == null) return;
size = mInputStream.read(buffer);
if (size > 0) {
onDataReceiveListener.onDataReceive(new String(buffer, 0, size));
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
}
```
在这个示例中,我们创建了一个 `SerialPortUtil` 类,用于发送和接收串口数据。我们可以通过调用 `send()` 方法向串口发送数据,并通过 `setOnDataReceiveListener()` 方法设置接收数据的监听器。当收到数据时,我们将调用 `onDataReceive()` 方法,将收到的数据传递给监听器。
在 `ReadThread` 中,我们使用一个 `buffer` 数组来存储接收到的数据。当收到数据时,我们将数据转换成字符串,并将其传递给监听器。但是,如果数据没有规律,我们需要在 `ReadThread` 中添加更多的逻辑来处理数据帧同步。例如,我们可以使用一个缓冲区来存储收到的数据,并在接收到完整的数据包时,将其传递给监听器。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)