首页 > 解决方案 > 如何在 Swift 中读取 3 个字节中的 19 位?

问题描述

我有一个从 BLE 设备获取的 20 字节数据对象。

这是我po在以下数据中的一个示例CBCharacteristic

▿ 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 

现在我有指令告诉我,在字节 1、2、3 上有我正在寻找的 19 位信号 (0-524288)

那么我怎样才能得到那个信号值呢?

如有必要,我将不胜感激阅读有关如何自己获取此信息的材料。我没有适当的 CS 背景,我什至不知道如何/在哪里寻找这个。

谢谢

编辑(回应@Sweeper):

这些是字节 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)

通过这样做,我可以获得第一个字节的预期数据:

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

这有助于了解协议的字节顺序吗?

标签: swiftbit-manipulationbytecore-bluetooth

解决方案


由于数据来自多个字节,答案取决于协议隐含的字节顺序。这 19 位使用两个完整字节和第三个字节中的三个位。

如果这三个字节存储在无符号 8 位变量abc中,则值将是

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

或者

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

a b和的值c来自字节 1、2 和 3 或字节 3、2、1,具体取决于协议中指定的顺序。

注意:表达式x & 0x07的意思是“三个低位”,因为0x07十六进制是00000111二进制的。


推荐阅读