コード例 #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
ファイル: ModulePipeline.cs プロジェクト: skunklab/virtualrtu
        private void Input_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            byte[] message = e.Message;
            byte[] msg     = message;

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                if (header == null)
                {
                    return;
                }

                Slave slave = config.Slaves.Where(s => s.UnitId == header.UnitId).FirstOrDefault();
                byte? alias = slave?.Alias.Value;

                foreach (var filter in InputFilters)
                {
                    msg = filter.Execute(message, alias);
                    msg ??= message;
                    logger?.LogDebug("Filter executed.");
                }

                OutputChannel.SendAsync(msg).GetAwaiter();
                logger?.LogDebug("Message sent to output channel.");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Fault input channel receive.");
            }
        }
コード例 #3
0
        private void Channel_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            try
            {
                MbapHeader header = MbapHeader.Decode(e.Message);

                if (!unitId.HasValue)
                {
                    unitId = header.UnitId;
                }

                if (unitId.HasValue && header.UnitId == unitId.Value)
                {
                    RtuPiSystem piSystem = map.GetItem((ushort)unitId.Value);

                    if (piSystem == null)
                    {
                        throw new Exception($"PI-System not found for unit id - {unitId.Value}");
                    }
                    else
                    {
                        client.SubscribeAsync(piSystem.RtuOutputEvent, QualityOfServiceLevelType.AtLeastOnce, ReceiveOutput).GetAwaiter();
                        client.PublishAsync(QualityOfServiceLevelType.AtLeastOnce, piSystem.RtuInputEvent, contentType, e.Message).GetAwaiter();
                    }
                }
                else
                {
                    throw new Exception("Unit Id missing from SCADA client message.");
                }
            }
            catch (Exception ex)
            {
                OnError?.Invoke(this, new AdapterErrorEventArgs(Id, ex));
            }
        }
コード例 #4
0
        public async Task SendAsync(byte[] message)
        {
            MbapHeader header = MbapHeader.Decode(message);

            if (slaveChannels.ContainsKey(header.UnitId))
            {
                SlaveChannel slaveChannel = slaveChannels[header.UnitId];
                await slaveChannel.SendAsync(message);
            }
        }
コード例 #5
0
 private void ModuleReceived(string resource, string contentType, byte[] message)
 {
     //received a message from subscription
     try
     {
         MbapHeader header = MbapHeader.Decode(message);
         diag?.PublishInput(header).GetAwaiter();
         logger?.LogDebug("Diagnostics sent input.");
         OnReceive?.Invoke(this, new ChannelReceivedEventArgs(channel.Id, message));
     }
     catch (Exception ex)
     {
         logger?.LogError(ex, "Unable to decode MBAP header module channel input.");
     }
 }
コード例 #6
0
ファイル: TcpManager.cs プロジェクト: skunklab/virtualrtu
        public async Task SendAsync(byte[] message)
        {
            MbapHeader header = MbapHeader.Decode(message);

            if (connections.ContainsKey(header.UnitId))
            {
                TcpConnection connection = connections[header.UnitId].Item1;
                byte[]        msg        = mapper.MapIn(message, connections[header.UnitId].Item2);
                await connection.SendAsync(msg);

                logger?.LogDebug($"Modbus message sent to UnitId {header.UnitId} TCP channel.");
            }
            else
            {
                logger?.LogWarning($"No tcp connection found with Unit ID = {header.UnitId}");
            }
        }
コード例 #7
0
        public async Task SendAsync(byte[] message)
        {
            if (client == null || !client.IsConnected)
            {
                logger?.LogWarning("MQTT client is not available to forward message.");
                return;
            }

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                if (map.HasItem(header.UnitId))
                {
                    if (!subscriptions.Contains(header.UnitId))
                    {
                        string resource = map.GetItem(header.UnitId).RtuOutputEvent;
                        await client.SubscribeAsync(resource, QualityOfServiceLevelType.AtMostOnce,
                                                    ModbusMessageReceived);

                        logger?.LogInformation(
                            $"MQTT client channel subscribed {resource} with Unit ID = {header.UnitId}");
                        subscriptions.Add(header.UnitId);
                    }

                    cache.Add(GetCacheKey(header), new Tuple <ushort, byte[]>(header.TransactionId, message), 20.0);
                    string pisystem = map.GetItem(header.UnitId).RtuInputEvent;
                    await client.PublishAsync(QualityOfServiceLevelType.AtMostOnce, pisystem,
                                              "application/octet-stream", message);

                    logger?.LogDebug($"VRTU published to {pisystem}");
                    await diag?.PublishInput(header);
                }
                else
                {
                    logger?.LogWarning($"Unit Id = {header.UnitId} in Modbus message not found in RTU map.");
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Fault sending MQTT client channel.");
            }
        }
コード例 #8
0
        private async void ReceiveOutput(string topic, string contentType, byte[] message)
        {
            logger?.LogDebug("SCADA client channel receiving output.");

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                byte[]     msg    = mapper.MapOut(message);
                await channel.SendAsync(msg);

                MbapHeader actual = MbapHeader.Decode(msg);
                await diagnostics.PublishOutput(actual, header.TransactionId);

                logger?.LogDebug("SCADA client channel was sent output message.");
            }
            catch (Exception ex)
            {
                logger?.LogError($"SCADA client receive output error - {ex.Message}");
                OnError?.Invoke(this, new AdapterErrorEventArgs(Id, ex));
            }
        }
コード例 #9
0
 private void ModbusMessageReceived(string resource, string contentType, byte[] message)
 {
     try
     {
         MbapHeader header = MbapHeader.Decode(message);
         string     key    = GetCacheKey(header);
         if (cache.Contains(key))
         {
             cache.Remove(key);
             OnReceive?.Invoke(this, new ChannelReceivedEventArgs(channel.Id, message));
             logger?.LogDebug("Output channel received message.");
             diag?.PublishOutput(header).GetAwaiter();
         }
         else
         {
             logger?.LogWarning("Vrtu channel cannot match received message.");
         }
     }
     catch (Exception ex)
     {
         logger?.LogError(ex, "Fault receiving message on vrtu channel.");
     }
 }
コード例 #10
0
        public async Task SendAsync(byte[] message)
        {
            if (client == null || !client.IsConnected)
            {
                logger?.LogWarning("Module channel client is unavailable to send.");
                return;
            }

            try
            {
                MbapHeader header   = MbapHeader.Decode(message);
                string     pisystem = UriGenerator.GetRtuPiSystem(hostname, virtualRtuId, deviceId, header.UnitId, false);
                await client.PublishAsync(QualityOfServiceLevelType.AtMostOnce, pisystem, "application/json", message);

                await diag?.PublishOutput(header);

                logger?.LogDebug("Published message on module channel");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Fault sending on module channel.");
            }
        }
コード例 #11
0
        private async void Channel_OnReceive(object sender, ChannelReceivedEventArgs e)
        {
            logger?.LogDebug("SCADA client channel starting receive.");

            try
            {
                MbapHeader  header   = MbapHeader.Decode(e.Message);
                RtuPiSystem piSystem = map.GetItem(header.UnitId);

                if (piSystem == null)
                {
                    logger?.LogWarning("SCADA client receive cannot find RTU pi-system.");
                    throw new InvalidOperationException("RTU pi-system was not found.");
                }

                if (!subscribed.Contains(header.UnitId))
                {
                    //subscribe to pi-system for unit id
                    await connection.AddSubscriptionAsync(piSystem.RtuOutputEvent.ToLowerInvariant(), ReceiveOutput);

                    subscribed.Add(header.UnitId);
                }

                byte[] msg = mapper.MapIn(e.Message);
                await connection.SendAsync(piSystem.RtuInputEvent.ToLowerInvariant(), CONTENT_TYPE, msg);

                MbapHeader mheader = MbapHeader.Decode(msg);


                //await connection.Monitor.SendInAsync(ModuleType.VRTU.ToString(), e.Message, mheader.TransactionId);
            }
            catch (Exception ex)
            {
                logger?.LogError($"SCADA client receive error - {ex.Message}");
                OnError?.Invoke(this, new AdapterErrorEventArgs(Id, ex));
            }
        }
