Exemplo n.º 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);
            }
        }
Exemplo n.º 2
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();
                }
            }
        }
Exemplo n.º 3
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();
                }
            }
        }
Exemplo n.º 4
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();
                }
            }
        }