Python 等效于 C 结构(将应用程序表单 C 移植到 Python) [英] Python equivalent of C struct (porting app form C to python)

查看:29
本文介绍了Python 等效于 C 结构(将应用程序表单 C 移植到 Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移植一个简单​​的蓝牙应用程序,它将 L2Cap 协议上的魔术"数据包发送到蓝牙设备..

我在将 C 中的 struct 对象转换为 Python 等效项时遇到问题..

在 c:

/* 命令类型 */#define CFGBT_TYPE_SETREQ 0x00#define CFGBT_TYPE_SETRES 0x01#define CFGBT_TYPE_GETREQ 0x02#define CFGBT_TYPE_GETRES 0x03/* 变量类型 */#define CFG_VARIDT_UINT16 0x0000#define CFG_VARIDT_UINT32 0x1000#define CFG_VARIDT_STRING16 0x2000类型定义结构{uint8_t 类型、状态;uint16_t 变量;uint8_t 值[16];} __attribute__((packed)) CFGBTFRAME;静态CFGBTFRAME c;

然后在应用程序中它是这样使用的:

/* 设置 */c.type = CFGBT_TYPE_GETREQ;c.varid = strtol(argv[3], NULL, 0);c.状态= 0;memset(c.value, 0, 16);/* 发送帧 */write(s, &c, sizeof(c));

你能指出我如何使用 python 构造相同的数据包/结构类结构吗?

我知道我可能需要使用 ctypes 并创建空"类,但是如何将所有这些组合在一起?

解决方案

您可以使用 struct 模块将值打包成字节字符串,例如:

<预><代码>>>>导入结构>>>类型,状态,变量,值 = 1, 0, 16, b'Hello'>>>buffer = struct.pack('>BBH16s', type, status, varid, value)>>>缓冲b'\x01\x00\x00\x10Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

或者,您可以使用 ctypes.Structure 来定义将表示您的结构的类.它的优点是更易于与 Python 代码一起使用,但您必须考虑对齐和填充问题并自行解决(可能使用 struct).

I'm porting a simple bluetooth app, which sends "magic" packet on L2Cap protocol to bluetooth device..

I have a problem with converting struct object in C to python equivalent..

In c:

/* command types */
#define CFGBT_TYPE_SETREQ           0x00
#define CFGBT_TYPE_SETRES           0x01
#define CFGBT_TYPE_GETREQ           0x02
#define CFGBT_TYPE_GETRES           0x03

/* varid types */
#define CFG_VARIDT_UINT16                   0x0000
#define CFG_VARIDT_UINT32                   0x1000
#define CFG_VARIDT_STRING16                 0x2000

typedef struct {
    uint8_t      type, status;
    uint16_t     varid;
    uint8_t      value[16];
} __attribute__((packed)) CFGBTFRAME;

static CFGBTFRAME c;

and then in app it's used like that:

        /* set up */
    c.type = CFGBT_TYPE_GETREQ;
    c.varid = strtol(argv[3], NULL, 0);
    c.status = 0;
    memset(c.value, 0, 16);
    /* send frame */
    write(s, &c, sizeof(c));

Can you point me out how to construct same packet/stuct-like structure using python?

I know I will probably need to use ctypes and create "empty" class, but how to get all this together?

解决方案

You can go about it with the struct module to pack values into a byte string, for example:

>>> import struct
>>> type, status, varid, value = 1, 0, 16, b'Hello'
>>> buffer = struct.pack('>BBH16s', type, status, varid, value)
>>> buffer
b'\x01\x00\x00\x10Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Alternatively, you can use ctypes.Structure to define a class that will be representing your structure. It has the advantage of being easier to use with Python code, but you have to take into account alignment and padding issues and resolve them yourself (perhaps with struct).

这篇关于Python 等效于 C 结构(将应用程序表单 C 移植到 Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