Skip to content

Commit

Permalink
A bit better data type handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
christiansandberg committed Nov 2, 2016
1 parent dd52551 commit 7a1325c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
6 changes: 3 additions & 3 deletions canopen/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def raw(self):
+---------------------------+----------------------------+
| REALxx | :class:`float` |
+---------------------------+----------------------------+
| VISIBLE_STRING | :class:`str` |
| +----------------------------+
| VISIBLE_STRING | :class:`str` / |
| | ``unicode`` (Python 2) |
+---------------------------+----------------------------+
| UNICODE_STRING | :class:`str` |
| UNICODE_STRING | :class:`str` / |
| | ``unicode`` (Python 2) |
+---------------------------+----------------------------+
| OCTET_STRING | :class:`bytes` |
+---------------------------+----------------------------+
Expand Down
20 changes: 11 additions & 9 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
UNSIGNED64 = 0x1B

SIGNED_TYPES = (INTEGER8, INTEGER16, INTEGER32, INTEGER64)
UNSIGNED_TYPES = (BOOLEAN, UNSIGNED8, UNSIGNED16, UNSIGNED32, UNSIGNED64)
UNSIGNED_TYPES = (UNSIGNED8, UNSIGNED16, UNSIGNED32, UNSIGNED64)
INTEGER_TYPES = SIGNED_TYPES + UNSIGNED_TYPES
FLOAT_TYPES = (REAL32, REAL64)
NUMBER_TYPES = INTEGER_TYPES + FLOAT_TYPES


def import_od(filename):
Expand Down Expand Up @@ -243,14 +244,15 @@ def encode_raw(self, value):
elif self.data_type in self.STRUCT_TYPES:
if self.data_type in INTEGER_TYPES:
value = int(value)
if self.min is not None and value < self.min:
logger.warning(
"Value %d is less than min value %d", value, self.min)
if self.max is not None and value > self.max:
logger.warning(
"Value %d is greater than max value %d",
value,
self.max)
if self.data_type in NUMBER_TYPES:
if self.min is not None and value < self.min:
logger.warning(
"Value %d is less than min value %d", value, self.min)
if self.max is not None and value > self.max:
logger.warning(
"Value %d is greater than max value %d",
value,
self.max)
try:
return self.STRUCT_TYPES[self.data_type].pack(value)
except struct.error:
Expand Down
24 changes: 18 additions & 6 deletions canopen/pdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ def export(self, filename):
for pdo_map in pdo_maps.values():
if pdo_map.cob_id is None:
continue
direction = "Tx" if pdo_map.cob_id & 0x80 else "Rx"
map_id = pdo_map.cob_id >> 8
name = "%sPDO%d_node%d" % (direction, map_id, self.parent.id)
frame = canmatrix.Frame(name, Id=pdo_map.cob_id, extended=0)
frame = canmatrix.Frame(pdo_map.name,
Id=pdo_map.cob_id,
extended=0)
for var in pdo_map.map:
is_signed = var.od.data_type in objectdictionary.SIGNED_TYPES
is_float = var.od.data_type in objectdictionary.FLOAT_TYPES
Expand Down Expand Up @@ -137,7 +136,7 @@ def __init__(self, pdo_node, com_index, map_index):
self.cob_id = None
#: Is the remote transmit request (RTR) allowed for this PDO
self.rtr_allowed = True
#: Transmission type (1-255)
#: Transmission type (0-255)
self.trans_type = None
#: List of variables mapped to this PDO
self.map = None
Expand Down Expand Up @@ -183,6 +182,19 @@ def _get_variable(self, index, subindex):
def _update_data_size(self):
self.data = bytearray(int(math.ceil(self._get_total_size() / 8.0)))

@property
def name(self):
"""A descriptive name of the PDO.
Examples:
- TxPDO1_node4
- RxPDO4_node1
"""
direction = "Tx" if self.cob_id & 0x80 else "Rx"
map_id = self.cob_id >> 8
node_id = self.cob_id & 0x7F
return "%sPDO%d_node%d" % (direction, map_id, node_id)

def read(self):
com_record = self.pdo_node.parent.sdo[self.com_index]
map_record = self.pdo_node.parent.sdo[self.map_index]
Expand Down Expand Up @@ -302,7 +314,7 @@ def stop(self):
self.transmit_thread = None

def remote_request(self):
"""Send a remote request for the transmit PDO.
"""Send a remote request for the transmit PDO.
Silently ignore if not allowed.
"""
if self.enabled and self.rtr_allowed:
Expand Down
2 changes: 1 addition & 1 deletion canopen/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

__version__ = "0.3.0.dev9"
__version__ = "0.3.0.dev10"

0 comments on commit 7a1325c

Please sign in to comment.