Explorar el Código

【完善】:uctypes 模块

SummerGift hace 7 años
padre
commit
7d61c09e46
Se han modificado 1 ficheros con 67 adiciones y 13 borrados
  1. 67 13
      docs/05-System_Module/03-uctypes.md

+ 67 - 13
docs/05-System_Module/03-uctypes.md

@@ -1,30 +1,84 @@
 # **uctypes** – 以结构化的方式访问二进制数据
 
-!!! abstract "简介"
-    `uctypes` 模块是 MicroPython 的外函数库,它提供 C 兼容的数据类型。
+!!! abstract "简介"   
+    uctypes 模块用来访问二进制数据结构,它提供 C 兼容的数据类型。 
 
 ## 常量
-### **uctypes.LITTLE_ENDIAN**
-Layout type for a little-endian packed structure. (Packed means that every field occupies exactly as many bytes as defined in the descriptor, i.e. the alignment is 1).
+- uctypes.LITTLE_ENDIAN — 小端压缩结构。
+- uctypes.BIG_ENDIAN — 大端压缩结构类型。
+- NATIVE —  mricopython 本地的存储类型
 
-### **uctypes.BIG_ENDIAN**  
-Layout type for a big-endian packed structure.
 
-### **uctypes.NATIVE**  
-Layout type for a native structure - with data endianness and alignment conforming to the ABI of the system on which MicroPython runs.
+## 构造函数
+
+### class uctypes.struct(addr, descriptor, type)
+将内存中以 c 形式打包的结构体或联合体转换为字典,并返回该字典。
+```
+addr:开始转换的地址
+descriptor:转换描述符
+格式:"field_name":offset|uctypes.UINT32
+offset:偏移量,
+单位:字节、VOID、UINT8、INT8、UINT16、INT16、UINT32、INT32、UINT64、INT64、BFUINT8、BFINT8、BFUINT16、BFINT16、BFUINT32、BFINT32、BF_POS、BF_LEN、FLOAT32、FLOAT64、PTR、ARRAY
+type:c 结构体或联合体存储类型,默认为本地存储类型
+```
+
+示例:
+
+```python
+>>> a = b"0123"
+>>> s = uctypes.struct(uctypes.addressof(a), {"a": uctypes.UINT8 | 0, "b": uctypes.UINT16 | 1}, uctypes.LITTLE_ENDIAN)
+>>> print(s)
+<struct STRUCT 3ffc7360>
+>>> print(s.a)
+48
+>>> s.a = 49
+>>> print(a)
+b'1123'
+```
+
+## 方法
 
-## 函数
 ### **uctypes.sizeof**(struct)  
-Return size of data structure in bytes. Argument can be either structure class or specific instantiated structure object (or its aggregate field).
+按字节返回数据的大小。参数可以是类或者数据对象 (或集合)。 
+示例:
+```python
+>>> a = b"0123"
+>>>b = uctypes.struct(uctypes.addressof(a), {"a": uctypes.UINT8 | 0, "b": uctypes.UINT16 | 1}, uctypes.LITTLE_ENDIAN)
+>>> b.a
+48
+>>> print(uctypes.sizeof(b))
+3
+```
 
 ### **uctypes.addressof**(obj)  
-Return address of an object. Argument should be bytes, bytearray or other object supporting buffer protocol (and address of this buffer is what actually returned).
+返回对象地址。参数需要是 bytes, bytearray 。 
+示例:
+
+```python
+>>> a = b"0123"
+>>> print(uctypes.addressof(a))
+1073504048
+```
 
 ### **uctypes.bytes_at**(addr, size)  
-Capture memory at the given address and size as bytes object. As bytes object is immutable, memory is actually duplicated and copied into bytes object, so if memory contents change later, created object retains original value.
+捕捉从 addr 开始到 size 个地址偏移量结束的内存数据为 bytearray 对象并返回。 
+示例:
+
+```python
+>>> a = b"0123"
+>>>print( uctypes.bytes_at(uctypes.addressof(a), 4))
+b'0123'
+```
 
 ### **uctypes.bytearray_at**(addr, size)  
-Capture memory at the given address and size as bytearray object. Unlike bytes_at() function above, memory is captured by reference, so it can be both written too, and you will access current value at the given memory address.
+捕捉给定大小和地址内存为 bytearray 对象。与 bytes_at() 函数不同的是,它可以被再次写入,可以访问给定地址的参数。 
+示例:
+
+```python
+>>> a = b"0123"
+>>> print(uctypes.bytearray_at(uctypes.addressof(a), 2))
+bytearray(b'01')
+```
 
 更多内容可参考 [uctypes](http://docs.micropython.org/en/latest/pyboard/library/uctypes.html) 。