static bool ReadResponseDataBlockResult(GXDLMSSettings settings, GXReplyData reply, int index)
{
reply.Error = 0;
byte lastBlock = reply.Data.GetUInt8();
// Get Block number.
UInt32 number = reply.Data.GetUInt16();
int blockLength = GXCommon.GetObjectCount(reply.Data);
// Check block length when all data is received.
if ((reply.MoreData & RequestTypes.Frame) == 0)
{
if (blockLength != reply.Data.Size - reply.Data.Position)
{
throw new OutOfMemoryException();
}
reply.Command = Command.None;
if (reply.Xml != null)
{
reply.Data.Trim();
reply.Xml.AppendStartTag(Command.ReadResponse, SingleReadResponse.DataBlockResult);
reply.Xml.AppendLine(TranslatorTags.LastBlock, "Value", reply.Xml.IntegerToHex(lastBlock, 2));
reply.Xml.AppendLine(TranslatorTags.BlockNumber, "Value", reply.Xml.IntegerToHex(number, 4));
reply.Xml.AppendLine(TranslatorTags.RawData, "Value", GXCommon.ToHex(reply.Data.Data, false, 0, reply.Data.Size));
reply.Xml.AppendEndTag(Command.ReadResponse, SingleReadResponse.DataBlockResult);
return false;
}
}
GetDataFromBlock(reply.Data, index);
// Is not Last block.
if (lastBlock == 0)
{
reply.MoreData = (RequestTypes)(reply.MoreData | RequestTypes.DataBlock);
}
else
{
reply.MoreData = (RequestTypes)(reply.MoreData & ~RequestTypes.DataBlock);
}
//If meter's block index is zero based.
if (number != 1 && settings.BlockIndex == 1)
{
settings.BlockIndex = (uint)number;
}
UInt32 expectedIndex = settings.BlockIndex;
if (number != expectedIndex)
{
throw new ArgumentException(
"Invalid Block number. It is " + number
+ " and it should be " + expectedIndex + ".");
}
// If last packet and data is not try to peek.
if (reply.MoreData == RequestTypes.None)
{
reply.Data.Position = 0;
HandleReadResponse(settings, reply, index);
settings.ResetBlockIndex();
}
return true;
}