Beispiel #1
0
        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}");
            }
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> Post([FromBody] byte[] message)
        {
            channel.OnReceive += Channel_OnReceive;
            mapper.MapIn(message);
            await channel.AddMessageAsync(message);

            ThreadPool.QueueUserWorkItem(Listen, waitHandles[0]);
            WaitHandle.WaitAll(waitHandles);
            if (result != null)
            {
                logger?.LogDebug("API returned response.");
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(result)
                });
            }

            logger?.LogWarning("API returned no response from RTU.");
            return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
        }
        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));
            }
        }
Beispiel #4
0
 public async Task SendAsync(byte[] message)
 {
     byte[] msg = mapper.MapIn(message, slave.Alias);
     lastMessage = msg;
     await channel.SendAsync(msg);
 }
Beispiel #5
0
 public byte[] Execute(byte[] message, byte?alias = null)
 {
     return(mapper.MapIn(message, alias));
 }