Hello,

I am looking for advice on an approach that I can use to solve this problem:

I have a Perl script that runs an lcd display over i2c. This runs continuously, reading and displaying new data from a text file. This runs fine and is stable over many months.

I have a second perl script that reads volts & amps from a device (INA219) that is also connected over i2c. This runs fine when run on a different machine (both Raspberry Pi's).

But ... when both scripts are run on the same machine, the lcd screen gets garbled and some of the amps/volts data is wrong. It seems that the underlying i2c driver does not handle contention. Both scripts use HiPi::Device::I2C.

I have overcome the problem by using time slots.

The amps/volts device can read/write at 0, 5, 10, 15 seconds etc. after each minute and the lcd script can write at (1,2,3) (6,7,8) (11,12,13) seconds etc. after each minute. The data from the amps/volts device is fine and the lcd display is stable using this scheme.

But this is an ugly fix, so I am looking for alternatives.

I looked at using a common module with the aim of passing read/write requests from each script to the module, which would then handle them sequentially, so no contention.

Using two small test scripts and a module I wrote (i2cAccess.pm) I found, perhaps not unexpectedly, that each script loads or uses a separate copy of the module. so there is no common access to the i2c bus. I can post the two scripts and the module if it would be of any use.

I considered having a lock file that each script accesses, to control access to the i2c bus, but that seems like a lot of overhead, probably worse that my time slot solution.

I also setup a test using IPC::Shareable

The aim being to pass the i2c objects, register information etc. to the 'server' program. This failed because, as far as I can tell, Shareable does not support objects. I found a reference to this at https://www.nntp.perl.org/group/perl.poe/2009/12/msg4706.html which says 'Storable can't serialize filehandles'. When adding an i2c object to a shared hash I get 'Can't store GLOB items at ... /IPC/Shareable.pm. Again I can post the test scripts I used if that would be helpful.

Can the monks suggest an approach to handling this issue.

Thank you for your consideration.


In reply to i2c bus contention by anita2R

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.