Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming writes to slave causes write error #29

Open
greengiant83 opened this issue Apr 7, 2014 · 1 comment
Open

Streaming writes to slave causes write error #29

greengiant83 opened this issue Apr 7, 2014 · 1 comment

Comments

@greengiant83
Copy link

If I stream writes from the master (Raspberry pi) to a slave (Arduino uno) I end up with a [Error: Cannot write to device] error on the master side if the delayBetweenPackets is to low. It doesnt seem to be consistent, but at around 50ms between packets I can normally get away with streaming data. At 10 ms between packets I can only get 11 packets sent before the error.

Is there a way to tell when bus is ready for another packet or will I just need to calibrate this delay between packets by trial and error?

Here is the test script running on the pi

var delayBetweenPackets = 50;
var numberOfPackets = 100;

var i2c = require('i2c');
var address = 0x18;
var wire = new i2c(address, {device: '/dev/i2c-1', debug: false}); // point to your i2c address, debug provides REPL interface
var i = 0;

sendPacket();

function sendPacket()
{
    var buf = new Buffer(4);
    buf.writeInt32LE(i, 0, true);

    wire.writeBytes(0x0, buf, function(err)
    {
        if(!err && i < numberOfPackets) 
        {
            i++;
            setTimeout(sendPacket, delayBetweenPackets);
        }
        else console.log("writeBytes result: ", err);
    });
}

and here is the code running on the Arduino

#include <Wire.h>
void setup()
{
  Wire.begin(0x18);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output

  Serial.println("Listening for i2c com");
}

void loop()
{
  delay(100);
}

void receiveEvent(int howMany)
{ 
  if(howMany >= 5)
  {
    Wire.read(); //first byte is the "command byte" and I don't what to do with it
    long value = (long)Wire.read();
    value += (long)Wire.read() << 8;
    value += (long)Wire.read() << 16;
    value += (long)Wire.read() << 24;
    Serial.print("Long value: ");
    Serial.println(value);
  }
}
@rzr
Copy link

rzr commented Oct 20, 2020

Is this issue still relevant on latest @abandonware's fork ?

https://libraries.io/npm/@abandonware%2Fi2c/usage

Relate-to: #97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants