eryksun added the comment:
It seems you want a BigEndianStructure:
from ctypes import *
class SAEJ1939MsgId(BigEndianStructure):
_fields_ = (('reserved', c_uint8, 3),
('priority', c_uint8, 3),
('extended_data_page', c_uint8, 1),
('data_page', c_uint8, 1),
('pdu_format', c_uint8),
('pdu_specific', c_uint8),
('source_address', c_uint8))
def __init__(self, *args, pgn=0, **kwds):
super().__init__(*args, **kwds)
if pgn > 0:
self.pgn = pgn
def __int__(self):
return int.from_bytes(self, 'big')
@property
def pgn(self):
"""pgn is an 18-bit number consisting of EDP, DP, PF, and PS"""
return (int(self) >> 8) & 0x3FFFF
@pgn.setter
def pgn(self, value):
value |= self.priority << 18
view = (c_char * 3).from_buffer(self)
view[:] = value.to_bytes(3, 'big')
@classmethod
def from_bytes(cls, msg_id):
return cls.from_buffer_copy(msg_id)
@classmethod
def from_integer(cls, msg_id):
msg_id_bytes = msg_id.to_bytes(sizeof(cls), 'big')
return cls.from_buffer_copy(msg_id_bytes)
Example:
>>> a = SAEJ1939MsgId(priority=7, source_address=3)
>>> hex(int(a))
'0x1c000003'
>>> b = SAEJ1939MsgId(pgn=0xf004, priority=7, source_address=3)
>>> hex(int(b))
'0x1cf00403'
>>> b.priority
7
>>> b.pdu_format
240
>>> b.pdu_specific
4
>>> b.source_address
3
>>> c = SAEJ1939MsgId.from_integer(int(b))
>>> hex(int(c))
'0x1cf00403'
----------
nosy: +eryksun
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue24859>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com