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

    if (_errorRequestBufferLength(requestBuffer)) {
        return;
    }

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

    if (vector.setCoil) {
        var callbackInvoked = false;
        var cb = function(err) {
            if (err) {
                if (!callbackInvoked) {
                    callbackInvoked = true;
                    callback(err);
                }

                return;
            }

            if (!callbackInvoked) {
                modbusSerialDebug({ action: "FC5 response", responseBuffer: responseBuffer });

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

        try {
            if (vector.setCoil.length === 4) {
                vector.setCoil(address, state === 0xff00, unitID, cb);
            }
            else {
                var promiseOrValue = vector.setCoil(address, state === 0xff00, unitID);
                _handlePromiseOrValue(promiseOrValue, cb);
            }
        }
        catch(err) {
            cb(err);
        }
    }
}
function _handleWriteCoil(requestBuffer, vector, unitID, callback) {
    var address = requestBuffer.readUInt16BE(2);
    var state = requestBuffer.readUInt16BE(4);

    if (_errorRequestBufferLength(requestBuffer)) {
        return;
    }

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

    if (vector.setCoil) {
        var callbackInvoked = false;
        var cb = function(err) {
            if (err) {
                if (!callbackInvoked) {
                    callbackInvoked = true;
                    callback(err);
                }

                return;
            }

            if (!callbackInvoked) {
                modbusSerialDebug({ action: "FC5 response", responseBuffer: responseBuffer });

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

        try {
            if (vector.setCoil.length === 4) {
                vector.setCoil(address, state === 0xff00, unitID, cb);
            }
            else {
                var promiseOrValue = vector.setCoil(address, state === 0xff00, unitID);
                _handlePromiseOrValue(promiseOrValue, cb);
            }
        }
        catch(err) {
            cb(err);
        }
    }
}
function _handleWriteCoil ( requestBuffer , vector , unitID , callback ) { var address = requestBuffer . readUInt16BE ( 2 ) ; var state = requestBuffer . readUInt16BE ( 4 ) ; if ( _errorRequestBufferLength ( requestBuffer ) ) { return ; } // build answer var responseBuffer = Buffer . alloc ( 8 ) ; responseBuffer . writeUInt16BE ( address , 2 ) ; responseBuffer . writeUInt16BE ( state , 4 ) ; if ( vector . setCoil ) { var callbackInvoked = false ; var cb = function ( err ) { if ( err ) { if ( ! callbackInvoked ) { callbackInvoked = true ; callback ( err ) ; } return ; } if ( ! callbackInvoked ) { modbusSerialDebug ( { action : "FC5 response" , responseBuffer : responseBuffer } ) ; callbackInvoked = true ; callback ( null , responseBuffer ) ; } } ; try { if ( vector . setCoil . length === 4 ) { vector . setCoil ( address , state === 0xff00 , unitID , cb ) ; } else { var promiseOrValue = vector . setCoil ( address , state === 0xff00 , unitID ) ; _handlePromiseOrValue ( promiseOrValue , cb ) ; } } catch ( err ) { cb ( err ) ; } } }
Function to handle FC5 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 FC5 request .