Skip to content

Commit

Permalink
Merge pull request #9 from supermete/improvement/merge_stop_methods
Browse files Browse the repository at this point in the history
Merge stop and shutdown methods. Update doc
  • Loading branch information
supermete authored Apr 26, 2024
2 parents c433252 + 373826f commit 9bfb738
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ You can install *caroa04* via [pip]() from [PyPI]():

## Usage

You can instantiate a CaroA04 object and start it to communicate with the device as follows.

``` python
from caroa04 import CaroA04

Expand All @@ -41,6 +43,25 @@ caro.node_id.phys = 0xE1 # set address code (will require device power cycle)
caro.shutdown() # free the bus
```

In order to share the bus with other participants, you can assign the bus attribute of the CaroA04 object before starting it.
You should simply then add CaroA04's listener to the existing bus' notifier.

``` python
import canopen
from caroa04 import CaroA04

nw = canopen.Network()
nw.connect(interface='pcan', bitrate=250000, channel='PCAN_USBBUS1')


caro = CaroA04()
caro.bus = nw.bus # use the bus already started by canopen
nw.notifier.add_listener(caro.listener) # add listener so that we can receive messages

caro.start(0xE0, 'pcan') # start communication
caro.shutdown() # free the bus
```

## Features

- This library uses the python-can library to communicate with the device. Please refer to its documentation to know about all the CAN interfaces that can be used with this library (https://python-can.readthedocs.io/en/stable/)
Expand Down
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ You can install "caroa04" via pip from PyPI:
Usage
-----

You can instantiate a CaroA04 object and start it to communicate with the device as follows.

.. code-block:: python
from caroa04 import CaroA04
Expand All @@ -60,6 +62,27 @@ Usage
..
In order to share the bus with other participants, you can assign the bus attribute of the CaroA04 object before starting it.
You should simply then add CaroA04's listener to the existing bus' notifier.

.. code-block:: python
import canopen
from caroa04 import CaroA04
nw = canopen.Network()
nw.connect(interface='pcan', bitrate=250000, channel='PCAN_USBBUS1')
caro = CaroA04()
caro.bus = nw.bus # use the bus already started by canopen
nw.notifier.add_listener(caro.listener) # add listener so that we can receive messages
caro.start(0xE0, 'pcan') # start communication
caro.shutdown() # free the bus
..
Features
--------

Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [ongoing] - 2024-04-26
### Changed
- Use notifier from can library to allow sharing the bus with other bus users, #7
- Merge stop and shutdown methods, to only have a stop method to be called when stopping, #8

### Fixed
- Fix communication when bus is set before starting , #5
Expand Down
3 changes: 0 additions & 3 deletions src/caroa04/caroa04.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ def stop(self):
"""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
Expand All @@ -165,4 +163,3 @@ def shutdown(self):
caro.do1.phys = True
print(caro.do1.phys)
caro.do1.phys = False
caro.shutdown()
4 changes: 0 additions & 4 deletions tests/test_caroa04.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ def listener(self, msg):
def stop(self):
if self.notifier is not None:
self.notifier.stop()

def shutdown(self):
if self.bus is not None:
self.bus.shutdown()
self.bus = None
Expand All @@ -105,9 +103,7 @@ def setup_teardown_class(self, caro, virtualdevice):
yield

caro.stop()
caro.shutdown()
virtualdevice.stop()
virtualdevice.shutdown()

def test_do1(self, caro, virtualdevice):
assert caro.do1.phys is False, "Initial value of DO1 is wrong"
Expand Down

0 comments on commit 9bfb738

Please sign in to comment.