Python Forum
Program running on RPi 3b+ Very Strange Behavior - Out of Bound Index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program running on RPi 3b+ Very Strange Behavior - Out of Bound Index
#9
When printing changes how things run, I first think of some sort of race condition. But you are waiting 5 seconds for a response, so that isn't likely. I think it more likely that printing didn't make any difference at all and it was just chance, and maybe tainted observation, that lead you to believe that printing was "fixing" the problem. That happens to me all the time. Your mind creates patterns regardless of the data. Sometimes this is insight and sometimes it is "That cloud looks like a bunny." You should really check the value retuned by the waitForNotifications(). If it is False, nothing was received.

I cannot test the code, so I am not surprised there are some bugs. This particular bug is a cut/paste error. This does not belong.
f'volts = {self.volts}',
I updated my original post.

I will try to describe the code.

You sublcassed bluepy.btle.DefaultDelegate to make your MyDelegate class. Then you provided a custom handleNotification() method to use when a notification is received. In addition to custom methods, you can also provide custom attributes. Instead of making a global variable and having the delegate copy data to the global variable, it makes more sense to have the delegate keep the data. That is what I was doing by defining a bunch of attributes in the __init__() method. When MyDelegate's handleNotification() method is called, it stores the notification data in the delegate's instance attributes.

I saw that elsewhere in your code you were publishing a dictionary. I thought it made sense to save the notification data as a dictionary. That is why I did this in the __init__() method.
        self.data = {
            "volts": None,
            "amps": None,
            "capacity": None,
            "remain": None,
            "cell1": None,
            "cell2": None,
            "cell3": None,
            "cell4": None,
        }
The data starts out as None, but as notifications are received, the dictionary is updated.

This is just a shorthand way of updating a dictionary.
            self.data.update(zip(
                ("volts", "amps", "capacity", "remain"),
                struct.unpack_from('>HHHH', data, 4)
))
It is the same as doing this:
volts, amps, capacity, remain= struct.unpack_from('>HHHH', data, 4)
self.data["volts"] = volts
self.data["amps"] = amps
self.data["capacity"] = capacity
self/data["remain"] = remain
And this is called unpacking:
volts, amps, capacity, remain= struct.unpack_from('>HHHH', data, 4)
The first value returned by struct.unpack_from() is assigned to volts, the second to amps, the third to capacity, and fourth to remain. Packing and unpacking are very useful in Python. Well worth mastering.

Let me know if you have more questions.
Reply


Messages In This Thread
RE: Program running on RPi 3b+ Very Strange Behavior - Out of Bound Index - by deanhystad - Mar-06-2023, 03:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 465 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  strange behavior of chess library in Python max22 1 422 Jan-18-2024, 06:35 PM
Last Post: deanhystad
  running a TensorFlow program Led_Zeppelin 0 961 Apr-07-2022, 06:33 PM
Last Post: Led_Zeppelin
  Strange write()/File behavior kaega2 2 1,776 Jan-28-2022, 02:53 AM
Last Post: kaega2
  Python Program running a lot slower after change to Ubuntu hubenhau 1 3,004 Mar-02-2021, 05:01 PM
Last Post: Serafim
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,148 Nov-13-2020, 07:43 AM
Last Post: buran
  Running Python 2.7 program ErnestTBass 2 2,876 Oct-21-2020, 08:06 AM
Last Post: snippsat
  read terminal text from running program AArdvark_UK 2 1,961 Aug-27-2020, 12:43 PM
Last Post: AArdvark_UK
  Modify code from running program ? samuelbachorik 2 2,517 Jun-26-2020, 08:17 PM
Last Post: samuelbachorik
  Upper-Bound Exclusive Meaning Johnny1998 1 3,474 Aug-02-2019, 08:32 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020