Skip to content

Commit

Permalink
Merge branch 'main' of github.com:supermete/caroa04
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoyding committed Apr 26, 2024
2 parents a3ab54b + 8e8fc6e commit c433252
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 69 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [ongoing] - 2024-04-26
### Changed
- Use notifier from can library to allow sharing the bus with other bus users, #7

### Fixed
- Fix communication when bus is set before starting , #5


## [0.1.2] - 2024-04-15
### Changed
- Move CanMessage/CanSignal inherited classes in canmessage.py. Delete travis yml and create mkdocs, #3
Expand Down
25 changes: 5 additions & 20 deletions src/caroa04/canmessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ def node_id(self):

@node_id.setter
def node_id(self, value):
self.read_id = (self.read_id & 0xFFFFFF00) | value
self.write_id = (self.write_id & 0xFFFFFF00) | value
self.read_id = (self.read_id & 0x700) | value
self.write_id = (self.write_id & 0x700) | value
self._node_id = value

def write(self):
Expand All @@ -379,14 +379,7 @@ def write(self):
is_extended_id=self.is_extended)
logging.debug(message)
self.bus.send(message)

self.bus.set_filters([{"can_id": self.write_id, "can_mask": 0x7ff, "extended": False}])
sts = self.bus.recv(5)
if sts is not None and sts.arbitration_id == self.write_id and sts.dlc > 0:
self.update_payload(sts.data)
else:
logging.warning('Could not get a response from the device')
self.bus.set_filters()
self.bus.recv(1)

def read(self):
"""
Expand All @@ -402,15 +395,7 @@ def read(self):
is_extended_id=self.is_extended)
logging.debug(message)
self.bus.send(message)

self.bus.set_filters([{"can_id": self.read_id, "can_mask": 0x7ff, "extended": False}])
sts = self.bus.recv(5)
if sts is not None and sts.arbitration_id == self.read_id and sts.dlc > 0:
logging.debug(sts)
self.update_payload(sts.data)
else:
logging.warning('Could not get a response from the device')
self.bus.set_filters()
self.bus.recv(1)


if __name__ == "__main__":
Expand All @@ -424,4 +409,4 @@ def read(self):
sig1.raw = 0xff

print(csm_fail.raw)
print(msg_3c2.payload)
print(msg_3c2.payload)
41 changes: 28 additions & 13 deletions src/caroa04/caroa04.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CaroA04:
def __init__(self):
self._node_id = DEFAULT_NODEID
self.bus = None
self.notifier = None

self.message_do = CanMessageRW(self._node_id, MSGID_DO_READ, MSGID_DO_WRITE, dlc=8)
self.message_di = CanMessageRW(self._node_id, MSGID_DI_READ, MSGID_DI_READ, dlc=8)
Expand Down Expand Up @@ -122,32 +123,46 @@ def start(self, node_id, interface, bitrate=None, channel=None):

if self.bus is None:
self.bus = can.Bus(interface=interface, channel=channel, bitrate=bitrate)
self.message_do.bus = self.bus
self.message_di.bus = self.bus
self.message_nodeid.bus = self.bus
self.message_bitrate.bus = self.bus
self.notifier = can.Notifier(self.bus, [self.listener], timeout=2.0)

self.message_do.bus = self.bus
self.message_di.bus = self.bus
self.message_nodeid.bus = self.bus
self.message_bitrate.bus = self.bus

def listener(self, msg):
if msg.arbitration_id in (self.message_do.read_id, self.message_do.write_id):
logging.debug(f"Response from caroA04> {msg}")
self.message_do.update_payload(msg.data)
elif msg.arbitration_id in (self.message_di.read_id, self.message_di.write_id):
logging.info(msg)
self.message_di.update_payload(msg.data)
elif msg.arbitration_id in (self.message_bitrate.read_id, self.message_bitrate.write_id):
logging.info(msg)
self.message_bitrate.update_payload(msg.data)
elif msg.arbitration_id in (self.message_nodeid.read_id, self.message_nodeid.write_id):
logging.info(msg)
self.message_nodeid.update_payload(msg.data)

def stop(self):
"""Stops any ongoing thread - unused"""
pass
"""Stops any ongoing thread"""
if self.notifier is not None:
self.notifier.stop()

def shutdown(self):
if self.bus is not None:
self.bus.shutdown() # free the port
self.bus = None
self.message_do.bus = None
self.message_di.bus = None
self.message_nodeid.bus = None
self.message_bitrate.bus = None
self.message_do.bus = None
self.message_di.bus = None
self.message_nodeid.bus = None
self.message_bitrate.bus = None


if __name__ == "__main__":
caro = CaroA04()
caro.start(0xE0, 'pcan', 250000, 'PCAN_USBBUS2')
print(caro.do1.phys)
caro.do1.phys = True
print(caro.do1.phys)
time.sleep(0.5)
caro.do1.phys = False
print(caro.do1.phys)
caro.shutdown()
64 changes: 28 additions & 36 deletions tests/test_caroa04.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import threading
import pytest
import can

Expand All @@ -12,9 +11,8 @@ class VirtualDevice:
It will respond to the messages sent by the library, and keep track of the signal's value to answer properly.
"""
def __init__(self):
self._bus = None
self._thread = threading.Thread()
self._stop_thread = threading.Event()
self.bus = None
self.notifier = None
self.message_do_set = CanMessage(MSGID_DO_WRITE)
self.message_do_get = CanMessage(MSGID_DO_READ)
self.message_di = CanMessage(MSGID_DI_READ)
Expand Down Expand Up @@ -57,42 +55,36 @@ def start(self, node_id):
self.message_do_get.arbitration_id = (self.message_do_get.arbitration_id & 0x700) | node_id
self.message_di.arbitration_id = (self.message_di.arbitration_id & 0x700) | node_id

