예제 #1
0
        public bool CloseBus(BusInfo busInfo)
        {
            lock (this.busInstances)
            {
                IBus bus = this.TryGetCreatedBus(busInfo);
                if (bus == null)
                {
                    return(false);
                }

                bus.Close();
                while (busInfo.Parent != null)
                {
                    bus = this.TryGetCreatedBus(busInfo.Parent);
                    if (bus == null)
                    {
                        break;
                    }

                    //该总线被其它设备所占用
                    if (this.busInstances.Keys.Any(obj => MatchBusWithParent(obj, busInfo)))
                    {
                        break;
                    }

                    bus.Close();
                    busInfo = busInfo.Parent;
                }
                return(true);
            }
        }
예제 #2
0
        /// <summary>
        /// 已重写,比较Type,与UniqueValue是否相同
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            BusInfo other = (obj) as BusInfo;

            if (other == null)
            {
                return(false);
            }

            if (this.Type != other.Type)
            {
                return(false);
            }

            if (string.Compare(this.UiqueValue, other.UiqueValue) != 0)
            {
                return(false);
            }

            if (string.Compare(this.DriverProvider, other.DriverProvider) != 0)
            {
                return(false);
            }

            return(true);
        }
예제 #3
0
 public IBus TryGetCreatedBus(BusInfo busInfo)
 {
     lock (this.busInstances)
     {
         if (this.busInstances.ContainsKey(busInfo))
         {
             return(this.busInstances[busInfo]);
         }
         return(null);
     }
 }
예제 #4
0
        public int Read(BusInfo busInfo, byte[] readBuf, int maxReadLen = 0)
        {
            IBus bus = this.CreateBusFromBusInfo(busInfo, true);

            try
            {
                return(bus.Read(readBuf, maxReadLen));
            }
            finally
            {
                if (bus != null && bus.State != BusState.Closed && busInfo.CacheResource == false)
                {
                    bus.Close();
                }
            }
        }
예제 #5
0
        public void Write(BusInfo busInfo, byte[] writeBuf, int offset, int len)
        {
            IBus bus = this.CreateBusFromBusInfo(busInfo, true);

            try
            {
                bus.Write(writeBuf, offset, len);
            }
            finally
            {
                if (bus != null && bus.State != BusState.Closed && busInfo.CacheResource == false)
                {
                    bus.Close();
                }
            }
        }
예제 #6
0
        private bool MatchBusWithParent(BusInfo matchListRoot, BusInfo busInfo)
        {
            if (busInfo == null)
            {
                throw new ArgumentNullException("busInfo");
            }

            while (matchListRoot != null)
            {
                if (matchListRoot.Equals(busInfo))
                {
                    return(true);
                }
                matchListRoot = matchListRoot.Parent;
            }
            return(false);
        }
예제 #7
0
        public int WriteAndRead(BusInfo busInfo, byte[] writeBuf, int offset, int len, byte[] readBuf, int maxReadLen = 0, int waitTimeBeforeRead = 0)
        {
            IBus bus = this.CreateBusFromBusInfo(busInfo, true);

            try
            {
                bus.Write(writeBuf, offset, len);
                if (waitTimeBeforeRead > 0)
                {
                    Thread.Sleep(waitTimeBeforeRead);
                }
                return(bus.Read(readBuf, maxReadLen));
            }
            finally
            {
                if (bus != null && bus.State != BusState.Closed && busInfo.CacheResource == false)
                {
                    bus.Close();
                }
            }
        }
예제 #8
0
 public void Open(BusInfo busInfo)
 {
     lock (this.busLock)
     {
         try
         {
             this.Info = busInfo;
             if (this.State != BusState.Closed && this.State != BusState.Error && this.State != BusState.Created)
             {
                 throw new Exception(string.Format("Bus:{0} is {1} could not open", busInfo.ReadableValue, this.State));
             }
             this.State = BusState.Opening;
             this.OpenImplement(busInfo);
             this.State = BusState.Opened;
         }
         catch
         {
             this.State = BusState.Error;
             this.Info  = null;
             throw;
         }
     }
 }
예제 #9
0
 public IBus CreateBusFromBusInfo(BusInfo busInfo, bool callOpen = false)
 {
     lock (this.busInstances)
     {
         IBus bus = null;
         if (this.busInstances.ContainsKey(busInfo))
         {
             bus = this.busInstances[busInfo];
         }
         else
         {
             Assembly assembly = Assembly.Load(busInfo.AssemblyName);
             Type     t        = assembly.GetType(busInfo.ClassFullName);
             bus = Activator.CreateInstance(t) as IBus;
             this.OnBusCreated(new BusEventArgs(bus));
         }
         if ((bus.State == BusState.Closed || bus.State == BusState.Created) && callOpen == true)
         {
             bus.Open(busInfo);
         }
         this.busInstances[busInfo] = bus;
         return(bus);
     }
 }
예제 #10
0
 protected abstract void OpenImplement(BusInfo busInfo);
예제 #11
0
 public T CreateBusFromBusInfo <T>(BusInfo busInfo, bool callOpen = false) where T : IBus
 {
     return((T)CreateBusFromBusInfo(busInfo, callOpen));
 }