Skip to main content
Home
for DLMS smart meters
Open source solutions for DLMS smart metering

Main navigation

  • Home
  • Products
  • About us
  • Open Source
  • Community
  • Forum
  • Downloads
User account menu
  • Log in

Breadcrumb

  1. Home
  2. How To Get Code To Convert Response Raw Data To String Using Dlms C# Library

How to get code to convert response raw data to string using dlms c# library

By jayaram , 8 July, 2024
Forums
Gurux.DLMS

i am trying to connect to smart meter through Medial type serial and i got the response properly

here is my code

public class Test
{
static GXDLMSClient client = new GXDLMSClient();
public static IGXMedia Media = null;
public static TraceLevel Trace = TraceLevel.Info;

public static void Main()
{
GXSerial serial = new GXSerial();
serial.PortName = "COM3";
serial.BaudRate = 9600;
serial.Parity = Parity.None;
serial.StopBits = StopBits.One;

// Is used Logican Name or Short Name referencing.
client.UseLogicalNameReferencing = true;

// Is used HDLC or COSEM transport layers for IPv4 networks (IEC 62056-47)
client.InterfaceType = InterfaceType.HDLC;
client.ClientAddress = 32;
client.ServerAddress = 1;
client.Authentication = Authentication.Low;
client.Password = ASCIIEncoding.ASCII.GetBytes("1111111111111111");
Media = serial as IGXMedia;
Media.Open();
GXReplyData reply = new GXReplyData();
byte[] data;
data = client.SNRMRequest();
if (data != null)
{
ReadDLMSPacket(data, reply);
//Has server accepted client.
client.ParseUAResponse(reply.Data);
}

//Generate AARQ request.
//Split requests to multiple packets if needed.
//If password is used all data might not fit to one packet.
foreach (byte[] it in client.AARQRequest())
{
reply.Clear();
reply = ReadDLMSPacket(it, reply);
}

//Parse reply.
client.ParseAAREResponse(reply.Data);
Console.WriteLine("Conformance: " + client.NegotiatedConformance);
Console.WriteLine(client.ConnectionState);
byte[][] obj = client.Read("0.0.96.1.0.255", ObjectType.Data,2);
reply = new GXReplyData();
ReadDataBlock(obj, reply);
}

in the above code last line ReadDataBlock(obj, reply); reply.value contains response that is in readable format only

but i need a logic how raw data of response is converting to readable string

i debug the gurux.dlms class library

[Obsolete("Use Read")]
public byte[][] Read(object name, ObjectType objectType, int attributeOrdinal)
{
return Read(name, objectType, attributeOrdinal, null, 0);
}

private byte[][] Read(object name, ObjectType objectType, int attributeOrdinal, GXByteBuffer data, int mode)
{
if (attributeOrdinal < 0)
{
throw new ArgumentException("Invalid parameter");
}

Settings.ResetBlockIndex();
GXByteBuffer gXByteBuffer = new GXByteBuffer();
if (UseLogicalNameReferencing)
{
gXByteBuffer.SetUInt16((ushort)objectType);
gXByteBuffer.Set(GXCommon.LogicalNameToBytes((string)name));
gXByteBuffer.SetUInt8((byte)attributeOrdinal);
if (data == null || data.Size == 0)
{
gXByteBuffer.SetUInt8(0);
}
else
{
gXByteBuffer.SetUInt8(1);
}

return GXDLMS.GetLnMessages(new GXDLMSLNParameters(Settings, 0u, Command.GetRequest, 1, gXByteBuffer, data, byte.MaxValue, Command.None)
{
AccessMode = mode
});
}

above read method is returning the expected result

but i need to check GXDLMS.GetLnMessages method logic but i am unable see the source code

jayaram

2 years 1 month ago

image changed

gurux.dlms GXDLMSClient class screen shot

Image
Profile picture for user Kurumi

Kurumi

2 years 1 month ago

Hi, Use this code instead of…

Hi,

Use this code instead of. It will update the received bytes to the correct format.

GXReplyData reply = new GXReplyData();
GXDLMSData obj = new GXDLMSData("0.0.96.1.0.255");
ReadDLMSPacket(Read(obj, 2), reply);
client.UpdateValue(obj, 2, reply.Value);

BR,
Mikko

  • Create new account
  • Reset your password

Hire Us!

Latest Releases

  • Mon, 08/17/2026 - 11:28
    Gurux.DLMS.Python 1.0.202
  • Mon, 08/17/2026 - 09:47
    gurux.dlms.cpp 9.0.2608.1701
  • Mon, 08/17/2026 - 09:25
    gurux.dlms.c 9.0.2608.1701
  • Mon, 07/27/2026 - 12:37
    gurux.dlms.cpp 9.0.2607.2701
  • Thu, 07/23/2026 - 16:19
    gurux.dlms.java 4.0.96

New forum topics

  • negative indexing for custom method/attribute
  • System title
  • Elster a1800 clock reset to year 2000 with one tariff billing
  • Possible memory leak in file dlms.c, getCompactArrayItem(), getCompactArrayItem2()
  • Official Gurux.DLMS.Client.Example.Net fails during High GMAC secure association (Invalid PDU), while Gurux Director connects successfully
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin