Exemple #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);
            }
        }
Exemple #2
0
        private void WriteAll()
        {
            try
            {
                //First we are going to have to build the write requests...
                _writeMsrPackets = new List <MultiServiceRequest>();
                BuildWriteRequests();

                //Now we have to send them out...
                for (int i = 0; i < _writeMsrPackets.Count; i++)
                {
                    CommonPacketItem addressItem =
                        CommonPacketItem.GetConnectedAddressItem(_parent.SessionInfo.ConnectionParameters.O2T_CID);
                    CommonPacketItem dataItem = CommonPacketItem.GetConnectedDataItem(_writeMsrPackets[i].Pack(),
                                                                                      SequenceNumberGenerator.SequenceNumber(_parent.SyncRoot));

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

                    if (reply != null)
                    {
                        MultiServiceReply msReply = new MultiServiceReply(reply);

                        DecodeWriteReply(i, msReply);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("LogixTagGroup WriteAll: " + ex.Message + " " + ex.StackTrace);
            }
        }
        public static WriteDataServiceReply WriteLogixData(SessionInfo si, string tagAddress, ushort dataType, ushort elementCount, byte[] data, object syncRoot, ushort structHandle = 0x0000) //ToDo syncRoot
#endif
        {
            WriteDataServiceRequest request = BuildLogixWriteDataRequest(tagAddress, dataType, elementCount, data, structHandle);

            EncapsRRData rrData = new EncapsRRData();

            rrData.CPF             = new CommonPacket();
            rrData.CPF.AddressItem = CommonPacketItem.GetConnectedAddressItem(si.ConnectionParameters.O2T_CID);
            rrData.CPF.DataItem    = CommonPacketItem.GetConnectedDataItem(request.Pack(), SequenceNumberGenerator.SequenceNumber(syncRoot));
            rrData.Timeout         = 2000;

            EncapsReply reply = si.SendUnitData(rrData.CPF.AddressItem, rrData.CPF.DataItem);

            if (reply == null)
            {
                return(null);
            }

            if (reply.Status != 0)
            {
                return(null);
            }

            return(new WriteDataServiceReply(reply));
        }
Exemple #4
0
        private void ReadAll()
        {
            try
            {
                //The lock object should already be held...

                //List<MultiServiceReply> replies = new List<MultiServiceReply>();
                //List<LogixTag> updatedTags = new List<LogixTag>();

                _byteReplies = new Dictionary <int, List <byte[]> >();

                CommonPacketItem addressItem =
                    CommonPacketItem.GetConnectedAddressItem(_parent.SessionInfo.ConnectionParameters.O2T_CID);

                for (int i = 0; i < _msrPackets.Count; i++)
                {
                    CommonPacketItem dataItem = CommonPacketItem.GetConnectedDataItem(_msrPackets[i].Pack(),
                                                                                      SequenceNumberGenerator.SequenceNumber(_parent.SyncRoot));

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

                    if (reply != null)
                    {
                        //We need to suck all the replies out of the packet, and request more data if there is more
                        MultiServiceReply msReply = new MultiServiceReply(reply);

                        DecodeReadPacket(i, msReply);

                        foreach (KeyValuePair <int, List <byte[]> > kvp in _byteReplies)
                        {
                            uint offset = 0;
                            for (int i2 = 0; i2 < kvp.Value.Count; i2++)
                            {
                                if (_tags[kvp.Key].UpdateValue(kvp.Value[i2], offset))
                                {
                                    //updatedTags.Add(_tags[kvp.Key]);
                                }

                                offset += (uint)(kvp.Value[i2].Length + 6);
                            }
                        }
                        //UpdateFinished?.Invoke(this, new UpdateFinishedEventArgs(updatedTags));

                        //updatedTags.Clear();
                        _byteReplies.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("LogixTagGroup ReadAll: " + ex.Message + " " + ex.StackTrace);
            }
        }
        public static ReadDataServiceReply ReadLogixDataFragment(SessionInfo si, string tagAddress, ushort elementCount, object syncRoot) //ToDo syncRoot
        {
            int requestSize = 0;
            ReadDataServiceRequest request = BuildLogixReadDataRequestFragment(tagAddress, elementCount, out requestSize);

            EncapsRRData rrData = new EncapsRRData();

            rrData.CPF             = new CommonPacket();
            rrData.CPF.AddressItem = CommonPacketItem.GetConnectedAddressItem(si.ConnectionParameters.O2T_CID);
            rrData.CPF.DataItem    = CommonPacketItem.GetConnectedDataItem(request.Pack(), SequenceNumberGenerator.SequenceNumber(syncRoot));
            rrData.Timeout         = 2000;

            EncapsReply reply = si.SendUnitData(rrData.CPF.AddressItem, rrData.CPF.DataItem);

            if (reply == null)
            {
                return(null);
            }

            if (reply.Status != 0 && reply.Status != 0x06)
            {
                //si.LastSessionError = (int)reply.Status;
                return(null);
            }

            return(new ReadDataServiceReply(reply));
        }