kelly / node-i2c

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

readBytes method #50

Closed wakatanka closed 9 years ago

wakatanka commented 9 years ago

Hi, i'm trying to read temperature from a sensor (sht21) with a raspberry pi, here is my working code:

var i2c = require('i2c'); var address = 0x40; var wire = new i2c(address, {device: '/dev/i2c-1',debug: false});

wire.scan(function(err, data) { // result contains an array of addresses if (err !== null) { console.log('error scan: ' + err); } else { console.log("scan data: "+data); } });

wire.writeByte(0xF3, function(err) { if (err !== null) { console.log("error write: " + err); } else { console.log("write ok"); } });

wire.readByte(function(err, res) { if (err !== null) { console.log("error readByte: " + err); } else { console.log(res); } })

this works flawless, but i need to read the first 3 bytes to get all data, so i will use readbytes method with this code:

wire.readBytes(0x00, 3, function(err, res) { // result contains a buffer of bytes //var value = res.readUInt8(0); if (err !== null) { console.log("readBytes: " + err); } else { console.log(res); } });

but it gives me an error. Probably i didn't understand what i have to pass as command parameter.

kelly commented 9 years ago

I think you want to do wire.readBytes(0xF3, 3, function .... The initial write that you're doing is the command byte. That's telling your device that you want to receive 3 bytes of data.

dsteinborn commented 9 years ago

Hi @wakatanka I'm also using the sh21t sensor with a raspberry pi. Have a look at pull request #35 from @bbx10 which contains a fix to read bytes without sending the command/register parameter. This fix also works for sh21t sensor.

bbx10 commented 9 years ago

I closed pull request #35 because the same functionality is available using plain read. For example, wire.read(3, function...). My fork is obsolete since @kib357 added plain I2C read and write.