コード例 #12
0
        private static void Main(string[] args)
        {
            Console.WriteLine("8\"\"\"\"8 8\"\"\"\"8 8\"\"\"\"8 8\"\"\"\"8 8\"\"\"\"8");
            Console.WriteLine("8      8    \" 8    8 8    8 8    8");
            Console.WriteLine("8eeeee 8e     8eeee8 8e   8 8eeee8 ");
            Console.WriteLine("    88 88     88   8 88   8 88   8");
            Console.WriteLine("e   88 88   e 88   8 88   8 88   8 ");
            Console.WriteLine("8eee88 88eee8 88   8 88eee8 88   8");
            Console.WriteLine("");
            Console.WriteLine("8\"\"\"\"8");
            Console.WriteLine("8    \" e     e  eeee eeeee eeeee ");
            Console.WriteLine("8e     8     8  8    8   8   8");
            Console.WriteLine("88     8e    8e 8eee 8e  8   8e");
            Console.WriteLine("88   e 88    88 88   88  8   88");
            Console.WriteLine("88eee8 88eee 88 88ee 88  8   88");
            Console.WriteLine("");


            Console.WriteLine("press any key to continue");
            Console.ReadKey();

            //string publicIP = "168.62.59.20";
            string publicIP = "20.185.9.100";

            Console.Write("Enter for default IP (127.0.0.1)? ");
            string inputIpAddress = Console.ReadLine();

            if (!string.IsNullOrEmpty(inputIpAddress))
            {
                publicIP = inputIpAddress;
            }
            else
            {
                publicIP = "127.0.0.1";
            }


            Random ran = new Random();

            byte[] buffer = new byte[100];
            ran.NextBytes(buffer);
            MbapHeader header = new MbapHeader
            {
                UnitId        = 2,
                ProtocolId    = 1,
                TransactionId = 1,
                Length        = 6
            };

            byte[] body = { 3, 79, 27, 0, 10 };


            byte[] array  = header.Encode();
            byte[] output = new byte[buffer.Length + array.Length];
            Buffer.BlockCopy(array, 0, output, 0, array.Length);
            Buffer.BlockCopy(buffer, 0, output, array.Length, buffer.Length);

            byte[]     o2 = Convert.FromBase64String("AAEAAAAGAQNPGwAK");
            MbapHeader mh = MbapHeader.Decode(o2);

            output = o2;
            string x = BitConverter.ToString(o2);

            CancellationTokenSource cts = new CancellationTokenSource();
            IPEndPoint endpoint         = new IPEndPoint(IPAddress.Parse(publicIP), 502);

            channel            = ChannelFactory.Create(false, endpoint, 1024, 4048, cts.Token);
            channel.OnError   += Channel_OnError;
            channel.OnClose   += Channel_OnClose;
            channel.OnOpen    += Channel_OnOpen;
            channel.OnReceive += Channel_OnReceive;

            channel.OpenAsync().GetAwaiter();
            while (!ican)
            {
                Task t = Task.Delay(2000);
                Task.WaitAll(t);
            }

            //channel.SendAsync(output).GetAwaiter();

            bool test = true;

            while (test)
            {
                Console.Write("Send a message [y/n] ? ");

                string decision = Console.ReadLine();
                if (decision.ToLowerInvariant() == "y")
                {
                    byte[] payload = Convert.FromBase64String("AAEAAAAGAQNPGwAK");
                    //MbapHeader header2 = MbapHeader.Decode(payload);
                    //header2.UnitId = 2;
                    //byte[] headerBytes = header2.Encode();
                    //byte[] buffer2 = new byte[payload.Length];
                    //Buffer.BlockCopy(headerBytes, 0, buffer2, 0, headerBytes.Length);
                    //Buffer.BlockCopy(payload, headerBytes.Length, buffer2, headerBytes.Length, payload.Length - headerBytes.Length);

                    //dummy++;
                    //payload[1] = dummy;

                    //string bc = BitConverter.ToString(payload);
                    //channel.SendAsync(buffer2).GetAwaiter();
                    channel.SendAsync(payload).GetAwaiter();
                    //channel.SendAsync(output).GetAwaiter();
                    Console.WriteLine($"Sent message length {output.Length}");
                }
                else
                {
                    test = false;
                }
            }

            Console.WriteLine("terminating...");
            Console.ReadKey();
        }