Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_visible_string(self):
var = od.Variable("Test VISIBLE_STRING", 0x1000)
var.data_type = od.VISIBLE_STRING
self.assertEqual(var.decode_raw(b"abcdefg"), "abcdefg")
self.assertEqual(var.decode_raw(b"zero terminated\x00"), "zero terminated")
self.assertEqual(var.encode_raw("testing"), b"testing")
def test_subindexes(self):
array = od.Array("Test Array", 0x1000)
last_subindex = od.Variable("Last subindex", 0x1000, 0)
last_subindex.data_type = od.UNSIGNED8
array.add_member(last_subindex)
array.add_member(od.Variable("Test Variable", 0x1000, 1))
array.add_member(od.Variable("Test Variable 2", 0x1000, 2))
self.assertEqual(array[0].name, "Last subindex")
self.assertEqual(array[1].name, "Test Variable")
self.assertEqual(array[2].name, "Test Variable 2")
self.assertEqual(array[3].name, "Test Variable_3")
def test_unsigned32(self):
var = od.Variable("Test UNSIGNED32", 0x1000)
var.data_type = od.UNSIGNED32
self.assertEqual(var.decode_raw(b"\xfc\xfd\xfe\xff"), 4294901244)
self.assertEqual(var.encode_raw(4294901244), b"\xfc\xfd\xfe\xff")
def build_variable(par_tree):
index = int(par_tree.get("Index"), 0)
subindex = int(par_tree.get("SubIndex"))
name = par_tree.get("SymbolName")
data_type = par_tree.get("DataType")
par = objectdictionary.Variable(name, index, subindex)
factor = par_tree.get("Factor", "1")
par.factor = int(factor) if factor.isdigit() else float(factor)
unit = par_tree.get("Unit")
if unit and unit != "-":
par.unit = unit
description = par_tree.find("Description")
if description is not None:
par.description = description.text
if data_type in DATA_TYPES:
par.data_type = DATA_TYPES[data_type]
else:
logger.warning("Don't know how to handle data type %s", data_type)
par.access_type = par_tree.get("AccessType", "rw")
try:
par.min = int(par_tree.get("MinimumValue"))
except (ValueError, TypeError):
def build_variable(eds, section, node_id, index, subindex=0):
"""Creates a object dictionary entry.
:param eds: String stream of the eds file
:param section:
:param node_id: Node ID
:param index: Index of the CANOpen object
:param subindex: Subindex of the CANOpen object (if presente, else 0)
"""
name = eds.get(section, "ParameterName")
var = objectdictionary.Variable(name, index, subindex)
var.data_type = int(eds.get(section, "DataType"), 0)
var.access_type = eds.get(section, "AccessType").lower()
if var.data_type > 0x1B:
# The object dictionary editor from CANFestival creates an optional object if min max values are used
# This optional object is then placed in the eds under the section [A0] (start point, iterates for more)
# The eds.get function gives us 0x00A0 now convert to String without hex representation and upper case
# The sub2 part is then the section where the type parameter stands
try:
var.data_type = int(eds.get("%Xsub1" % var.data_type, "DefaultValue"), 0)
except NoSectionError:
logger.warning("%s has an unknown or unsupported data type (%X)", name, var.data_type)
# Assume DOMAIN to force application to interpret the byte data
var.data_type = objectdictionary.DOMAIN
if eds.has_option(section, "LowLimit"):
try:
def _fill_map(self, needed):
"""Fill up mapping array to required length."""
logger.info("Filling up fixed-length mapping array")
while len(self.map) < needed:
# Generate a dummy mapping for an invalid object with zero length.
obj = objectdictionary.Variable('Dummy', 0, 0)
var = Variable(obj)
var.length = 0
self.map.append(var)
def load_configuration(self):
''' Load the configuration of the node from the object dictionary.'''
for obj in self.object_dictionary.values():
if isinstance(obj, Record) or isinstance(obj, Array):
for subobj in obj.values():
if isinstance(subobj, Variable) and subobj.writable and (subobj.value is not None):
self.__load_configuration_helper(subobj.index, subobj.subindex, subobj.name, subobj.value)
elif isinstance(obj, Variable) and obj.writable and (obj.value is not None):
self.__load_configuration_helper(obj.index, None, obj.name, obj.value)
self.pdo.read() # reads the new configuration from the driver