Skip to content

Commit

Permalink
Improve serial port read-write timeout mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
bang.liu committed Dec 3, 2020
1 parent 9d95809 commit f51a9cf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':pwc02incubatormodule')
implementation 'com.github.bingfeng1225:PWSerialPort:1.1.5'
implementation 'com.github.bingfeng1225:PWSerialPort:1.1.6'

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
Expand Down
2 changes: 1 addition & 1 deletion pwc02incubatormodule/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.github.bingfeng1225:PWSerialPort:1.1.5'
implementation 'com.github.bingfeng1225:PWSerialPort:1.1.6'
implementation group: 'io.netty', name: 'netty-all', version: '4.1.43.Final'
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,58 @@ private boolean ignorePackage() {
return result;
}


private boolean processBytesBuffer() {
while (this.buffer.readableBytes() < 4) {
return true;
}
byte[] header = new byte[C02IncubatorTools.HEADER.length];
this.buffer.getBytes(0, header);
byte command = this.buffer.getByte(3);
if (!C02IncubatorTools.checkHeader(header)) {
return this.ignorePackage();
}
if (!C02IncubatorTools.checkCommand(command)) {
//当前指令不合法 丢掉正常的包头以免重复判断
this.buffer.resetReaderIndex();
this.buffer.skipBytes(2);
this.buffer.discardReadBytes();
return this.ignorePackage();
}

int frameLength = 0xFF & this.buffer.getByte(2) + 3;
if (this.buffer.readableBytes() < frameLength) {
return true;
}
this.buffer.markReaderIndex();
byte[] data = new byte[frameLength];
this.buffer.readBytes(data, 0, data.length);

if (!C02IncubatorTools.checkFrame(data)) {
this.buffer.resetReaderIndex();
//当前包不合法 丢掉正常的包头以免重复判断
this.buffer.skipBytes(2);
this.buffer.discardReadBytes();
return this.ignorePackage();
}
this.buffer.discardReadBytes();
if (!this.ready) {
this.ready = true;
if (null != this.listener && null != this.listener.get()) {
this.listener.get().onC02IncubatorReady();
}
}
this.switchWriteModel();
if (null != this.listener && null != this.listener.get()) {
this.listener.get().onC02IncubatorPrint("C02IncubatorSerialPort Recv:" + C02IncubatorTools.bytes2HexString(data, true, ", "));
}
Message msg = Message.obtain();
msg.obj = data;
msg.what = 0x01;
this.handler.sendMessage(msg);
return true;
}

@Override
public void onConnected(PWSerialPortHelper helper) {
if (!this.isInitialized() || !helper.equals(this.helper)) {
Expand Down Expand Up @@ -205,53 +257,12 @@ public void onStateChanged(PWSerialPortHelper helper, PWSerialPortState state) {
}

@Override
public void onByteReceived(PWSerialPortHelper helper, byte[] buffer, int length) throws IOException {
public boolean onByteReceived(PWSerialPortHelper helper, byte[] buffer, int length) throws IOException {
if (!this.isInitialized() || !helper.equals(this.helper)) {
return;
return false;
}
this.buffer.writeBytes(buffer, 0, length);
while (this.buffer.readableBytes() >= 4) {
byte[] header = new byte[C02IncubatorTools.HEADER.length];
this.buffer.getBytes(0, header);
byte command = this.buffer.getByte(3);
if (!C02IncubatorTools.checkHeader(header) || !C02IncubatorTools.checkCommand(command)) {
if (this.ignorePackage()) {
continue;
} else {
break;
}
}
int frameLength = 0xFF & this.buffer.getByte(2) + 3;
if (this.buffer.readableBytes() < frameLength) {
break;
}
this.buffer.markReaderIndex();
byte[] data = new byte[frameLength];
this.buffer.readBytes(data, 0, data.length);

if (!C02IncubatorTools.checkFrame(data)) {
this.buffer.resetReaderIndex();
//当前包不合法 丢掉正常的包头以免重复判断
this.buffer.skipBytes(4);
this.buffer.discardReadBytes();
continue;
}
this.buffer.discardReadBytes();
if (!this.ready) {
this.ready = true;
if (null != this.listener && null != this.listener.get()) {
this.listener.get().onC02IncubatorReady();
}
}
this.switchWriteModel();
if (null != this.listener && null != this.listener.get()) {
this.listener.get().onC02IncubatorPrint("C02IncubatorSerialPort Recv:" + C02IncubatorTools.bytes2HexString(data, true, ", "));
}
Message msg = Message.obtain();
msg.obj = data;
msg.what = 0x01;
this.handler.sendMessage(msg);
}
return this.processBytesBuffer();
}


Expand All @@ -274,7 +285,7 @@ public void handleMessage(Message msg) {
}
case 0x01:
if (null != C02IncubatorSerialPort.this.listener && null != C02IncubatorSerialPort.this.listener.get()) {
C02IncubatorSerialPort.this.listener.get().onC02IncubatorPackageReceived( (byte[]) msg.obj);
C02IncubatorSerialPort.this.listener.get().onC02IncubatorPackageReceived((byte[]) msg.obj);
}
break;
}
Expand Down

0 comments on commit f51a9cf

Please sign in to comment.