Zebra 在 Android Kotlin 中编写 RFID,某些标签可以正确执行,但其他标签则不能正确执行
我正在开发一个库存管理系统,需要将资产 ID(最大长度为 17)写入 RFID 标签。
第一个问题是,当我在标签上写入长度比已写入的 ID 短的 ID 时,它会将未覆盖的字符保留在标签中。 例如:如果标签上写有 ID“123456789”,并且我在标签上写了 id“abc”。标签的资产 ID 变为 abc456789。我尝试在写入之前删除并删除标签,但没有成功。
之后,我在目标 ID 之前添加零,直到它达到最大长度 (17),这样就不会在标签上写入长度较短的资产 ID,并且在读取后我会删除所有前面的零。这对某个标签效果很好,但对另一个标签效果不佳,我发现另一个标签上不能写入超过 12 个字符,但我不明白为什么,问题不在于 RFID 标签,因为它在另一个应用程序中运行良好,可以书写超过 12 个字符。
如果有人能帮助我确定为什么这个标签上只写了 12 个字符,而资产 ID 的其余部分被忽略,即使相同的代码适用于另一个 RFID 标签,我将非常感激。
这是写入标签方法:
fun writeTag(sourceEPC: String?, targetData: String): TagData? {
errorMessage = ""
try {
val tagData = TagData()
val tagAccess = TagAccess()
val writeAccessParams = tagAccess.WriteAccessParams()
writeAccessParams.accessPassword = 0
writeAccessParams.memoryBank = MEMORY_BANK.MEMORY_BANK_EPC
writeAccessParams.offset = 2
var paddedTargetData = padLeftZeros(targetData,17)
val targetDataInHex = HexStringConverter.getHexStringConverterInstance().stringToHex(if (paddedTargetData.length % 2 != 0) "0$paddedTargetData" else paddedTargetData)//if ODD
val padded = targetDataInHex + RFID_ADDED_VALUE
writeAccessParams.setWriteData(padded)
writeAccessParams.writeRetries = 1
writeAccessParams.writeDataLength = padded.length / 4 // WORD EQUALS 4 HEX
reader!!.Actions.TagAccess.writeWait(sourceEPC, writeAccessParams, null, tagData)
return tagData
} catch (e: InvalidUsageException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.info
println(errorMessage)
return null
} catch (e: OperationFailureException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.results
println(errorMessage)
return null
} catch (e: UnsupportedEncodingException) {
errorMessage = if (e.message == null) "" else e.message!!
println(errorMessage)
return null
}
}
读取完整标签方法:
fun readFullTag(sourceEPC: String): TagData? {
errorMessage = ""
try {
val tagAccess = TagAccess()
val readAccessParams = tagAccess.ReadAccessParams()
readAccessParams.accessPassword = 0
readAccessParams.memoryBank = MEMORY_BANK.MEMORY_BANK_TID
readAccessParams.offset = 0
return reader?.Actions?.TagAccess?.readWait(sourceEPC, readAccessParams, null, false)
} catch (e: InvalidUsageException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.info
println(errorMessage)
return null
} catch (e: OperationFailureException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.results
println(errorMessage)
return null
}
}
处理标签数据方法:
override fun handleTagData(tagData: Array<TagData?>?) {
var readValue = ""
if (!tagData.isNullOrEmpty()) readValue = tagData[0]!!.tagID.trimIndent().replace("\n", "")
if (isWritingRFID) {
isWritingRFID = false
if (currentRFIDAssetCode.isNotEmpty())
writeRFID(readValue, currentRFIDAssetCode)
} else {
CoroutineScope(Dispatchers.Main).launch {
if (!pDialog.isShowing) readFullRFID(readValue)
}
}
}
请随时询问任何其他代码或信息。
I am working on a inventory management system where it's required to write asset ids (with length of maximum 17) in to an RFID tag.
First the problem was that when I write on a tag an ID with shorter length than the already written one, it keeps the non overridden characters in the tag.
for ex: if the tag has the ID "123456789" written on it and I write id "abc" on the tag. The tag's asset id becomes abc456789. I tried killing and erasing the tag before writing but it didn't work.
After that, I though of appending zeros before the target ID until it reaches the maximum length (17) so that this way no asset id with shorter length will be written on the tag and after reading I remove all preceding zero's. This worked well with a certain tag but not with another one, I figured out that the other tag can't be written on with more than 12 characters but I don't get why and the problem isn't in the RFID tag since it works well in another application and can be written on with more than 12 characters.
I would be really thankful if anyone could help me identify why this tag has only 12 characters written on it and the rest of the asset ID is neglected even though the same code works with another RFID tag.
Here's the write tag method:
fun writeTag(sourceEPC: String?, targetData: String): TagData? {
errorMessage = ""
try {
val tagData = TagData()
val tagAccess = TagAccess()
val writeAccessParams = tagAccess.WriteAccessParams()
writeAccessParams.accessPassword = 0
writeAccessParams.memoryBank = MEMORY_BANK.MEMORY_BANK_EPC
writeAccessParams.offset = 2
var paddedTargetData = padLeftZeros(targetData,17)
val targetDataInHex = HexStringConverter.getHexStringConverterInstance().stringToHex(if (paddedTargetData.length % 2 != 0) "0$paddedTargetData" else paddedTargetData)//if ODD
val padded = targetDataInHex + RFID_ADDED_VALUE
writeAccessParams.setWriteData(padded)
writeAccessParams.writeRetries = 1
writeAccessParams.writeDataLength = padded.length / 4 // WORD EQUALS 4 HEX
reader!!.Actions.TagAccess.writeWait(sourceEPC, writeAccessParams, null, tagData)
return tagData
} catch (e: InvalidUsageException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.info
println(errorMessage)
return null
} catch (e: OperationFailureException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.results
println(errorMessage)
return null
} catch (e: UnsupportedEncodingException) {
errorMessage = if (e.message == null) "" else e.message!!
println(errorMessage)
return null
}
}
Read Full Tag method:
fun readFullTag(sourceEPC: String): TagData? {
errorMessage = ""
try {
val tagAccess = TagAccess()
val readAccessParams = tagAccess.ReadAccessParams()
readAccessParams.accessPassword = 0
readAccessParams.memoryBank = MEMORY_BANK.MEMORY_BANK_TID
readAccessParams.offset = 0
return reader?.Actions?.TagAccess?.readWait(sourceEPC, readAccessParams, null, false)
} catch (e: InvalidUsageException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.info
println(errorMessage)
return null
} catch (e: OperationFailureException) {
errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.results
println(errorMessage)
return null
}
}
Handle Tag Data method:
override fun handleTagData(tagData: Array<TagData?>?) {
var readValue = ""
if (!tagData.isNullOrEmpty()) readValue = tagData[0]!!.tagID.trimIndent().replace("\n", "")
if (isWritingRFID) {
isWritingRFID = false
if (currentRFIDAssetCode.isNotEmpty())
writeRFID(readValue, currentRFIDAssetCode)
} else {
CoroutineScope(Dispatchers.Main).launch {
if (!pDialog.isShowing) readFullRFID(readValue)
}
}
}
Feel free to ask for any additional code or info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的另一种方法是使用 NFC 中常用的技术,而不是用零填充,数据的第一个字节是数据长度的值(以十六进制表示)。
因此,旧数据是否没有被清零并不重要,并且您在检测它是用于消隐的零还是真正的零时不会有问题,
例如
0A 31 32 33 34 35 36 37 38 39 30
或文本
10 1 2 3 4 5 6 7 8 9 0
将被
03 41 42 43
或文本
3 ABC
覆盖,导致内存中的文本为
3 ABC 4 5 6 7 8 9 0
但是你会读取第一个字节来得到长度为 3,然后再读取 3 个字节以获取文本
A BC
Another way to solve this problem instead of padding with zeros is use a technique used a lot in NFC and the first byte of your data is the value of the length of the data (in Hex).
Therefore it does not matter if the old data is not zero'd out and you won't have a problem detecting if it is a zero for blanking or a real zero
e.g.
0A 31 32 33 34 35 36 37 38 39 30
or in text
10 1 2 3 4 5 6 7 8 9 0
would be overwritten with
03 41 42 43
or in text
3 A B C
resulting in text in memory of
3 A B C 4 5 6 7 8 9 0
But you would read the first byte to get the length of 3 and then read 3 more bytes to get in text
A B C