if self._bus is None:
self._bus = can.interface.Bus(interface='virtual')
if not self._thread.is_alive():
self._stop_thread.clear()
self.__thread = threading.Thread(target=self._emit, daemon=True)
self.__thread.start()

def _emit(self):
while not self._stop_thread.is_set():
sts = self._bus.recv(1)
if sts is not None:
if sts.arbitration_id == self.message_do_get.arbitration_id:
# read request received, respond with the last values set with message_do_set
self._bus.send(can.Message(arbitration_id=self.message_do_get.arbitration_id,
data=self.message_do_set.payload,
is_extended_id=False))
elif sts.arbitration_id == self.message_do_set.arbitration_id:
# write request received, respond with empty message
self.message_do_get.update_payload(sts.data)
self._bus.send(can.Message(arbitration_id=self.message_do_set.arbitration_id,
data=sts.data,
is_extended_id=False))
elif sts.arbitration_id == self.message_di.arbitration_id:
# read DI request received, respond with the DI message
self._bus.send(can.Message(arbitration_id=self.message_di.arbitration_id,
data=self.message_di.payload,
is_extended_id=False))
if self.bus is None:
self.bus = can.interface.Bus(interface='virtual')
self.notifier = can.Notifier(self.bus, [self.listener], timeout=2.0)

def listener(self, msg):
if msg.arbitration_id == self.message_do_get.arbitration_id:
# read request received, respond with the last values set with message_do_set
self.bus.send(can.Message(arbitration_id=self.message_do_get.arbitration_id,
data=self.message_do_set.payload,
is_extended_id=False))
elif msg.arbitration_id == self.message_do_set.arbitration_id:
# write request received, respond with empty message
self.message_do_get.update_payload(msg.data)
self.bus.send(can.Message(arbitration_id=self.message_do_set.arbitration_id,
data=msg.data,
is_extended_id=False))
elif msg.arbitration_id == self.message_di.arbitration_id:
# read DI request received, respond with the DI message
self.bus.send(can.Message(arbitration_id=self.message_di.arbitration_id,
data=self.message_di.payload,
is_extended_id=False))

def stop(self):
self._stop_thread.set()
if self._thread.is_alive():
self._thread.join()
if self.notifier is not None:
self.notifier.stop()

def shutdown(self):
self._bus.shutdown()
self._bus = None
if self.bus is not None:
self.bus.shutdown()
self.bus = None


class TestVirtualCanIoExp1:
Expand Down

0 comments on commit c433252

Please sign in to comment.