简体   繁体   English

循环节点js中的回调函数

[英]looping the callback function in node js

This is a piece of code which writes data to a ble device and reads data from it. 这是一段将数据写入ble设备并从中读取数据的代码。 data is written to the device in the form of a buffer. 数据以缓冲区的形式写入设备。 the value in 'mydata' (AAAD0000) is the command to be written in order to read the data. “ mydata”(AAAD0000)中的值是为了读取数据而要写入的命令。

function named chara3() consists of write and read function which is a callback function in which the command is passed read back. 名为chara3()的函数由write和read函数组成,后者是一个回调函数,在该回调函数中以回读的方式传递命令。

My requirement is the 'mydata' value which i said earlier, the last two zeros is the memory address. 我的要求是我前面说的“ mydata”值,后两个零是内存地址。 i need to read the data in different memory addresses starting from zero to 59. That is AAAD0000 to AAAD0059. 我需要读取从零到59的不同内存地址中的数据。即AAAD0000到AAAD0059。 so of course i need to run a loop. 因此,我当然需要运行一个循环。 If I'm reading the zeroth location, the code is quite fine and i got the output as well but when i tried to make it inside a loop, the code is all a mess. 如果我正在读取第零个位置,那么代码就很好,我也得到了输出,但是当我试图将其放入循环中时,代码全都是一团糟。 the read part is not executing. 读取的部分未执行。

can any one suggest a better way to read data from zeroth memory location to 59th memory location (AAAD0000 to AAAD0059)??? 有谁能建议一种更好的方法来从第零个存储位置读取数据到第59个存储位置(AAAD0000至AAAD0059)???

first command writes to it then reads data memory location incremented by 1 this should repeat up to 59 第一个命令向其写入,然后读取以1递增的数据存储位置,这应重复最多59

        var mydata = 'AAAD0000';
        function chara3() {
          var buff2 = new Buffer(mydata, 'hex');
          SensorCharacteristic.write(buff2, false, function(error) { //[0x002d]
            console.log('Writing command  SUCCESSFUL',mydata);
            if (!error) {
              SensorCharacteristic.read((error, data) => {
                console.log("i just entered");
                if (data.toString('hex') != '0000') {
                  console.log('Temperature History: ', data.toString('hex'));
                  enter();
                }
                else {
                  console.log('contains null value');
                } //else
              });
            }
            function enter()
            {
            mydata = (parseInt(mydata, 16) + 00000001).toString(16);

            }
          }); //.write

        } //chara3

there's no error. 没有错误。 But some part of the code is not executing. 但是代码的某些部分没有执行。

You can use the recursion by wrapping your methods into a promise 您可以通过将方法包装到Promise中来使用递归

async function chara3(mydata = 'AAAD0000') {
    if (mydata === 'AAAD0059') {
        return;
    }
    var buff2 = new Buffer(mydata, 'hex');
    return new Promise((resolve) => {
        SensorCharacteristic.write(buff2, false, function (error) { //[0x002d]
            console.log('Writing command  SUCCESSFUL', mydata);
            if (!error) {
                SensorCharacteristic.read(async (error, data) => {
                    console.log("i just entered");
                    if (data.toString('hex') != '0000') {
                        console.log('Temperature History: ', data.toString('hex'));
                        let next = await chara3(enter())
                        return resolve(next);
                    }
                    else {
                        console.log('contains null value');
                        return resolve();
                    } //else
                });
            }
        }); //.write
    });
} //chara3

function enter() {
    return (parseInt(mydata, 16) + 00000001).toString(16);
}

Also if you can convert your methods SensorCharacteristic.write and SensorCharacteristic.read into promises you can simply map 另外,如果您可以将方法SensorCharacteristic.writeSensorCharacteristic.read转换为SensorCharacteristic.write ,则只需映射即可

  function chara3(mydata) {
        var buff2 = new Buffer(mydata, 'hex');
        await SensorCharacteristic.write(buff2, false);
        console.log('Writing command  SUCCESSFUL', mydata);
        let data = await SensorCharacteristic.read();
        if (data.toString('hex') != '0000') {
            console.log('Temperature History: ', data.toString('hex'));
            enter();
        } else {
            console.log('contains null value');
        }
    };

    let promiseArray = Array(60).fill().map((_, i) => (parseInt('AAAD0000', 16) + i).toString(16)).map(chara3);
    Promise.all(promiseArray).then(() => console.log('done'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM