Multiple I2C-bus-read with BinaryStream

Hello everybody,

I want to read out continous a via I2C-Bus connected sensor on a Raspberry Pi, using the GPIO-Lib and a BinaryStream. But after the timer was executed a few hundred times, the whole application quits. I think the reason is that i call every time the gpio.I2CSetup-function to get the handle. Or is this wrong way to do this?

Code in the Timer (executed every two seconds):

dim DataBuffer as String
' Get handle
in_handle(10)=gpio.I2CSetup(8)
' Read 11 Bytes via BineryStream-Method
Dim I2CStream As BinaryStream
I2CStream = New BinaryStream(in_handle(10),BinaryStream.HandleTypeFileNumber)
DataBuffer = I2CStream.Read(11)

I tried to solve this like this:

  • Create a global Variable in_handle() in a module
  • Call in_handle(10)=gpio.I2CSetup(8) only at startup once

But then i can only read the bytes one time, when second time the timer is executed i only receive 0’s.
So my question: How to do it correctly? Maybe there is a solution to “reset” the BinaryStream or to destroy the handle.

Greetings,
Marco

One question. Why in_handle(10)? Do you have 10 sensors?

I usually try to do what is minimally required inside the timer.
Create DataBuffer, in_handle, I2CStream as global variables and initialize them before starting the timer.
Then all the timer needs to do is read from the sensor:

DataBuffer = I2CStream.Read(11)

Hello John,

Yes, this is correct. I only use index 10 for my question to make it easier to understand.

[quote]I usually try to do what is minimally required inside the timer.
Create DataBuffer, in_handle, I2CStream as global variables and initialize them before starting the timer.
Then all the timer needs to do is read from the sensor:

DataBuffer = I2CStream.Read(11)[/quote]

I tried this (again now, to be sure i didn’t made a mistake):
At first time i get back correct data. But when the timer enters second time, i get back 11 times a 0xFF.

So how to “reset” a BinaryStream?

Are the other sensors active at the same time?
Each sensor should have it’s own BinaryStream, otherwise you would only read the last one created.
Create a global I2CStream() array to match in_handle()
Does the sensor need some type of ‘start’ or ‘restart’ signal?

No, just one sensor on the bus.

Please look at my first post, with this it is working but application crashes after lots of reads.

I did another test:

When i do only

I2CStream = New BinaryStream(in_handle(10),BinaryStream.HandleTypeFileNumber) DataBuffer = I2CStream.Read(11)

in the timer, at second time the timer fires the app stops and in debug-window i can see Event Loop in the Stack/Main-Window-Area

I saw the original post and what I suspect is the gpio connection or the binary stream aren’t properly cleaning up at the end of the timer call, resulting in a memory leak / crash after several hundred iterations.

My intention was to help you move those actions out of the timer since they should only be needed once.
The I2CSetup call may be resetting the sensor, so you get the first batch of data, but nothing after that.
Which is why I asked if there is a way to restart the sensor without resetting the gpio/I2C connection.

I have a temperature/humidity sensor using the same library that has been running for 90 days without a problem.

If your app is crashing, just use the UnhandledException event of the Application to save a log of the exception. Then you can debug.

Hello,

it worked when i did it private like this in one procedure:

Dim DataBuffer As MemoryBlock I2CStream = New BinaryStream(in_handle,BinaryStream.HandleTypeFileNumber) DataBuffer=I2CStream.Read(3) I2CStream=Nil

And then i can do this unlimited times.

Thanks a lot!