MyTooliT / ICOc

ICOc is a tool to control the ICOtronic system, acquire data, and test stationary transceiver units and sensory tool holders.
https://mytoolit.github.io/ICOc/
2 stars 0 forks source link

IndexError when opening DataStreamContextManager multiple times inside a loop. #41

Closed chriMay closed 1 year ago

chriMay commented 1 year ago

Issue appeared when I tried to open a DataStreamContextManager multiple times in a for loop and trying to read data from the data stream instead of starting the loop inside the ContextManager (see read_data() in line 24 in the attachment)

Reproduction: Connect to device -> opening DataStreamContextManager in a loop at least 7 times -> each time try to read a minimal amount of data from the data stream! (A minimal script that produces the error can be found as attachement.)

Expected Output: No Error Messages icoc_test.py.zip

sanssecours commented 1 year ago

Thank you for the bug report Christoph 💖. I could not reproduce this bug with the latest version of ICOc. At least your script worked on a

machine, after I replaced the MAC address with the one of my test STH:

from asyncio import sleep, run, TaskGroup
from netaddr import EUI

from mytoolit.can import Network
from mytoolit.can.network import NetworkError
from mytoolit.can.streaming import StreamingData
from mytoolit.measurement import convert_raw_to_g
from mytoolit.measurement.storage import Storage

class ChrisNetwork:
    def __init__(self):
        self.device = EUI("08-6B-D7-01-DE-81")
        self.network = Network()

    async def connect(self):
        try:
            await self.network.connect_sensor_device(self.device)
            print("Connected to sensor device")
        except NetworkError:
            print("NetworkError")
        print("success")

    async def read_data(self):
        for i in range(7):
            async with self.network.open_data_stream(first=True) as stream:
                print("Reading data from sensor!")
                # stream_data = StreamingData()
                print(f"---{i}---")
                # async for data in stream:
                #    stream_data.extend(data)
                #    print(f"---{i}---")
                #    break

async def main(my_network: ChrisNetwork):
    await my_network.connect()
    await my_network.read_data()
    print("Shutting down!")
    await my_network.network.shutdown()
    print("Finish!")

my_network = ChrisNetwork()
run(main(my_network))

Steps to Reproduce

  1. Install ICOc

    pip install icoc
  2. Copy the script above into a file called test.py

  3. Run the script

    python3.11 test.py

The output was:

Connected to sensor device
success
Reading data from sensor!
---0---
Reading data from sensor!
---1---
Reading data from sensor!
---2---
Reading data from sensor!
---3---
Reading data from sensor!
---4---
Reading data from sensor!
---5---
Reading data from sensor!
---6---
Shutting down!
Finish!