Skip to content

Commit fb892d6

Browse files
authored
add busParams and use snake case for VectorChannelConfig (hardbyte#1422)
1 parent d3103d8 commit fb892d6

File tree

4 files changed

+179
-50
lines changed

4 files changed

+179
-50
lines changed

can/interfaces/vector/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
"""
22
"""
33

4-
from .canlib import VectorBus, VectorChannelConfig, get_channel_configs
4+
from .canlib import (
5+
VectorBus,
6+
get_channel_configs,
7+
VectorChannelConfig,
8+
VectorBusParams,
9+
VectorCanParams,
10+
VectorCanFdParams,
11+
)
512
from .exceptions import VectorError, VectorOperationError, VectorInitializationError

can/interfaces/vector/canlib.py

+103-37
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,12 @@ def _find_global_channel_idx(
295295
if serial is not None:
296296
hw_type: Optional[xldefine.XL_HardwareType] = None
297297
for channel_config in channel_configs:
298-
if channel_config.serialNumber != serial:
298+
if channel_config.serial_number != serial:
299299
continue
300300

301-
hw_type = xldefine.XL_HardwareType(channel_config.hwType)
302-
if channel_config.hwChannel == channel:
303-
return channel_config.channelIndex
301+
hw_type = xldefine.XL_HardwareType(channel_config.hw_type)
302+
if channel_config.hw_channel == channel:
303+
return channel_config.channel_index
304304

305305
if hw_type is None:
306306
err_msg = f"No interface with serial {serial} found."
@@ -331,7 +331,7 @@ def _find_global_channel_idx(
331331

332332
# check if channel is a valid global channel index
333333
for channel_config in channel_configs:
334-
if channel == channel_config.channelIndex:
334+
if channel == channel_config.channel_index:
335335
return channel
336336

337337
raise CanInitializationError(
@@ -727,26 +727,28 @@ def _detect_available_configs() -> List[AutoDetectedConfig]:
727727
LOG.info("Found %d channels", len(channel_configs))
728728
for channel_config in channel_configs:
729729
if (
730-
not channel_config.channelBusCapabilities
730+
not channel_config.channel_bus_capabilities
731731
& xldefine.XL_BusCapabilities.XL_BUS_ACTIVE_CAP_CAN
732732
):
733733
continue
734734
LOG.info(
735-
"Channel index %d: %s", channel_config.channelIndex, channel_config.name
735+
"Channel index %d: %s",
736+
channel_config.channel_index,
737+
channel_config.name,
736738
)
737739
configs.append(
738740
{
739741
# data for use in VectorBus.__init__():
740742
"interface": "vector",
741-
"channel": channel_config.hwChannel,
742-
"serial": channel_config.serialNumber,
743+
"channel": channel_config.hw_channel,
744+
"serial": channel_config.serial_number,
743745
# data for use in VectorBus.set_application_config():
744-
"hw_type": channel_config.hwType,
745-
"hw_index": channel_config.hwIndex,
746-
"hw_channel": channel_config.hwChannel,
746+
"hw_type": channel_config.hw_type,
747+
"hw_index": channel_config.hw_index,
748+
"hw_channel": channel_config.hw_channel,
747749
# additional information:
748750
"supports_fd": bool(
749-
channel_config.channelCapabilities
751+
channel_config.channel_capabilities
750752
& xldefine.XL_ChannelCapabilities.XL_CHANNEL_FLAG_CANFD_ISO_SUPPORT
751753
),
752754
"vector_channel_config": channel_config,
@@ -875,22 +877,53 @@ def set_timer_rate(self, timer_rate_ms: int) -> None:
875877
self.xldriver.xlSetTimerRate(self.port_handle, timer_rate_10us)
876878

877879

880+
class VectorCanParams(NamedTuple):
881+
bitrate: int
882+
sjw: int
883+
tseg1: int
884+
tseg2: int
885+
sam: int
886+
output_mode: xldefine.XL_OutputMode
887+
can_op_mode: xldefine.XL_CANFD_BusParams_CanOpMode
888+
889+
890+
class VectorCanFdParams(NamedTuple):
891+
bitrate: int
892+
data_bitrate: int
893+
sjw_abr: int
894+
tseg1_abr: int
895+
tseg2_abr: int
896+
sam_abr: int
897+
sjw_dbr: int
898+
tseg1_dbr: int
899+
tseg2_dbr: int
900+
output_mode: xldefine.XL_OutputMode
901+
can_op_mode: xldefine.XL_CANFD_BusParams_CanOpMode
902+
903+
904+
class VectorBusParams(NamedTuple):
905+
bus_type: xldefine.XL_BusTypes
906+
can: VectorCanParams
907+
canfd: VectorCanFdParams
908+
909+
878910
class VectorChannelConfig(NamedTuple):
879911
"""NamedTuple which contains the channel properties from Vector XL API."""
880912

881913
name: str
882-
hwType: xldefine.XL_HardwareType
883-
hwIndex: int
884-
hwChannel: int
885-
channelIndex: int
886-
channelMask: int
887-
channelCapabilities: xldefine.XL_ChannelCapabilities
888-
channelBusCapabilities: xldefine.XL_BusCapabilities
889-
isOnBus: bool
890-
connectedBusType: xldefine.XL_BusTypes
891-
serialNumber: int
892-
articleNumber: int
893-
transceiverName: str
914+
hw_type: xldefine.XL_HardwareType
915+
hw_index: int
916+
hw_channel: int
917+
channel_index: int
918+
channel_mask: int
919+
channel_capabilities: xldefine.XL_ChannelCapabilities
920+
channel_bus_capabilities: xldefine.XL_BusCapabilities
921+
is_on_bus: bool
922+
connected_bus_type: xldefine.XL_BusTypes
923+
bus_params: VectorBusParams
924+
serial_number: int
925+
article_number: int
926+
transceiver_name: str
894927

895928

896929
def _get_xl_driver_config() -> xlclass.XLdriverConfig:
@@ -907,6 +940,38 @@ def _get_xl_driver_config() -> xlclass.XLdriverConfig:
907940
return driver_config
908941

909942

943+
def _read_bus_params_from_c_struct(bus_params: xlclass.XLbusParams) -> VectorBusParams:
944+
return VectorBusParams(
945+
bus_type=xldefine.XL_BusTypes(bus_params.busType),
946+
can=VectorCanParams(
947+
bitrate=bus_params.data.can.bitRate,
948+
sjw=bus_params.data.can.sjw,
949+
tseg1=bus_params.data.can.tseg1,
950+
tseg2=bus_params.data.can.tseg2,
951+
sam=bus_params.data.can.sam,
952+
output_mode=xldefine.XL_OutputMode(bus_params.data.can.outputMode),
953+
can_op_mode=xldefine.XL_CANFD_BusParams_CanOpMode(
954+
bus_params.data.can.canOpMode
955+
),
956+
),
957+
canfd=VectorCanFdParams(
958+
bitrate=bus_params.data.canFD.arbitrationBitRate,
959+
data_bitrate=bus_params.data.canFD.dataBitRate,
960+
sjw_abr=bus_params.data.canFD.sjwAbr,
961+
tseg1_abr=bus_params.data.canFD.tseg1Abr,
962+
tseg2_abr=bus_params.data.canFD.tseg2Abr,
963+
sam_abr=bus_params.data.canFD.samAbr,
964+
sjw_dbr=bus_params.data.canFD.sjwDbr,
965+
tseg1_dbr=bus_params.data.canFD.tseg1Dbr,
966+
tseg2_dbr=bus_params.data.canFD.tseg2Dbr,
967+
output_mode=xldefine.XL_OutputMode(bus_params.data.canFD.outputMode),
968+
can_op_mode=xldefine.XL_CANFD_BusParams_CanOpMode(
969+
bus_params.data.canFD.canOpMode
970+
),
971+
),
972+
)
973+
974+
910975
def get_channel_configs() -> List[VectorChannelConfig]:
911976
"""Read channel properties from Vector XL API."""
912977
try:
@@ -919,22 +984,23 @@ def get_channel_configs() -> List[VectorChannelConfig]:
919984
xlcc: xlclass.XLchannelConfig = driver_config.channel[i]
920985
vcc = VectorChannelConfig(
921986
name=xlcc.name.decode(),
922-
hwType=xldefine.XL_HardwareType(xlcc.hwType),
923-
hwIndex=xlcc.hwIndex,
924-
hwChannel=xlcc.hwChannel,
925-
channelIndex=xlcc.channelIndex,
926-
channelMask=xlcc.channelMask,
927-
channelCapabilities=xldefine.XL_ChannelCapabilities(
987+
hw_type=xldefine.XL_HardwareType(xlcc.hwType),
988+
hw_index=xlcc.hwIndex,
989+
hw_channel=xlcc.hwChannel,
990+
channel_index=xlcc.channelIndex,
991+
channel_mask=xlcc.channelMask,
992+
channel_capabilities=xldefine.XL_ChannelCapabilities(
928993
xlcc.channelCapabilities
929994
),
930-
channelBusCapabilities=xldefine.XL_BusCapabilities(
995+
channel_bus_capabilities=xldefine.XL_BusCapabilities(
931996
xlcc.channelBusCapabilities
932997
),
933-
isOnBus=bool(xlcc.isOnBus),
934-
connectedBusType=xldefine.XL_BusTypes(xlcc.connectedBusType),
935-
serialNumber=xlcc.serialNumber,
936-
articleNumber=xlcc.articleNumber,
937-
transceiverName=xlcc.transceiverName.decode(),
998+
is_on_bus=bool(xlcc.isOnBus),
999+
bus_params=_read_bus_params_from_c_struct(xlcc.busParams),
1000+
connected_bus_type=xldefine.XL_BusTypes(xlcc.connectedBusType),
1001+
serial_number=xlcc.serialNumber,
1002+
article_number=xlcc.articleNumber,
1003+
transceiver_name=xlcc.transceiverName.decode(),
9381004
)
9391005
channel_list.append(vcc)
9401006
return channel_list

doc/interfaces/vector.rst

+24
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ Miscellaneous
5959
:show-inheritance:
6060
:class-doc-from: class
6161

62+
.. autoclass:: can.interfaces.vector.canlib.VectorBusParams
63+
:show-inheritance:
64+
:class-doc-from: class
65+
66+
.. autoclass:: can.interfaces.vector.canlib.VectorCanParams
67+
:show-inheritance:
68+
:class-doc-from: class
69+
70+
.. autoclass:: can.interfaces.vector.canlib.VectorCanFdParams
71+
:show-inheritance:
72+
:class-doc-from: class
73+
6274
.. autoclass:: can.interfaces.vector.xldefine.XL_HardwareType
6375
:show-inheritance:
6476
:member-order: bysource
@@ -83,6 +95,18 @@ Miscellaneous
8395
:members:
8496
:undoc-members:
8597

98+
.. autoclass:: can.interfaces.vector.xldefine.XL_OutputMode
99+
:show-inheritance:
100+
:member-order: bysource
101+
:members:
102+
:undoc-members:
103+
104+
.. autoclass:: can.interfaces.vector.xldefine.XL_CANFD_BusParams_CanOpMode
105+
:show-inheritance:
106+
:member-order: bysource
107+
:members:
108+
:undoc-members:
109+
86110
.. autoclass:: can.interfaces.vector.xldefine.XL_Status
87111
:show-inheritance:
88112
:member-order: bysource

test/test_vector.py

+44-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
VectorOperationError,
2323
VectorChannelConfig,
2424
)
25+
from can.interfaces.vector import VectorBusParams, VectorCanParams, VectorCanFdParams
2526
from test.config import IS_WINDOWS
2627

2728
XLDRIVER_FOUND = canlib.xldriver is not None
@@ -604,18 +605,49 @@ def test_winapi_availability() -> None:
604605

605606
def test_vector_channel_config_attributes():
606607
assert hasattr(VectorChannelConfig, "name")
607-
assert hasattr(VectorChannelConfig, "hwType")
608-
assert hasattr(VectorChannelConfig, "hwIndex")
609-
assert hasattr(VectorChannelConfig, "hwChannel")
610-
assert hasattr(VectorChannelConfig, "channelIndex")
611-
assert hasattr(VectorChannelConfig, "channelMask")
612-
assert hasattr(VectorChannelConfig, "channelCapabilities")
613-
assert hasattr(VectorChannelConfig, "channelBusCapabilities")
614-
assert hasattr(VectorChannelConfig, "isOnBus")
615-
assert hasattr(VectorChannelConfig, "connectedBusType")
616-
assert hasattr(VectorChannelConfig, "serialNumber")
617-
assert hasattr(VectorChannelConfig, "articleNumber")
618-
assert hasattr(VectorChannelConfig, "transceiverName")
608+
assert hasattr(VectorChannelConfig, "hw_type")
609+
assert hasattr(VectorChannelConfig, "hw_index")
610+
assert hasattr(VectorChannelConfig, "hw_channel")
611+
assert hasattr(VectorChannelConfig, "channel_index")
612+
assert hasattr(VectorChannelConfig, "channel_mask")
613+
assert hasattr(VectorChannelConfig, "channel_capabilities")
614+
assert hasattr(VectorChannelConfig, "channel_bus_capabilities")
615+
assert hasattr(VectorChannelConfig, "is_on_bus")
616+
assert hasattr(VectorChannelConfig, "bus_params")
617+
assert hasattr(VectorChannelConfig, "connected_bus_type")
618+
assert hasattr(VectorChannelConfig, "serial_number")
619+
assert hasattr(VectorChannelConfig, "article_number")
620+
assert hasattr(VectorChannelConfig, "transceiver_name")
621+
622+
623+
def test_vector_bus_params_attributes():
624+
assert hasattr(VectorBusParams, "bus_type")
625+
assert hasattr(VectorBusParams, "can")
626+
assert hasattr(VectorBusParams, "canfd")
627+
628+
629+
def test_vector_can_params_attributes():
630+
assert hasattr(VectorCanParams, "bitrate")
631+
assert hasattr(VectorCanParams, "sjw")
632+
assert hasattr(VectorCanParams, "tseg1")
633+
assert hasattr(VectorCanParams, "tseg2")
634+
assert hasattr(VectorCanParams, "sam")
635+
assert hasattr(VectorCanParams, "output_mode")
636+
assert hasattr(VectorCanParams, "can_op_mode")
637+
638+
639+
def test_vector_canfd_params_attributes():
640+
assert hasattr(VectorCanFdParams, "bitrate")
641+
assert hasattr(VectorCanFdParams, "data_bitrate")
642+
assert hasattr(VectorCanFdParams, "sjw_abr")
643+
assert hasattr(VectorCanFdParams, "tseg1_abr")
644+
assert hasattr(VectorCanFdParams, "tseg2_abr")
645+
assert hasattr(VectorCanFdParams, "sam_abr")
646+
assert hasattr(VectorCanFdParams, "sjw_dbr")
647+
assert hasattr(VectorCanFdParams, "tseg1_dbr")
648+
assert hasattr(VectorCanFdParams, "tseg2_dbr")
649+
assert hasattr(VectorCanFdParams, "output_mode")
650+
assert hasattr(VectorCanFdParams, "can_op_mode")
619651

620652

621653
# *****************************************************************************

0 commit comments

Comments
 (0)