Exemple #1
0
        public void OneTimeSetUp()
        {
            IPlc           plc           = Substitute.For <IPlc>();
            ILoggerFactory loggerFactory = Substitute.For <ILoggerFactory>();

            this.isEndValueReadEventCallback                 = false;
            this.isEndValueReadEventStatusCallback           = false;
            this.isFromStartValueToEndValueReadEventCallback = false;
            this.isNotStartValueReadEventCallback            = false;
            this.isValueReadChangedEventCallback             = false;
            this.plcMonitorImp = new PlcMonitorImp(plc, loggerFactory);
            this.address       = new DataAddress
            {
                Name   = "Adddress",
                Type   = DataAddressType.Int,
                Value  = "E0_300",
                Offset = 0
            };

            plc.Initialize();
            plc.Read <int>(address).Returns(
                new int[] { 0 }, new int[] { 0 }, new int[] { 0 }, new int[] { 0 }, new int[] { 0 },
                new int[] { 1 }, new int[] { 1 }, new int[] { 1 }, new int[] { 1 }, new int[] { 1 },
                new int[] { 3 }, new int[] { 3 }, new int[] { 3 }, new int[] { 3 }, new int[] { 3 });
        }
Exemple #2
0
        private void ReadModule(ref List <BsonDocument> Datastreams, ref long ReqTime, Action <int, dynamic> Callback, IPlc PLC)
        {
            long      TimeStamp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
            LogObject logObj;
            Stopwatch watch = new Stopwatch();

            foreach (BsonDocument Stream in Datastreams)
            {
                Stream["oldTime"] = 0;
            }
            Callback(1, Datastreams);

            watch.Start();
            long Req = 0;

            while (true)
            {
                Callback(0, null);
                TimeStamp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
                watch.Restart();
                foreach (BsonDocument Stream in Datastreams)
                {
                    try
                    {
                        if (BsonResolver.Resolve(Stream["Active"]) && BsonResolver.Resolve(Stream["Interval"]) <= TimeStamp - BsonResolver.Resolve(Stream["oldTime"]))
                        {
                            byte[] buffer = GetBuffer(BsonResolver.Resolve(Stream["DataType"]));
                            PLC.Read(Stream, ref buffer);

                            logObj            = new LogObject();
                            logObj.PLC        = BsonResolver.Resolve(PLC.ClientDetails["id"].ToString());
                            logObj.Datastream = BsonResolver.Resolve(Stream["Collection"]) + "_" + BsonResolver.Resolve(Stream["id"]);
                            logObj.Value      = buffer;
                            logObj._ts        = TimeStamp;
                            logObj.DataType   = BsonResolver.Resolve(Stream["DataType"]);
                            logObj.Position   = BsonResolver.Resolve(Stream["Position"]);

                            Queue.Enqueue(logObj);

                            Stream["oldTime"] = TimeStamp;
                        }
                    }
                    catch
                    {
                        PLC.Disconnect();
                        while (!PLC.Connect())
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }
                Req = watch.ElapsedMilliseconds;
                if (Req > 0)
                {
                    ReqTime = Req;
                }
            }
        }
        public void ReadBoolTest()
        {
            var values    = new string[] { };
            var errorInfo = string.Empty;
            var param     = new string[] { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1" };

            plc.Read <ushort>(Arg.Any <DataAddress>()).Returns(new ushort[] { 65535 });

            plcAdapter.ReadUshort("D1200", ref values, ref errorInfo);
            Assert.AreEqual(values, param);

            plcAdapter.ReadUshort("D", "1200", ref values, ref errorInfo);
            Assert.AreEqual(values, param);
        }
Exemple #4
0
        private List <Tuple <DataAddress, TValue> > ScanAddress(IPlc plc, List <EventInfo <TValue> > eventInfoes)
        {
            var        result     = new List <Tuple <DataAddress, TValue> >();
            SortResult sortResult = InMemoryCache.GetOrAdd <SortResult>(eventInfoes.GetHashCode().ToString(), key =>
            {
                var r = new SortResult();
                var distinctPlcAddresses = eventInfoes.Select(e => (e.Event as EventBase).PlcAddress).Distinct();

                r.AddressNotSegment.Addresses = distinctPlcAddresses.ToList();

                if (!PlcMonitorImp.IsPlcManualSimulator)
                {
                    r = PlcAddressSortHelper.Sort <TValue>(distinctPlcAddresses);
                }

                return(r);
            });

            sortResult.AddressSegments.ForEach(addressSegment =>
            {
                var startAddress = addressSegment.StartAddress;
                List <TValue> values;

                startAddress.Offset = addressSegment.AllAddressesByDes.Count - 1;

                values = plc.Read <TValue>(startAddress).ToList();

                for (var index = 0; index < values.Count(); index++)
                {
                    result.Add(new Tuple <DataAddress, TValue>(addressSegment.AllAddressesByDes[index], values[index]));
                }
            });

            sortResult.AddressNotSegment.Addresses.ForEach(address =>
            {
                var value = plc.ReadSingle <TValue>(address);

                result.Add(new Tuple <DataAddress, TValue>(address, value));
            });

            return(result);
        }
Exemple #5
0
 /// <summary>
 /// 读取单个值
 /// </summary>
 /// <param name="plc">Plc接口</param>
 /// <typeparam name="TValue">类型</typeparam>
 /// <param name="address">地址</param>
 /// <returns>返回值</returns>
 public static TValue ReadSingle <TValue>(this IPlc plc, DataAddress address)
 {
     return(plc.Read <TValue>(address).Single());
 }