简体   繁体   中英

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. 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.

function named chara3() consists of write and read function which is a callback function in which the command is passed read back.

My requirement is the 'mydata' value which i said earlier, the last two zeros is the memory address. i need to read the data in different memory addresses starting from zero to 59. That is AAAD0000 to 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)???

first command writes to it then reads data memory location incremented by 1 this should repeat up to 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

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

  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'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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