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

1 year 11 months ago

image changed

gurux.dlms GXDLMSClient class screen shot

Image
Profile picture for user Kurumi

Kurumi

1 year 11 months 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

  • Tue, 06/09/2026 - 11:16
    gurux.dlms.java 4.0.95
  • Tue, 06/09/2026 - 10:03
    Gurux.DLMS.Python 1.0.199
  • Mon, 06/08/2026 - 13:39
    gurux.dlms.cpp 9.0.2606.0801
  • Mon, 06/01/2026 - 10:15
    gurux.dlms.cpp 9.0.2606.0101
  • Thu, 05/28/2026 - 16:06
    gurux.dlms.java 4.0.94

New forum topics

  • Error reading L&G Meter
  • Pass a TCP Client to GXNet
  • Australian EDMI Mk10D (Essential Energy area)
  • Strange mix of data notificiation vs get response
  • DLMS Connection
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin