-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e94ee4
commit cdda7c6
Showing
9 changed files
with
107 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.sjx.serialhelper | ||
|
||
object ByteUtils { | ||
/** | ||
* ByteArray中查找数组索引 | ||
*/ | ||
fun getIndexRange(byteArray: ByteArray, startByteArray: ByteArray, endByteArray: ByteArray): IntArray{ | ||
// 查找头 | ||
var resultStartIndex = -1 | ||
var resultEndIndex = -1 | ||
var startI = 0 | ||
while (startI < byteArray.size - startByteArray.size){ | ||
// 查找 | ||
var findStart = true | ||
for(i in startByteArray.indices){ | ||
findStart = true | ||
if (byteArray[startI + i] != startByteArray[i]){ | ||
findStart = false | ||
break | ||
} | ||
} | ||
if(findStart){ | ||
resultStartIndex = startI | ||
break | ||
} | ||
startI ++ | ||
} | ||
|
||
var endI = startI + startByteArray.size | ||
while (endI < byteArray.size - endByteArray.size){ | ||
// 查找 | ||
var findEnd = true | ||
for(i in endByteArray.indices){ | ||
findEnd = true | ||
if (byteArray[endI + i] != endByteArray[i]){ | ||
findEnd = false | ||
break | ||
} | ||
} | ||
if(findEnd){ | ||
resultEndIndex = endI + endByteArray.size | ||
break | ||
} | ||
endI ++ | ||
} | ||
|
||
return intArrayOf(resultStartIndex, resultEndIndex) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
serialhelperlibrary/src/main/java/com/sjx/serialhelperlibrary/CheckFullFrame.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package com.sjx.serialhelperlibrary | ||
|
||
interface CheckFullFrame { | ||
/** | ||
* @param data 需要校验的数据 | ||
* @return 返回根据自己规则对应的起始索引与终止索引号, intArrayOf(0, data.size) | ||
*/ | ||
fun isFullFrame(data: ByteArray): IntArray | ||
} |
3 changes: 3 additions & 0 deletions
3
serialhelperlibrary/src/main/java/com/sjx/serialhelperlibrary/OnUsbDataListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package com.sjx.serialhelperlibrary | ||
|
||
interface OnUsbDataListener { | ||
/** | ||
* @param bytes 接收串口返回的数据,在子线程中 | ||
*/ | ||
fun onDataReceived(bytes: ByteArray) | ||
fun onDataError(e: Exception?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters