Skip to content

调整64字节为1024字节防止数据丢失bug #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class SerialPortWrapper {
private OutputStream out;
private InputStream in;
private Thread readThread;
private static final int BUFFER_SIZE = 1024;
private Remover remover;

private AtomicBoolean closed = new AtomicBoolean(false);
Expand All @@ -37,22 +38,29 @@ public SerialPortWrapper(String path, SerialPort serialPort, final EventSender s
this.readThread = new Thread(new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[64];
byte[] buffer = new byte[BUFFER_SIZE];
while (!closed.get()) {
try {
int size;
if (in == null) return;
size = in.read(buffer);

int size = in.read(buffer);
if (size > 0) {
WritableMap event = Arguments.createMap();
String hex = SerialPortApiModule.bytesToHex(buffer, size);
event.putString("data", hex);
event.putString("path", path);
Log.i("serialport", "read size: " + size + ", hex: " + hex);
sender.sendEvent(DataReceivedEvent, event);
}

Thread.sleep(10);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不需要强制休眠,Inputstream.read 方法是阻塞,如果没有读到数据会阻塞住,并不是一直空跑 while 循环

} catch (IOException e) {
Log.e("serialport", "Error reading data: " + e.getMessage());
e.printStackTrace();
return;
} catch (InterruptedException e) {
Log.e("serialport", "Thread interrupted: " + e.getMessage());
return;
}
}
}
Expand Down