Пример #1
0
        /// <summary>
        /// 读取指定寄存器,阻塞的
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="dev"></param>
        /// <param name="positions"></param>
        /// <returns></returns>
        public TResult Read <TResult>(Expression <Func <TStorage, TResult> > positions)
        {
            ready();
            var pro  = positions.GetProperty();
            var attr = pro.GetAttribute <StorageAttribute>();

            if (attr != null)
            {
                var byteSize    = typeof(TResult).GetByteSize();
                var storageSize = Math.Ceiling((float)byteSize / 2);

                byte[] bySend = _iProtocol.GetRead03Data(DeviceAddress, attr.Addr, storageSize.ToString(), CRCType, ref _tcpFlag);

                _con.Com.Write(bySend);

                byte[] byRead = _iProtocol.ReadReturnRead(_con.Com, byteSize);

                if (_iProtocol.CheckReceiveData(byRead))
                {
                    return(_iProtocol.ToSingleField <TResult>(byRead, ByteOrder, byteSize));
                }
            }
            else
            {
                throw new ComException("结构体属性未指定StorageAttribute特性");
            }
            throw new ComException("未读取到数据");
        }