Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing aborts and typehints for sdoclient #352

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions canopen/objectdictionary/eds.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re
import io
import logging
import copy
import logging
import re

from canopen.objectdictionary import datatypes

try:
from configparser import RawConfigParser, NoOptionError, NoSectionError
except ImportError:
Expand Down Expand Up @@ -190,6 +192,28 @@ def import_from_node(node_id, network):
return od


def _calc_bit_length(data_type):
if data_type == datatypes.INTEGER8:
return 8
elif data_type == datatypes.INTEGER16:
return 16
elif data_type == datatypes.INTEGER32:
return 32
elif data_type == datatypes.INTEGER64:
return 64
else:
raise ValueError(f"Invalid data_type '{data_type}', expecting a signed integer data_type.")


def _signed_int_from_hex(hex_str, bit_length):
number = int(hex_str, 0)
limit = ((1 << bit_length - 1) - 1)
if number > limit:
return limit - number
else:
return number


def _convert_variable(node_id, var_type, value):
if var_type in (objectdictionary.OCTET_STRING, objectdictionary.DOMAIN):
return bytes.fromhex(value)
Expand Down Expand Up @@ -251,12 +275,20 @@ def build_variable(eds, section, node_id, index, subindex=0):

if eds.has_option(section, "LowLimit"):
try:
var.min = int(eds.get(section, "LowLimit"), 0)
min_string = eds.get(section, "LowLimit")
if var.data_type in objectdictionary.SIGNED_TYPES:
var.min = _signed_int_from_hex(min_string, _calc_bit_length(var.data_type))
else:
var.min = int(min_string, 0)
except ValueError:
pass
if eds.has_option(section, "HighLimit"):
try:
var.max = int(eds.get(section, "HighLimit"), 0)
max_string = eds.get(section, "HighLimit")
if var.data_type in objectdictionary.SIGNED_TYPES:
var.max = _signed_int_from_hex(max_string, _calc_bit_length(var.data_type))
else:
var.max = int(max_string, 0)
except ValueError:
pass
if eds.has_option(section, "DefaultValue"):
Expand Down
Loading