ports/asciiport.js
function _asciiDecodeResponseBuffer(bufAscii) {

    // create a new buffer of the correct size (based on ascii encoded buffer length)
    var bufDecoded = Buffer.alloc((bufAscii.length - 1) / 2);

    // decode into new buffer (removing delimiters at start and end)
    for (var i = 0; i < (bufAscii.length - 3) / 2; i++) {
        bufDecoded.write(String.fromCharCode(bufAscii.readUInt8(i * 2 + 1), bufAscii.readUInt8(i * 2 + 2)), i, 1, "hex");
    }

    // check the lrc is true
    var lrcIn = bufDecoded.readUInt8(bufDecoded.length - 2);
    if(calculateLrc(bufDecoded.slice(0, -2)) !== lrcIn) {
        // return null if lrc error
        var calcLrc = calculateLrc(bufDecoded.slice(0, -2));

        modbusSerialDebug({ action: "LRC error", LRC: lrcIn.toString(16), calcLRC: calcLrc.toString(16) });
        return null;
    }

    // replace the 1 byte lrc with a two byte crc16
    bufDecoded.writeUInt16LE(crc16(bufDecoded.slice(0, -2)), bufDecoded.length - 2);

    return bufDecoded;
}
function _asciiDecodeResponseBuffer(bufAscii) {

    // create a new buffer of the correct size (based on ascii encoded buffer length)
    var bufDecoded = Buffer.alloc((bufAscii.length - 1) / 2);

    // decode into new buffer (removing delimiters at start and end)
    for (var i = 0; i < (bufAscii.length - 3) / 2; i++) {
        bufDecoded.write(String.fromCharCode(bufAscii.readUInt8(i * 2 + 1), bufAscii.readUInt8(i * 2 + 2)), i, 1, "hex");
    }

    // check the lrc is true
    var lrcIn = bufDecoded.readUInt8(bufDecoded.length - 2);
    if(calculateLrc(bufDecoded.slice(0, -2)) !== lrcIn) {
        // return null if lrc error
        var calcLrc = calculateLrc(bufDecoded.slice(0, -2));

        modbusSerialDebug({ action: "LRC error", LRC: lrcIn.toString(16), calcLRC: calcLrc.toString(16) });
        return null;
    }

    // replace the 1 byte lrc with a two byte crc16
    bufDecoded.writeUInt16LE(crc16(bufDecoded.slice(0, -2)), bufDecoded.length - 2);

    return bufDecoded;
}
function _asciiDecodeResponseBuffer ( bufAscii ) { // create a new buffer of the correct size (based on ascii encoded buffer length) var bufDecoded = Buffer . alloc ( ( bufAscii . length - 1 ) / 2 ) ; // decode into new buffer (removing delimiters at start and end) for ( var i = 0 ; i < ( bufAscii . length - 3 ) / 2 ; i ++ ) { bufDecoded . write ( String . fromCharCode ( bufAscii . readUInt8 ( i * 2 + 1 ) , bufAscii . readUInt8 ( i * 2 + 2 ) ) , i , 1 , "hex" ) ; } // check the lrc is true var lrcIn = bufDecoded . readUInt8 ( bufDecoded . length - 2 ) ; if ( calculateLrc ( bufDecoded . slice ( 0 , - 2 ) ) !== lrcIn ) { // return null if lrc error var calcLrc = calculateLrc ( bufDecoded . slice ( 0 , - 2 ) ) ; modbusSerialDebug ( { action : "LRC error" , LRC : lrcIn . toString ( 16 ) , calcLRC : calcLrc . toString ( 16 ) } ) ; return null ; } // replace the 1 byte lrc with a two byte crc16 bufDecoded . writeUInt16LE ( crc16 ( bufDecoded . slice ( 0 , - 2 ) ) , bufDecoded . length - 2 ) ; return bufDecoded ; }
Ascii decode a 'response' buffer and return it.

@param {Buffer} bufAscii the ascii data buffer to decode.
@return {Buffer} the decoded buffer, or null if decode error
@private
Ascii decode a response buffer and return it .