• Nice!

    If you get rid of bufferHex, and use normal numbers in checkValidity that should save some memory, and calls to parseInt.
    Also, if you use a typed array: Uint8Array(10) as buffer, that should save some memory too.
    And in checkvalidity you can use E.sum to sum the numbers (and Array.slice to get the data between the indexes). That should save some processor cycles, and code space as well.

    Just replace this

    function checkvalidity(bufferHex) {
        let  sum = 0;
        for (let i = 2; i < 8; i++) {
            sum = sum + parseInt(bufferHex[i], 16);
        }
        sum = sum &0xFF;
        const validCheckSum = parseInt(bufferHex[8],16);
        return sum == validCheckSum;
    }
    

    with

    function checkvalidity(buff) {
        let sum =  E.sum(buff.slice(2,8));
        sum = sum & 0xFF;
        const validCheckSum = buff[8];
        return sum == validCheckSum;
    }
    

    I think I got the indexes right, tho just tested it in browser console with the sample in comments. :)

    If you use Uint8Array, you have to use indexing to add data to the buffer, just replace

    bufferDecimal.push(charDecimal);
    

    with something like

    bufferDecimal[bufferIndex++] = charDecimal;
    

    And probably you have to change the buffer index checks to prevent overflow, if the data is not correct.
    Sorry, I have a tendency to optimize code as I see... Hope you don't mind too much as effectively I have given you some work to do :)

About

Avatar for AkosLukacs @AkosLukacs started