Skip to main content
Home
for DLMS smart meters

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 5 months ago

image changed

gurux.dlms GXDLMSClient class screen shot

Image
Profile picture for user Kurumi

Kurumi

1 year 4 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, 12/02/2025 - 10:21
    gurux.dlms.c 9.0.2512.0201
  • Mon, 12/01/2025 - 13:55
    Gurux.DLMS.Python 1.0.193
  • Thu, 11/13/2025 - 15:23
    Gurux.DLMS.Python 1.0.192
  • Thu, 11/13/2025 - 14:55
    gurux.dlms.java 4.0.84
  • Wed, 10/29/2025 - 08:51
    gurux.dlms.c 9.0.2510.2901

New forum topics

  • Incomplete Firmware Upgrade Process
  • SNRM byte mening
  • P384 Client Certificate incorrect issuer CN
  • Confirmed push service support
  • DLMS Stack hang during Firmware Upgrade
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin