简体   繁体   English

如何在 Swift 中读取 3 个字节中的 19 位?

[英]How to read 19bits out of 3 bytes in Swift?

I have a Data object with 20 bytes that I'm getting from a BLE device.我有一个从 BLE 设备获取的 20 个字节的数据对象。

This is an example when I po the data in the CBCharacteristic :这是我在CBCharacteristic po数据时的一个示例:

▿ 20 bytes
  - count : 20
  ▿ pointer : 0x0000000282889ab0
    - pointerValue : 10779925168
  ▿ bytes : 20 elements
    - 0 : 16
    - 1 : 0
    - 2 : 0
    - 3 : 21
    - 4 : 0
    - 5 : 0
    - 6 : 20
    - 7 : 3
    - 8 : 87
    - 9 : 154
    - 10 : 3
    - 11 : 88
    - 12 : 204
    - 13 : 20
    - 14 : 255
    - 15 : 197
    - 16 : 7
    - 17 : 159
    - 18 : 56
    - 19 : 122 

Now I have instructions that tell me that on Byte 1,2,3 there is the signal that I'm looking for as 19 bits (0-524288)现在我有说明告诉我在字节 1,2,3 上有我正在寻找的信号为 19 位 (0-524288)

So how can I get that signal value?那么我怎样才能得到那个信号值呢?

I would appreciate reading material on how to get this on my own if necessary.如有必要,我将不胜感激阅读有关如何自行获取此信息的材料。 I don't have a proper CS background and I'm lost on how/where to even look for this.我没有适当的 CS 背景,我什至不知道如何/在哪里寻找它。

Thank you谢谢

EDIT (in response to @Sweeper):编辑(响应@Sweeper):

These are instructions for Byte 0这些是字节 0 的指令

General state / configuration. Contains following bits:
7 (highest) – Error state, reads 0 normally and 1 if any error in hardware side
6 – button pressed (’1’ – button is pressed, ’0’ – button is not pressed)
5 – USB connected (’1’ – USB is connected, ’0’ – USB is not connected)
4 – Charging/charged (’1’ – Charging, ’0’ – not charging)
3 – Gain of channel A. 2 gains (0 is slower, 1 is higher)
2 – Gain of channel B. 2 gains (0 is slower, 1 is higher)
1 – Gain of channel C. 2 gains (0 is slower, 1 is higher)
0 – Gain of channel D. 2 gains (0 is slower, 1 is higher)

And by doing this I can get the expected data for the first byte:通过这样做,我可以获得第一个字节的预期数据:

guard let data = characteristic.value else { return }
guard data.count == 20 else { return }
let val = [UInt8](data)
let general:UInt8 = val[0]
let error = general >> 7 & 1
let buttonPressed = general >> 6 & 1
let usbConnected = general >> 5 & 1
let charging = general >> 4 & 1
let gainChannelA = general >> 3 & 1
let gainChannelB = general >> 2 & 1
let gainChannelC = general >> 1 & 1
let gainChannelD = general >> 0 & 1

Does this help in knowing the endianness of the protocol?这有助于了解协议的字节序吗?

Since the data comes from multiple bytes, the answer depends on the endianness implied by the protocol.由于数据来自多个字节,因此答案取决于协议隐含的字节顺序 These 19 bits use two full bytes and three bits in a third byte.这 19 位使用两个完整字节和第三个字节中的三个位。

If these three bytes are stored in unsigned 8-bit variables a , b , and c , the value would be either如果这三个字节存储在无符号 8 位变量abc ,则该值将是

Int(a) << 11 + Int(b) << 3 + Int(c) & 0x07

or或者

Int(c) << 11 + Int(b) << 3 + Int(a) & 0x07

values for a b and c would come either from bytes 1, 2, and 3 or bytes 3, 2, 1, depending on the order specified in the protocol. a bc值将来自字节 1、2 和 3 或字节 3、2、1,具体取决于协议中指定的顺序。

Note: Expression x & 0x07 means "three lower bits", because 0x07 hex is 00000111 in binary.注意:表达式x & 0x07表示“三个低位”,因为0x07十六进制是二进制的00000111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM