Ejemplo n.º 1
0
        /// <summary>
        /// Registers specified custom function.
        /// </summary>
        /// <typeparam name="TRequest">The type of the modbus request message.</typeparam>
        /// <param name="functionCode">The function code to provide the implementation for.</param>
        /// <param name="applyRequest">The delegate to apply the request to the DataStore and return the appropriate response.</param>
        /// <exception cref="ArgumentException">Custom function registration already exists.</exception>
        public void RegisterCustomFunction <TRequest>(byte functionCode, Func <TRequest, DataStore, IModbusMessage> applyRequest)
            where TRequest : IModbusMessage, new()
        {
            if (applyRequest == null)
            {
                throw new ArgumentNullException("applyRequest");
            }
            if (_customMessages.ContainsKey(functionCode))
            {
                throw new ArgumentException("A custom function already exists with the specified function code. You must unregister it first.", "functionCode");
            }

            // CONSIDER only allowing true user-defined function codes, Modbus defines 65-72 and 100-110 as user-defined function codes.

            // wrap in more generic delegate type
            Func <IModbusMessage, DataStore, IModbusMessage> wrapper = (message, dataStore) => applyRequest((TRequest)message, dataStore);

            _customMessages[functionCode] = new CustomMessageInfo(typeof(TRequest), wrapper);
        }
Ejemplo n.º 2
0
 internal bool TryGetCustomMessageInfo(byte functionCode, out CustomMessageInfo messageInfo)
 {
     return(_customMessages.TryGetValue(functionCode, out messageInfo));
 }