使用nodejs缓冲区的rpio软件包始终获得0值

时间:2018-10-08 08:26:19

标签: javascript node.js buffer

我正在尝试使用i2c和npm软件包SI7021rpio(第18页)传感器中读取一个值。但是我的确收到了此代码返回的0值,但看不到错误的部分...

我正在使用节点v10.11.0,因此正在使用“新” Buffer api。我会用错吗?

const rpio = require('rpio')

const SI7021_I2C_ADDR = 0x40
const CMD_MEASURE_RELATIVE_HUMIDITY = 0xF5

rpio.i2cBegin()
rpio.i2cSetSlaveAddress(SI7021_I2C_ADDR)
rpio.i2cSetBaudRate(100000) // 100kHz

rpio.i2cWrite(Buffer.from([CMD_MEASURE_RELATIVE_HUMIDITY])) // Send command to measure relative humidity (1 byte)
rpio.msleep(25)
const rhCode = rpio.i2cRead(Buffer.alloc(2)) // Read relative humidity code (2 bytes = 16 bit)
rpio.i2cEnd()

console.log('Humidity Code: ', rhCode) // Always return 0 :-(

我认为我在错误地使用i2cRead。我不了解example,因为它正在使用rpio.i2cRead(new Buffer(32), 16)来读取16个字节。我需要读取2个字节。

0 个答案:

没有答案