简体   繁体   English

将 Swift 结构转换为数据字节

[英]Convert Swift struct to Data bytes

I want to pass bytes data to bluetooth service in a format that is expected by bluetooth firmware.我想以蓝牙固件预期的格式将字节数据传递给蓝牙服务。

Expected data structure in c struct are: c 结构中的预期数据结构是:

typedef enum {
        CMD_TX_INFO = 0,
        CMD_DATA_REQ_START,
        CMD_DATA_REQ_END,
        CMD_DATA_REQ
} ble_cmd_type_t;

typedef struct ble_data_info_s
{
        uint32_t data_size_bytes;
        uint32_t info;
} ble_data_info_t;

typedef PACKED ( struct ble_cmd_info
{
        ble_cmd_type_t  cmd_type;
        ble_data_info_t info;
        uint8_t len;
        uint8_t data[10];
}) ble_cmd_data_t;

I have converted the this to swift struct as follows:我已将 this 转换为 swift 结构,如下所示:

enum BLECmdType : UInt8{
    case CMD_TX_INFO = 0
    case CMD_DATA_REQ_START
    case CMD_DATA_REQ_END
    case CMD_DATA_REQ
}

struct BLEDataInfo
{
    let dataSizeBytes: UInt32
    let info: UInt32
}

struct BLECmdData
{
    let cmdType:BLECmdType 
    let info:BLEDataInfo
    let len:UInt8
    let data: Array<UInt8>?
}

Then i convert the swit struct object into bytes like this, i am not sure if this approach is correct?然后我将 swit struct object 转换成这样的字节,我不确定这种方法是否正确? I am not getting the bytes in correct expected format:我没有以正确的预期格式获取字节:

var command = BLECmdData(cmdType:BLECmdType.CMD_DATA_REQ_START, 
    info: BLEDataInfo(dataSizeBytes: 100000, info: 10), 
    len: 1, 
    data: [1,2,3,4,5,6,7,8,9,10])

let bleData = Data(bytes: &command , count: MemoryLayout<BLECmdData>.stride)
        

Firstly is it possible to print the bytes generated for bleData object in string format so i can debug and see?首先,是否可以以字符串格式打印为bleData object 生成的字节,以便我可以调试并查看?

Secondly the bytes sent to BLE device are:其次,发送到 BLE 设备的字节是:

Bytes:
01 00 00 00 A0 86 01 00
E5 73 E0 98 0A 00 00 00
70 C5 CA 80

Firmware engineer mentioned, data should start with following:固件工程师提到,数据应该从以下开始:

Bytes:
01 00 A0 86 01 00
E5 73 E0 98

I am not sure if my approach to convert c struct to swift here is correct.我不确定我将 c 结构转换为 swift 的方法是否正确。

Looking forward for your reply.期待您的回复。

Thanks谢谢

  1. the result of the line let bleData = Data(bytes: &command, count: MemoryLayout<BLECmdData>.stride) let bleData = Data(bytes: &command, count: MemoryLayout<BLECmdData>.stride)这一行的结果

it depends on what cpu architecture the programming you're running on.这取决于您正在运行的编程的 cpu 架构。 (32bits vs 64 bits, big-endian vs little-endian). (32 位与 64 位,大端与小端)。

  1. you have to ask Firmware engineer about the protocol to communicate with their devices, then you will encode the data as the format specified in the protocol.您必须向Firmware engineer询问与他们的设备通信的协议,然后您将数据编码为协议中指定的格式。 It isn't important that you store the data in a class/struct/enum etc..., the important thing is how the data is encoded.将数据存储在类/结构/枚举等中并不重要,重要的是数据的编码方式。

I found a way to successfully push the data in correct format:我找到了一种以正确格式成功推送数据的方法:

Set the values:设置值:

var cmdTypeCopy:UInt8 = BLECmdType.CMD_DATA_REQ_START
var imageInfo = BLEDataInfo(dataSizeBytes: 100000, info: 10)
var cmdLenCopy = 10
var cmdDataCopy:Array<UInt8> = [1,2,3,4,5,6,7,8,9,10]

Create Data:创建数据:

var cmdTypeData = Data(bytes: &cmdTypeCopy, count: MemoryLayout<UInt8>.stride)
var imageInfoData = Data(bytes: &imageInfo, count: MemoryLayout<BLEImageInfo>.stride)
var cmdLenData = Data(bytes: &cmdLenCopy, count: MemoryLayout<UInt8>.stride)
var cmdDataData = Data(bytes: &cmdDataCopy, count: 10)

Then append one by one:然后append一一:

cmdTypeData.append(imageInfoData)
cmdTypeData.append(cmdLenData)
cmdTypeData.append(cmdDataData)

This worked perfectly and firmware got the data in correct format.这完美地工作并且固件以正确的格式获取数据。

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

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