Пример #1
0
        private void AddOrCreateReadPacket(LogixTag tag, int idx)
        {
            //First we create the request...
            int temp = 0;
            ReadDataServiceRequest request = LogixServices.BuildLogixReadDataRequest(
                tag.Address, tag.Elements, out temp);

            //Now we read it from the PLC to find out if it's fragmented...
            CommonPacketItem addressItem = CommonPacketItem.GetConnectedAddressItem(_parent.SessionInfo.ConnectionParameters.O2T_CID);
            CommonPacketItem dataItem    = CommonPacketItem.GetConnectedDataItem(request.Pack(), SequenceNumberGenerator.SequenceNumber(_parent.SyncRoot)); //ToDo _parent.SyncRoot

            EncapsReply reply = _parent.SessionInfo.SendUnitData(addressItem, dataItem);

            if (reply != null)
            {
                //It's a good tag, let's figure out if it's fragmented...
                ReadDataServiceReply rdReply = new ReadDataServiceReply(reply);

                PacketMap pm = new PacketMap()
                {
                    TagIndex = idx
                };
                pm.PacketIndex  = new List <int>();
                pm.ServiceIndex = new List <int>();
                pm.Offsets      = new List <uint>();
                pm.NumReplies   = 1;

                if (rdReply.Status == 0x06)
                {
                    //Partial read... We'll have to request more data, but first let's make this packet
                    request = LogixServices.BuildFragmentedReadDataRequest(tag.Address, tag.Elements,
                                                                           0, out temp);
                    int[] status = FindPacketOrCreate(request.Pack(), (ushort)(rdReply.Data.Length + 2));
                    uint  offset = (uint)rdReply.Data.Length;
                    pm.PacketIndex.Add(status[0]);
                    pm.ServiceIndex.Add(status[1]);
                    pm.Offsets.Add(0);

                    while (rdReply.Status == 0x06)
                    {
                        rdReply = LogixServices.ReadLogixDataFragmented(_parent.SessionInfo, tag.Address, tag.Elements,
                                                                        offset, _parent.SyncRoot); //ToDo _parent.SyncRoot
                        request = LogixServices.BuildFragmentedReadDataRequest(tag.Address, tag.Elements,
                                                                               offset, out temp);
                        status = FindPacketOrCreate(request.Pack(), (ushort)(rdReply.Data.Length + 2));
                        pm.PacketIndex.Add(status[0]);
                        pm.ServiceIndex.Add(status[1]);
                        offset += (uint)rdReply.Data.Length;
                        pm.Offsets.Add(offset);
                        pm.NumReplies++;
                    }
                }
                else if (rdReply.Status == 0x00 && rdReply.Data != null)
                {
                    //Full read, create the packet...
                    int[] status = FindPacketOrCreate(request.Pack(), (ushort)(rdReply.Data.Length + 2));
                    pm.PacketIndex.Add(status[0]);
                    pm.ServiceIndex.Add(status[1]);
                    pm.Offsets.Add(0);
                }

                _readPackets.Add(pm);
            }
        }