canlib.kvadblib¶
Wrapper for the Kvaser kvaDbLib library
A CAN database contains information about messages. Each message has (among other attributes) an identifier, a name and one or several signals. The kvaDbLib library is an API for these CAN databases.
-
class
canlib.kvadblib.Attribute(db, handle)[source]¶ Bases:
objectFactory for creating different types of attributes.
This class is also the base class and thus contains all common properties.
-
name¶ Name of attribute.
Type: str
-
value¶ Attribute value
-
-
class
canlib.kvadblib.AttributeDefinition(db, handle)[source]¶ Bases:
objectFactory for creating different types of attribute definitions.
This class is also the base class and thus contains all common properties.
-
name¶ Name of attribute definition.
Type: str
-
owner¶ Return attribute owner
Type: AttributeOwner
-
-
class
canlib.kvadblib.AttributeOwner[source]¶ Bases:
canlib.cenum.CEnum-
DB= 1¶
-
ENV= 5¶
-
INVALID= 0¶
-
MESSAGE= 2¶
-
NODE= 3¶
-
SIGNAL= 4¶
-
-
class
canlib.kvadblib.AttributeType[source]¶ Bases:
canlib.cenum.CEnum-
ENUM= 4¶
-
FLOAT= 3¶
-
INTEGER= 1¶
-
INVALID= 0¶
-
STRING= 5¶
-
-
class
canlib.kvadblib.BoundMessage(message, frame)[source]¶ Bases:
objectA CAN data object that manipulates data through signals.
-
class
canlib.kvadblib.BoundSignal(signal, frame)[source]¶ Bases:
object-
is_enum¶ Whether this signal is an enum-signal
New in version 1.7.
Type: bool
-
name¶ Signal’s name string
Type: str
-
phys¶ Signal’s physical value
Type: intorfloat
-
raw¶ Signal’s raw value
Type: int
-
unit¶ Signal’s unit string
Type: str
-
-
class
canlib.kvadblib.Dbc(filename=None, name=None, protocol=None)[source]¶ Bases:
objectHolds the root database handle.
-
delete_attribute_definition(name)[source]¶ Delete attribute definition from database.
New in version 1.7.
-
delete_message(message)[source]¶ Delete message from database.
Parameters: message ( Message) – message to be deleted
-
flags¶ Get the database flags.
E.g. DATABASE_FLAG_J1939
-
get_attribute_definition_by_name(name)[source]¶ Find attribute definition using name.
Parameters: name (str) – name of attribute definition Returns an attribute definition object depending on type, e.g. if the type is AttributeType.INTEGER, an
IntegerAttributeDefinitionis returned.
-
get_attribute_value(name)[source]¶ Return attribute value
If the attribute is not set on the database, we return the attribute definition default value.
New in version 1.6.
-
get_message(id=None, name=None)[source]¶ Find message by id or name
If both id and name is given, both most match.
Parameters: - id (str) – message id to look for
- name (str) – message name to look for
Returns: Raises: KvdNoMessage– If no match was found, or if none ofidandnamewere given.
-
get_message_by_id(id)[source]¶ Find message by id
Parameters: id (str) – message id to look for Returns: MessageRaises: KvdNoMessage– If no match was found.
-
get_message_by_name(name)[source]¶ Find message by name
Parameters: name (str) – message name to look for Returns: MessageRaises: KvdNoMessage– If no match was found.
-
get_node_by_name(name)[source]¶ Find node by name
Parameters: name (str) – node name to look for Returns: NodeRaises: KvdNoNode– If no match was found.
-
messages(show_all=True)[source]¶ Return a generator of all database messages.
If you would like to omit the special message ‘VECTOR__INDEPENDENT_SIG_MSG’, which is used to collect removed signals under, set show_all to False or use the Dbc iterator directly:
db = kvadblib.Dbc(filename='mydb.dbc') for message in db: print(msg)
Parameters: show_all ( bool) – If true, all messages, including any special message such as ‘VECTOR__INDEPENDENT_SIG_MSG’ will be returnedChanged in version 1.8: Added argument show_all.
-
name¶ The current database name (read-only)
Type: str
-
new_attribute_definition(name, owner, type, definition)[source]¶ Create a new attribute definition in the database.
The owner specify where the attribute is applicable, e.g.
AttributeOwner.MESSAGEspecifies that this attribute is only applicable on messages (Message).Parameters: - name (str) – a unique name.
- owner (
AttributeOwner) – the owner type
Returns:
-
new_message(name, id, flags=0, dlc=None, comment=None)[source]¶ Create a new message in the database.
Parameters: - name (str) – name of message
- id (int) – message id
- flags (int, optional) – message flags, e.g. MESSAGE_EXT
Returns: canlib.kvadblib.message.Message
-
new_node(name, comment=None)[source]¶ Create a new node in the database.
Parameters: - name (str) – name of message
- comment (str, optional) – message comment
Returns:
-
node_in_signal(node, signal)[source]¶ Check if signal has been added to node.
Returns: True – signals contains node False: otherwise
-
protocol¶ The database protocol
Type: ProtocolType
-
-
class
canlib.kvadblib.DefaultDefinition(default)¶ Bases:
tupleAttribute definition for attributes defined using only default.
-
default¶ Alias for field number 0
-
-
class
canlib.kvadblib.EnumDefaultDefinition(default, enums)¶ Bases:
tupleAttribute definition for enumeration attributes.
Holds a definition using default and key-value pairs.
-
default¶ Alias for field number 0
-
enums¶ Alias for field number 1
-
-
class
canlib.kvadblib.EnumDefinition(db, handle, definition=None)[source]¶ Bases:
canlib.kvadblib.attributedef.AttributeDefinitionDefinition of an enum attribute.
-
add_enum_definition(enums)[source]¶ Add enum definitions.
Parameters: enums (dict) – key - value pair(s), example: {‘empty’: 0}
-
definition¶ Return attribute definition
Changed in version 1.6.
Type: EnumDefaultDefinition
-
-
class
canlib.kvadblib.EnumSignal(db, message, sh, name=None, type=None, byte_order=<SignalByteOrder.INTEL: 0>, mode=<SignalMultiplexMode.SIGNAL: 0>, size=None, scaling=ValueScaling(factor=1, offset=0), limits=None, unit=None, comment=None, enums={})[source]¶ Bases:
canlib.kvadblib.signal.SignalDatabase signal of type enum, holds meta data about a signal.
-
enums¶ Signal enum definition dictionary
Type: dict
-
-
class
canlib.kvadblib.Error[source]¶ Bases:
canlib.cenum.CEnum-
DATABASE_INTERNAL= -8¶
-
DB_FILE_OPEN= -7¶
-
FAIL= -1¶
-
INTERNAL= -6¶
-
IN_USE= -13¶
-
NO_ATTRIB= -10¶
-
NO_DATABASE= -2¶
-
NO_MSG= -4¶
-
NO_NODE= -9¶
-
NO_SIGNAL= -5¶
-
ONLY_ONE_ALLOWED= -11¶
-
PARAM= -3¶
-
WRONG_OWNER= -12¶
-
-
class
canlib.kvadblib.FloatDefinition(db, handle, definition=None)[source]¶ Bases:
canlib.kvadblib.attributedef.AttributeDefinitionDefinition of a float attribute.
-
definition¶ Attribute definition
Type: MinMaxDefinition
-
-
class
canlib.kvadblib.FrameBox(db, messages=())[source]¶ Bases:
objectHelper class for sending signals
This class allows sending signals without worrying about what message they are defined in. It does this by binding a message and all its signals to the same
Frameobject.Objects are created by giving them a
Dbcdatabase, and optionally a list of messages (either names orcanlib.kvadblib.Messageobjects):db = Dbc(...) framebox = FrameBox(db, messages=('Msg0','Msg1'))
Messages can also be added after instantiation with
add_message:framebox.add_message('Msg0', 'Msg1')
Then setting signal values for any added message is done with:
framebox.signal('Sig0').phys = 7 framebox.signal('Sig1').phys = 20
Once all values are set, they can easily be sent via the channel
channelwith:for frame in framebox.frames(): channel.write(frame)
Any
Frameboxmethods that return messages requires the message to have been added to the framebox, either with themessagesconstructor argument or withadd_message. Likewise, any methods that return signals require the signal’s message to have been added.-
add_message(message)[source]¶ Add a message to the framebox
The message will be available for all future uses of
FrameBox.messageandFrameBox.messages, and all its signals will be available for uses ofFrameBox.signalandFrameBox.signals.The
messageargument can either be a message name, or acanlib.kvadblib.Messageobject.
-
message(name)[source]¶ Retrieves a message by name
Returns a
BoundMessagethat shares itsFrameobject with its child signals.
-
signal(name)[source]¶ Retrieves a signal by name
Returns a
BoundSignalthat shares itsFrameobject with its parent message and sibling signals.
-
-
class
canlib.kvadblib.IntegerDefinition(db, handle, definition=None)[source]¶ Bases:
canlib.kvadblib.attributedef.AttributeDefinitionDefinition of an integer attribute.
-
definition¶ Return attribute definition
Type: MinMaxDefinition
-
-
exception
canlib.kvadblib.KvdErrInParameter[source]¶ Bases:
canlib.kvadblib.exceptions.KvdError-
status= -3¶
-
-
exception
canlib.kvadblib.KvdError[source]¶ Bases:
canlib.exceptions.DllExceptionBase class for exceptions raised by the kvadblib dll
-
exception
canlib.kvadblib.KvdInUse[source]¶ Bases:
canlib.kvadblib.exceptions.KvdError-
status= -13¶
-
-
exception
canlib.kvadblib.KvdNoAttribute[source]¶ Bases:
canlib.kvadblib.exceptions.KvdNotFound-
status= -10¶
-
-
exception
canlib.kvadblib.KvdNoMessage[source]¶ Bases:
canlib.kvadblib.exceptions.KvdNotFound-
status= -4¶
-
-
exception
canlib.kvadblib.KvdNoNode[source]¶ Bases:
canlib.kvadblib.exceptions.KvdNotFound-
status= -9¶
-
-
exception
canlib.kvadblib.KvdOnlyOneAllowed[source]¶ Bases:
canlib.kvadblib.exceptions.KvdError-
status= -11¶
-
-
exception
canlib.kvadblib.KvdWrongOwner[source]¶ Bases:
canlib.kvadblib.exceptions.KvdNotFound-
status= -12¶
-
-
class
canlib.kvadblib.Message(db, handle, name=None, id=None, flags=None, dlc=None, comment=None)[source]¶ Bases:
objectDatabase message, holds signals.
-
bind(frame=None)[source]¶ Bind this message to a frame
Creates a new BoundMessage object representing this message bound to the given Frame object, or a new Frame object if
frameisNone.
-
comment¶ Comment message
Type: str
-
delete_signal(signal)[source]¶ Delete signal from message.
Parameters: signal ( Signal) – signal to be deleted
-
dlc¶ The message dlc
Type: int
-
flags¶ The message flags
Type: MessageFlag
-
get_attribute_value(name)[source]¶ Return attribute value
If the attribute is not set on the message, we return the attribute definition default value.
-
id¶ The message identifier
Type: int
-
name¶ The message name
Type: str
-
new_signal(name, type=<SignalType.UNSIGNED: 2>, byte_order=<SignalByteOrder.INTEL: 0>, mode=<SignalMultiplexMode.SIGNAL: 0>, representation=None, size=None, scaling=ValueScaling(factor=1, offset=0), limits=None, unit=None, comment=None, enums={})[source]¶ Create and add a new signal to the message.
-
qualified_name¶ The qualified message name
Returns database and message names separated by a dot.
Type: str
-
-
class
canlib.kvadblib.MinMaxDefinition(default, min, max)¶ Bases:
tupleAttribute definition for attributes defined using default, min and max.
-
default¶ Alias for field number 0
-
max¶ Alias for field number 2
-
min¶ Alias for field number 1
-
-
class
canlib.kvadblib.Node(db, handle, name=None, comment=None)[source]¶ Bases:
objectDatabase Node
-
comment¶ The node’s comment
Type: str
-
get_attribute_value(name)[source]¶ Return attribute value
If the attribute is not set on the message, we return the attribute definition default value.
-
name¶ The node’s name
Type: str
-
-
class
canlib.kvadblib.ProtocolType[source]¶ Bases:
canlib.cenum.CEnum-
AFDX= 7¶
-
BEAN= 5¶
-
CAN= 0¶
-
CANFD= 9¶
-
ETHERNET= 6¶
-
FLEXRAY= 4¶
-
J1708= 8¶
-
LIN= 2¶
-
MOST= 3¶
-
UNKNOWN= 10¶
-
VAN= 1¶
-
-
class
canlib.kvadblib.Signal(db, message, sh, name=None, type=None, byte_order=None, mode=None, representation=None, size=None, scaling=None, limits=None, unit=None, comment=None)[source]¶ Bases:
objectDatabase signal, holds meta data about a signal
-
bind(frame=None)[source]¶ Bind this signal to a frame
Creates a new BoundSignal object representing this signal bound to the given Frame object, or a new Frame object if
frameisNone..
-
byte_order¶ Get the signal byte order encoding.
Type: SignalByteOrder
-
comment¶ Get the signal comment.
Type: str
-
data_from(can_data, phys=None, raw=None)[source]¶ Convert a raw or physical value into CAN data bytes.
-
get_attribute_value(name)[source]¶ Return attribute value
If the attribute is not set on the signal, we return the attribute definition default value.
-
limits¶ Get message min and max values
Type: ValueLimits
-
mode¶
-
name¶ Get the signal name.
Type: str
-
qualified_name¶ Get the qualified signal name.
Returns database, message and signal names separated by dots.
Type: str
-
scaling¶ Get the signals factor and offset
Type: ValueScaling
-
set_attribute_value(name, value)[source]¶ Set value of attribute ‘name’ on signal.
If no attribute called ‘name’ is set on signal, attach a signal attribute from the database attribute definition first.
-
type¶ Get the signal representation type.
Type: SignalType
-
unit¶ Get the signal unit
Type: str
-
-
class
canlib.kvadblib.SignalMultiplexMode[source]¶ Bases:
canlib.cenum.CEnum-
MUX_INDEPENDENT= -1¶
-
MUX_SIGNAL= -2¶
-
SIGNAL= 0¶
-
-
class
canlib.kvadblib.SignalType[source]¶ Bases:
canlib.cenum.CEnum-
DOUBLE= 4¶
-
ENUM_SIGNED= 101¶
-
ENUM_UNSIGNED= 102¶
-
FLOAT= 3¶
-
INVALID= 0¶
-
SIGNED= 1¶
-
UNSIGNED= 2¶
-
-
class
canlib.kvadblib.StringDefinition(db, handle, definition=None)[source]¶ Bases:
canlib.kvadblib.attributedef.AttributeDefinitionDefinition of a string attribute.
-
definition¶ Return attribute definition
Type: DefaultDefinition
-
-
class
canlib.kvadblib.ValueLimits(min, max)¶ Bases:
tuple-
max¶ Alias for field number 1
-
min¶ Alias for field number 0
-
-
class
canlib.kvadblib.ValueScaling(factor, offset)¶ Bases:
tuple-
factor¶ Alias for field number 0
-
offset¶ Alias for field number 1
-
-
class
canlib.kvadblib.ValueSize(startbit, length)¶ Bases:
tuple-
length¶ Alias for field number 1
-
startbit¶ Alias for field number 0
-
-
canlib.kvadblib.bytes_to_dlc(num_bytes, protocol)[source]¶ Convert number of bytes to DLC for given protocol.