Gurux.DLMS.Objects.GXDLMSImageTransfer.ImageBlockTransfer C# (CSharp) Method

ImageBlockTransfer() public method

public ImageBlockTransfer ( Gurux.DLMS.GXDLMSClient client, byte imageBlockValue, int &ImageBlockCount ) : byte[][]
client Gurux.DLMS.GXDLMSClient
imageBlockValue byte
ImageBlockCount int
return byte[][]
        public byte[][] ImageBlockTransfer(GXDLMSClient client, byte[] imageBlockValue, out int ImageBlockCount)
        {
            ImageBlockCount = (int)(imageBlockValue.Length / ImageBlockSize);
            if (imageBlockValue.Length % ImageBlockSize != 0)
            {
                ++ImageBlockCount;
            }
            List<byte[]> packets = new List<byte[]>();
            for (int pos = 0; pos != ImageBlockCount; ++pos)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8((byte)2);
                GXCommon.SetData(client.Settings, data, DataType.UInt32, pos);
                byte[] tmp;
                int bytes = (int)(imageBlockValue.Length - ((pos + 1) * ImageBlockSize));
                //If last packet
                if (bytes < 0)
                {
                    bytes = (int)(imageBlockValue.Length - (pos * ImageBlockSize));
                    tmp = new byte[bytes];
                    Array.Copy(imageBlockValue, pos * ImageBlockSize, tmp, 0, bytes);
                }
                else
                {
                    tmp = new byte[ImageBlockSize];
                    Array.Copy(imageBlockValue, (pos * ImageBlockSize), tmp, 0, ImageBlockSize);
                }
                GXCommon.SetData(client.Settings, data, DataType.OctetString, tmp);
                packets.AddRange(client.Method(this, 2, data.Array(), DataType.Array));
            }
            return packets.ToArray();
        }

Usage Example

        public void UpdateImage(GXDLMSImageTransfer target, byte[] data, string Identification)
        {
            //Check that image transfer ia enabled.
            byte[] reply = ReadDataBlock(Client.Read(target, 5));
            Client.UpdateValue(reply, target, 5);
            if (!target.ImageTransferEnabled)
            {
                throw new Exception("Image transfer is not enabled");
            }

            //Step 1: The client gets the ImageBlockSize.
            reply = ReadDataBlock(Client.Read(target, 2));
            Client.UpdateValue(reply, target, 2);

            // Step 2: The client initiates the Image transfer process.
            ReadDataBlock(target.ImageTransferInitiate(Client, Identification, data.Length));
            // Step 3: The client transfers ImageBlocks.
            int ImageBlockCount;
            ReadDataBlock(target.ImageBlockTransfer(Client, data, out ImageBlockCount));
            //Step 4: The client checks the completeness of the Image in
            //each server individually and transfers any ImageBlocks not (yet) transferred;
            Client.UpdateValue(reply, target, 2);

            // Step 5: The Image is verified;
            ReadDataBlock(target.ImageVerify(Client));
            // Step 6: Before activation, the Image is checked;

            //Get list to imaages to activate.
            reply = ReadDataBlock(Client.Read(target, 7));
            Client.UpdateValue(reply, target, 7);
            bool bFound = false;
            foreach (GXDLMSImageActivateInfo it in target.ImageActivateInfo)
            {
                if (it.Identification == Identification)
                {
                    bFound = true;
                    break;
                }
            }

            //Read image transfer status.
            reply = ReadDataBlock(Client.Read(target, 6));
            Client.UpdateValue(reply, target, 6);
            if (target.ImageTransferStatus != ImageTransferStatus.VerificationSuccessful)
            {
                throw new Exception("Image transfer status is " + target.ImageTransferStatus.ToString());
            }

            if (!bFound)
            {
                throw new Exception("Image not found.");
            }

            //Step 7: Activate image.
            ReadDataBlock(target.ImageActivate(Client));
        }