示例#1
0
        private void Input_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            MbapHeader header = MbapHeader.Decode(e.Message);

            if (header == null)
            {
                logger?.LogWarning("MBAP Header returned null");
                return;  //assume keep alive
            }
            if (!map.HasItem(header.UnitId))
            {
                byte[] errorMsg = ModbusErrorMessage.Create(e.Message, ErrorCode.GatewayPathsNotAvailable);
                this.InputChannel.SendAsync(errorMsg).GetAwaiter();
                return;
            }

            if (!map.GetItem(header.UnitId).Authorize(e.Message))
            {
                byte[] errorMsg = ModbusErrorMessage.Create(e.Message, ErrorCode.IllegalAddress);
                this.InputChannel.SendAsync(errorMsg).GetAwaiter();
                return;
            }

            byte[] message = e.Message;
            byte[] msg     = null;

            foreach (var filter in InputFilters)
            {
                msg = filter.Execute(message);
                msg ??= message;
            }

            OutputChannel.SendAsync(msg).GetAwaiter();
        }
示例#2
0
        private void Cache_OnExpired(object sender, CacheItemExpiredEventArgs e)
        {
            Tuple <ushort, byte[]> tuple = (Tuple <ushort, byte[]>)e.Value;

            byte[] errorMsg = ModbusErrorMessage.Create(tuple.Item2, ErrorCode.DeviceFailedToRespond);
            OnReceive?.Invoke(this, new ChannelReceivedEventArgs(channel.Id, errorMsg));
        }