Esempio n. 1
0
        public static WriteDataServiceReply WriteLogixData(SessionInfo si, string tagAddress, ushort dataType, ushort elementCount, byte[] data, ushort structHandle = 0x0000)
#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);
            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));
        }
Esempio n. 2
0
        private void FindWritePacketOrCreate(PacketMap pm, WriteDataServiceRequest request)
        {
            //The data is already broken up into appropriately sized chunks, we just have
            //to find a place to put it...

            int rSize = request.Size;

            for (int i = 0; i < _writeMsrPackets.Count; i++)
            {
                if (_writeMsrPackets[i].Size + rSize < MAX_MSR_SIZE)
                {
                    //This one will fit the packet
                    int svc = _writeMsrPackets[i].AddService(request.Pack());
                    pm.PacketIndex.Add(i);
                    pm.ServiceIndex.Add(i);
                    return;
                }
            }

            //If we got here, we have to create a new one...
            MultiServiceRequest msr = new MultiServiceRequest();

            pm.PacketIndex.Add(_writeMsrPackets.Count);
            _writeMsrPackets.Add(msr);
            pm.ServiceIndex.Add(msr.AddService(request.Pack()));
        }
Esempio n. 3
0
        internal void GenerateRequests()
        {
            int rSize = 0;

            _readRequest  = LogixServices.BuildLogixReadDataRequest(_address, _elements, out rSize);
            _writeRequest = LogixServices.BuildLogixWriteDataRequest(_address, _dataType, _elements, new byte[] { });
        }
Esempio n. 4
0
        private void DistributeWriteRequest(PacketMap pm, byte[] requestData, int alignment)
        {
            //Ok, the request may have to be broken up into multiple requests...
            if (requestData.Length > MAX_MSR_SIZE)
            {
                //This will have to be broken up... the overhead of an MSR packet is 12 bytes, so
                //that means we can only fit up to MAX_MSR_SIZE - 12 bytes into a single packet.
                //We need to figure out how much data we can stuff into one packet, then make
                //multiple ones based on that...
                //The first packet should always be the maximum size we can fit into one request...
                WriteDataServiceRequest fragReq = LogixServices.BuildFragmentedWriteRequest(
                    _tags[pm.TagIndex].Address, _tags[pm.TagIndex].DataType, _tags[pm.TagIndex].Elements,
                    0, null, _tags[pm.TagIndex].StructHandle);
                int  maxSize       = MAX_MSR_SIZE - 12 - fragReq.Size;
                int  alignedSize   = maxSize - (maxSize % alignment);
                int  remainingSize = requestData.Length;
                uint offset        = 0;

                while (remainingSize > 0)
                {
                    //We can fit up to alignedSize bytes in the array...
                    byte[] temp;
                    if (remainingSize < alignedSize)
                    {
                        temp          = new byte[remainingSize];
                        remainingSize = 0;
                    }
                    else
                    {
                        temp           = new byte[alignedSize];
                        remainingSize -= alignedSize;
                    }

                    Buffer.BlockCopy(requestData, (int)offset, temp, 0, temp.Length);

                    fragReq = LogixServices.BuildFragmentedWriteRequest(_tags[pm.TagIndex].Address,
                                                                        _tags[pm.TagIndex].DataType, _tags[pm.TagIndex].Elements, offset, temp, _tags[pm.TagIndex].StructHandle);

                    offset += (uint)temp.Length;

                    FindWritePacketOrCreate(pm, fragReq);
                }
            }
            else
            {
                //We can fit it into a single packet, we just need to find
                //one
                WriteDataServiceRequest request = LogixServices.BuildLogixWriteDataRequest(
                    _tags[pm.TagIndex].Address, _tags[pm.TagIndex].DataType, _tags[pm.TagIndex].Elements,
                    requestData, _tags[pm.TagIndex].StructHandle);

                FindWritePacketOrCreate(pm, request);
            }
        }
Esempio n. 5
0
        public static WriteDataServiceRequest BuildLogixWriteDataRequest(string tagAddress, ushort dataType, ushort elementCount, byte[] data, ushort structHandle = 0x0000)
#endif
		{
            byte[] newIOI = IOI.BuildIOI(tagAddress);

            int pathLen = newIOI.Length;
            WriteDataServiceRequest request = new WriteDataServiceRequest();
            request.Service = (byte)ControlNetService.CIP_WriteData;
            request.PathSize = (byte)(pathLen / 2);
            byte[] path = new byte[pathLen];
            Buffer.BlockCopy(newIOI, 0, path, 0, newIOI.Length);
            request.Path = path;
            request.Elements = elementCount;
            request.DataType = dataType;
            request.StructHandle = structHandle;
            request.Data = data;

            return request;
        }
        public static WriteDataServiceRequest BuildFragmentedWriteRequest(string tagAddress, ushort dataType, ushort elementCount, uint offset, byte[] data, ushort structHandle = 0x0000)
#endif
		{
            byte[] newIOI = IOI.BuildIOI(tagAddress);

            int pathLen = newIOI.Length;
            WriteDataServiceRequest request = new WriteDataServiceRequest();
            request.Service = (byte)ControlNetService.CIP_WriteDataFragmented;
            request.PathSize = (byte)(pathLen / 2);
            byte[] path = new byte[pathLen];
            Buffer.BlockCopy(newIOI, 0, path, 0, newIOI.Length);
            request.Path = path;
            request.Elements = elementCount;
            request.DataType = dataType;
            request.StructHandle = structHandle;
            request.IsFragmented = true;
            request.Offset = offset;
            request.Data = data;

            return request;
        }
Esempio n. 7
0
        internal void GenerateRequests()
        {
            int rSize = 0;
            _readRequest = LogixServices.BuildLogixReadDataRequest(_address, _elements, out rSize);
            _writeRequest = LogixServices.BuildLogixWriteDataRequest(_address, _dataType, _elements, new byte[] { });

        }