如何使用课程中的字段?

时间:2019-06-26 10:46:33

标签: python python-3.x class

class TPCANTimestamp (Structure):
    """
    Represents a timestamp of a received PCAN message
    Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
    """
    _fields_ = [ ("millis", c_uint),           # Base-value: milliseconds: 0.. 2^32-1
                 ("millis_overflow", c_ushort), # Roll-arounds of millis
                 ("micros", c_ushort) ]

如何使用TPCANTimestamp类中的字段?

1 个答案:

答案 0 :(得分:0)

  

如何使用 TPCANTimestamp类中的字段?

将它们用作Structure的属性:

from ctypes import *


class TPCANTimestamp(Structure):
    """
    Represents a timestamp of a received PCAN message
    Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
    """
    _fields_ = [ ("millis", c_uint),           # Base-value: milliseconds: 0.. 2^32-1
                 ("millis_overflow", c_ushort), # Roll-arounds of millis
                 ("micros", c_ushort)]

t = TPCANTimestamp(100, 20, 30)
print(t.millis, t.micros)

输出:

100 30