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

11 months 2 weeks ago

image changed

gurux.dlms GXDLMSClient class screen shot

Image
Profile picture for user Kurumi

Kurumi

11 months 2 weeks 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

  • Log in or register to post comments
  • Create new account
  • Reset your password

Hire Us!

Latest Releases

  • Thu, 06/19/2025 - 09:33
    Gurux.DLMS.Python 1.0.185
  • Wed, 06/18/2025 - 15:11
    Gurux.DLMS.Python 1.0.184
  • Wed, 06/18/2025 - 10:05
    Gurux.DLMS.Python 1.0.183
  • Wed, 06/18/2025 - 09:06
    GXDLMSDirector 9.0.2506.1801
  • Wed, 06/18/2025 - 08:41
    Gurux.DLMS.Net 9.0.2506.1801

New forum topics

  • Unable to read parameter values from Landis+Gyr E550 meter using GXDLMSDirector - NoAccess status
  • data.SetComplete(false); But all data recieved
  • Crash (endless loop)
  • Server is not able to download object list with maxPduSize 64 or 128 (or anything below 156-bytes PDU size)
  • Day profile action item add error (String is not recognized as valid DateTime value)
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin