public virtual void ExecuteCustomMessage_Foobar() { try { bool delegateExectuted = false; Slave.RegisterCustomFunction <CustomFoobarRequest>(CustomFoobarRequest.FoobarFunctionCode, (request, dataStore) => { delegateExectuted = true; // get data from data store, make sure to access within a lock to respect multi threaded access ushort[] data; lock (dataStore.SyncRoot) data = dataStore.HoldingRegisters.Slice(request.StartAddress, request.NumberOfPoints).ToArray(); return(new CustomFoobarResponse { SlaveAddress = request.SlaveAddress, ByteCount = 4, Data = data }); }); var foobarRequest = new CustomFoobarRequest(SlaveAddress, 104, 2); var foobarResponse = Master.ExecuteCustomMessage <CustomFoobarResponse>(foobarRequest); Assert.IsTrue(delegateExectuted); Assert.AreEqual(new ushort[] { 0, 0 }, foobarResponse.Data); } finally { Slave.UnregisterCustomFunction(CustomFoobarRequest.FoobarFunctionCode); } }