Пример #1
0
        /// <summary>
        /// 读取地址表所有寄存器,阻塞的
        /// </summary>
        /// <param name="dev"></param>
        /// <returns></returns>
        public TStorage Read()
        {
            ready();
            var         type        = typeof(TStorage);
            List <byte> packReceive = new List <byte>();
            //以一次读取120个字
            var packLength = 120;
            var packSends  = type.SendPackaging(packLength);

            if (packSends?.Count > 0)
            {
                foreach (var s in packSends)
                {
                    byte[] bySend = _iProtocol.GetRead03Data(DeviceAddress, s.Start, s.Length.ToString(), CRCType, ref _tcpFlag);

                    _con.Com.Write(bySend);

                    byte[] byRead = _iProtocol.ReadReturnRead(_con.Com, s.Length * 2);
                    if (_iProtocol.CheckReceiveData(byRead))
                    {
                        var byValue = _iProtocol.GetCutReceiveData(byRead);
                        packReceive.AddRange(byValue.ToList());
                    }

                    Thread.Sleep(5);
                }
                if (packReceive.Count == type.GetByteSize())
                {
                    packReceive = ModBusHelper.ExchangeOrder(packReceive, 0, ByteOrder, type);
                    byte[] byRe = packReceive.ToArray();
                    return((TStorage)ConvertHelper.BytesToStuct(byRe, type));
                }
            }

            throw new ComException("未读取到数据");
        }