kelly / node-i2c

Node.js native bindings for i2c-dev. Plays well with Raspberry Pi and Beaglebone.
Other
216 stars 91 forks source link

Streaming writes to slave causes write error #29

Open greengiant83 opened 10 years ago

greengiant83 commented 10 years ago

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 commented 3 years ago

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

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

Relate-to: https://github.com/kelly/node-i2c/issues/97