【问题标题】:Intel Edison Arduino breakout board: How do I recieve all of the i2c result bytes?(NodeJS)英特尔 Edison Arduino 分线板:如何接收所有 i2c 结果字节?(NodeJS)
【发布时间】:2014-12-25 04:36:42
【问题描述】:

我有一个英特尔 Edison Arduino 分线板、Atlas Scientific EZO PH 传感器和一个 Atlas Scientific 电源隔离器。

我在 Intel Edison Arduino 分线板 i2c 总线 (A4/A5) 和 ph 传感器之间安装了电源隔离器。

可在此处找到 Atlas Scientific 数据表:
EZO Ph 传感器:PH Data-sheet
电源隔离器:Power Isolator Data-sheet

NodeJS 代码:

var m = require('mraa');  
var i2c = new m.I2c(1);  

i2c.address(0x63);  
i2c.write("R,56.26");  

console.log("Reading I2C..");  

function readPH() {  
    var data = i2c.read();  
    console.log( ">> I2C value: " + data);  
}  
setTimeout(function (e) { readPH(); }, 1000);

我将命令 R,56.26 发送到 ph 传感器,等待 1 秒然后执行 i2c.read();

当发送命令和执行i2c.read() 时,我看到灯闪烁并改变颜色,所以我知道我正在请求读取并获得结果。

EZO PH 传感器数据表说明:

我似乎只能存储我的结果的第一位,而不是完整的 7 个字节。我似乎总是返回 1。我收到的 1 对应于“成功”。为了进一步排除故障,我将 R,34.53 发送到 ph 传感器,然后提前执行 i2c.read(); 并收到对应于“待处理”的 254。所以,我相信我收到了来自 EZO PH 传感器的结果或部分结果。

我不知道如何存储 7 字节结果。我不知道 mraa nodejs 库 API 的内部工作原理。所以,我不确定是否应该使用对象、数组或变量来存储结果,或者我是否在i2c.read(); 中缺少参数

更新

我通读了 mraa github 示例部分,其中解释说:“没有明确的 nodejs API 文档,请参阅示例。API 本身与 python 非常相似,但具有 js 语法。”因此,我编写了一个相当简单的 python 脚本,可以成功获取 Ph Sensor 值。

Python 代码:

import time
import mraa

i2c = mraa.I2c(1)
i2c.address(0x63)

i2c.write("R,23.5")

time.sleep(1.3)
d = "       "
i2c.read(d)
print(d)

python 代码输出:2.974 获得结果证明我的电路设计有效,现在我需要弄清楚 NodeJS API 是什么才能成功读取 i2c。有谁知道用于获取 i2c 读数的 NodeJS API?

更新
工作示例代码:

var m = require('mraa');
var i2c = new m.I2c(1);
i2c.address(0x63);
i2c.write("R,56.26");
console.log("Reading I2C..");
function readPH() {
    var d = i2c.read(7);
    console.log(">> " + d);
}
setTimeout(function (e) { readPH(); }, 1000);

【问题讨论】:

    标签: node.js intel-edison


    【解决方案1】:

    'i2c.read();'不读取一系列数据,而只读取一个字节的数据。 您的代码使用 'i2c.read();'一次,所以你只有一个字节。

    要读取所有数据直到NULL到来,readPH函数可能需要修改如下。 (此代码未测试,因为我没有arduino)

    function readPH() {  
        char readout[7]; //
        char aChar;
        int i;
        for (i=0; i<7; i++) {
            aChar = i2c.read();
            readout[i]=aChar;
            if (aChar== NULL){ // all data have been read.
                console.log( ">> I2C value: " + readout);  
            }
        }
     }
    

    【讨论】:

    • @ Fumu 7 你的代码引发了顿悟。我能够得到一个结果。请参阅我的原始帖子以获取我的工作示例代码。
    【解决方案2】:

    正如@Fumu 7 所解释的,i2c.read() 不读取一系列数据,而只读取一个字节的数据。 i2c.read(7) 将读取 7 个字节的流。
    工作示例代码:

    var m = require('mraa');
    var i2c = new m.I2c(1);
    i2c.address(0x63);
    i2c.write("R,56.26");
    console.log("Reading I2C..");
    function readPH() {
        var d = i2c.read(7);
        console.log(">> " + d);
    }
    setTimeout(function (e) { readPH(); }, 1000);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多