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
image changed
gurux.dlms GXDLMSClient class screen shot
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