Skip to content

Commit

Permalink
Add back PdoBase.export() dependency (canmatrix)
Browse files Browse the repository at this point in the history
The canmatrix dependency was removed on Oct 10, 2021
with commit c46228f.

Resolves christiansandberg#488
  • Loading branch information
erlend-aasland committed Jul 3, 2024
1 parent cc37aee commit 75fa0f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ classifiers = [
"Topic :: Scientific/Engineering",
]
dependencies = [
"canmatrix ~= 1.0.0",
"python-can >= 3.0.0",
]
dynamic = ["version"]
Expand Down
74 changes: 46 additions & 28 deletions test/test_pdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,41 @@


class TestPDO(unittest.TestCase):

def test_bit_mapping(self):
def setUp(self):
node = canopen.Node(1, EDS_PATH)
map = node.pdo.tx[1]
map.add_variable('INTEGER16 value') # 0x2001
map.add_variable('UNSIGNED8 value', length=4) # 0x2002
map.add_variable('INTEGER8 value', length=4) # 0x2003
map.add_variable('INTEGER32 value') # 0x2004
map.add_variable('BOOLEAN value', length=1) # 0x2005
map.add_variable('BOOLEAN value 2', length=1) # 0x2006
pdo = node.pdo.tx[1]
pdo.add_variable('INTEGER16 value') # 0x2001
pdo.add_variable('UNSIGNED8 value', length=4) # 0x2002
pdo.add_variable('INTEGER8 value', length=4) # 0x2003
pdo.add_variable('INTEGER32 value') # 0x2004
pdo.add_variable('BOOLEAN value', length=1) # 0x2005
pdo.add_variable('BOOLEAN value 2', length=1) # 0x2006

# Write some values
map['INTEGER16 value'].raw = -3
map['UNSIGNED8 value'].raw = 0xf
map['INTEGER8 value'].raw = -2
map['INTEGER32 value'].raw = 0x01020304
map['BOOLEAN value'].raw = False
map['BOOLEAN value 2'].raw = True
pdo['INTEGER16 value'].raw = -3
pdo['UNSIGNED8 value'].raw = 0xf
pdo['INTEGER8 value'].raw = -2
pdo['INTEGER32 value'].raw = 0x01020304
pdo['BOOLEAN value'].raw = False
pdo['BOOLEAN value 2'].raw = True

self.pdo = pdo
self.node = node

# Check expected data
self.assertEqual(map.data, b'\xfd\xff\xef\x04\x03\x02\x01\x02')
def test_pdo_map_bit_mapping(self):
self.assertEqual(pdo.data, b'\xfd\xff\xef\x04\x03\x02\x01\x02')

# Read values from data
self.assertEqual(map['INTEGER16 value'].raw, -3)
self.assertEqual(map['UNSIGNED8 value'].raw, 0xf)
self.assertEqual(map['INTEGER8 value'].raw, -2)
self.assertEqual(map['INTEGER32 value'].raw, 0x01020304)
self.assertEqual(map['BOOLEAN value'].raw, False)
self.assertEqual(map['BOOLEAN value 2'].raw, True)
def test_pdo_map_getitem(self):
pdo = self.pdo
self.assertEqual(pdo['INTEGER16 value'].raw, -3)
self.assertEqual(pdo['UNSIGNED8 value'].raw, 0xf)
self.assertEqual(pdo['INTEGER8 value'].raw, -2)
self.assertEqual(pdo['INTEGER32 value'].raw, 0x01020304)
self.assertEqual(pdo['BOOLEAN value'].raw, False)
self.assertEqual(pdo['BOOLEAN value 2'].raw, True)

def test_pdo_getitem(self):
node = self.node
self.assertEqual(node.tpdo[1]['INTEGER16 value'].raw, -3)
self.assertEqual(node.tpdo[1]['UNSIGNED8 value'].raw, 0xf)
self.assertEqual(node.tpdo[1]['INTEGER8 value'].raw, -2)
Expand All @@ -55,10 +60,23 @@ def test_bit_mapping(self):
self.assertEqual(node.tpdo[0x2002].raw, 0xf)
self.assertEqual(node.pdo[0x1600][0x2002].raw, 0xf)

def test_save_pdo(self):
node = canopen.Node(1, EDS_PATH)
node.tpdo.save()
node.rpdo.save()
def test_pdo_save(self):
self.node.tpdo.save()
self.node.rpdo.save()

def test_pdo_export(self):
import canmatrix
import tempfile

for pdo in "tpdo", "rpdo":
with tempfile.NamedTemporaryFile(suffix=".csv") as tmp:
fn = tmp.name
with self.subTest(pdo=pdo, filename=fn):
getattr(self.node, pdo).export(fn)
with open(fn) as csv:
header = csv.readline()
self.assertIn("ID", header)
self.assertIn("Frame Name", header)


if __name__ == "__main__":
Expand Down

0 comments on commit 75fa0f5

Please sign in to comment.