servers/servertcp_handler.js
function _handleForceMultipleCoils(requestBuffer, vector, unitID, callback) {
    var address = requestBuffer.readUInt16BE(2);
    var length = requestBuffer.readUInt16BE(4);

    // if length is bad, ignore message
    if (requestBuffer.length !== 7 + Math.ceil(length / 8) + 2) {
        return;
    }

    // build answer
    var responseBuffer = Buffer.alloc(8);
    responseBuffer.writeUInt16BE(address, 2);
    responseBuffer.writeUInt16BE(length, 4);

    var callbackInvoked = false;
    var cbCount = 0;
    var buildCb = function(/* i - not used at the moment */) {
        return function(err) {
            if (err) {
                if (!callbackInvoked) {
                    callbackInvoked = true;
                    callback(err);
                }

                return;
            }

            cbCount = cbCount + 1;

            if (cbCount === length && !callbackInvoked) {
                modbusSerialDebug({ action: "FC15 response", responseBuffer: responseBuffer });

                callbackInvoked = true;
                callback(null, responseBuffer);
            }
        };
    };

    if (length === 0)
        callback({
            modbusErrorCode: 0x02, // Illegal address
            msg: "Invalid length"
        });

    if (vector.setCoil) {
        var state;

        for (var i = 0; i < length; i++) {
            var cb = buildCb(i);
            state = requestBuffer.readBit(i, 7);

            try {
                if (vector.setCoil.length === 4) {
                    vector.setCoil(address + i, state !== false, unitID, cb);
                }
                else {
                    var promiseOrValue = vector.setCoil(address + i, state !== false, unitID);
                    _handlePromiseOrValue(promiseOrValue, cb);
                }
            }
            catch(err) {
                cb(err);
            }
        }
    } else if (vector.setCoilArray) {
        state = [];

        for (i = 0; i < length; i++) {
            cb = buildCb(i);
            state.push(requestBuffer.readBit(i, 7));
            _handlePromiseOrValue(promiseOrValue, cb);
        }

        try {
            if (vector.setCoilArray.length === 4) {
                vector.setCoilArray(address, state, unitID, cb);
            }
            else {
                vector.setCoilArray(address, state, unitID);
            }
        }
        catch(err) {
            cb(err);
        }
    }
}
function _handleForceMultipleCoils(requestBuffer, vector, unitID, callback) {
    var address = requestBuffer.readUInt16BE(2);
    var length = requestBuffer.readUInt16BE(4);

    // if length is bad, ignore message
    if (requestBuffer.length !== 7 + Math.ceil(length / 8) + 2) {
        return;
    }

    // build answer
    var responseBuffer = Buffer.alloc(8);
    responseBuffer.writeUInt16BE(address, 2);
    responseBuffer.writeUInt16BE(length, 4);

    var callbackInvoked = false;
    var cbCount = 0;
    var buildCb = function(/* i - not used at the moment */) {
        return function(err) {
            if (err) {
                if (!callbackInvoked) {
                    callbackInvoked = true;
                    callback(err);
                }

                return;
            }

            cbCount = cbCount + 1;

            if (cbCount === length && !callbackInvoked) {
                modbusSerialDebug({ action: "FC15 response", responseBuffer: responseBuffer });

                callbackInvoked = true;
                callback(null, responseBuffer);
            }
        };
    };

    if (length === 0)
        callback({
            modbusErrorCode: 0x02, // Illegal address
            msg: "Invalid length"
        });

    if (vector.setCoil) {
        var state;

        for (var i = 0; i < length; i++) {
            var cb = buildCb(i);
            state = requestBuffer.readBit(i, 7);

            try {
                if (vector.setCoil.length === 4) {
                    vector.setCoil(address + i, state !== false, unitID, cb);
                }
                else {
                    var promiseOrValue = vector.setCoil(address + i, state !== false, unitID);
                    _handlePromiseOrValue(promiseOrValue, cb);
                }
            }
            catch(err) {
                cb(err);
            }
        }
    } else if (vector.setCoilArray) {
        state = [];

        for (i = 0; i < length; i++) {
            cb = buildCb(i);
            state.push(requestBuffer.readBit(i, 7));
            _handlePromiseOrValue(promiseOrValue, cb);
        }

        try {
            if (vector.setCoilArray.length === 4) {
                vector.setCoilArray(address, state, unitID, cb);
            }
            else {
                vector.setCoilArray(address, state, unitID);
            }
        }
        catch(err) {
            cb(err);
        }
    }
}
function _handleForceMultipleCoils ( requestBuffer , vector , unitID , callback ) { var address = requestBuffer . readUInt16BE ( 2 ) ; var length = requestBuffer . readUInt16BE ( 4 ) ; // if length is bad, ignore message if ( requestBuffer . length !== 7 + Math . ceil ( length / 8 ) + 2 ) { return ; } // build answer var responseBuffer = Buffer . alloc ( 8 ) ; responseBuffer . writeUInt16BE ( address , 2 ) ; responseBuffer . writeUInt16BE ( length , 4 ) ; var callbackInvoked = false ; var cbCount = 0 ; var buildCb = function ( /* i - not used at the moment */ ) { return function ( err ) { if ( err ) { if ( ! callbackInvoked ) { callbackInvoked = true ; callback ( err ) ; } return ; } cbCount = cbCount + 1 ; if ( cbCount === length && ! callbackInvoked ) { modbusSerialDebug ( { action : "FC15 response" , responseBuffer : responseBuffer } ) ; callbackInvoked = true ; callback ( null , responseBuffer ) ; } } ; } ; if ( length === 0 ) callback ( { modbusErrorCode : 0x02 , // Illegal address msg : "Invalid length" } ) ; if ( vector . setCoil ) { var state ; for ( var i = 0 ; i < length ; i ++ ) { var cb = buildCb ( i ) ; state = requestBuffer . readBit ( i , 7 ) ; try { if ( vector . setCoil . length === 4 ) { vector . setCoil ( address + i , state !== false , unitID , cb ) ; } else { var promiseOrValue = vector . setCoil ( address + i , state !== false , unitID ) ; _handlePromiseOrValue ( promiseOrValue , cb ) ; } } catch ( err ) { cb ( err ) ; } } } else if ( vector . setCoilArray ) { state = [ ] ; for ( i = 0 ; i < length ; i ++ ) { cb = buildCb ( i ) ; state . push ( requestBuffer . readBit ( i , 7 ) ) ; _handlePromiseOrValue ( promiseOrValue , cb ) ; } try { if ( vector . setCoilArray . length === 4 ) { vector . setCoilArray ( address , state , unitID , cb ) ; } else { vector . setCoilArray ( address , state , unitID ) ; } } catch ( err ) { cb ( err ) ; } } }
Function to handle FC15 request.

@param requestBuffer - request Buffer from client
@param vector - vector of functions for read and write
@param unitID - Id of the requesting unit
@param {function} callback - callback to be invoked passing {Buffer} response
@returns undefined
@private
Function to handle FC15 request .