using System;
using Gurux.Common;
using Gurux.DLMS;
using Gurux.DLMS.Enums;
using Gurux.Net;
using System.Text;
class Program
{
static void Main(string[] args)
{
GXNet connection = null;
try
{
// Create and configure the GXDLMSClient
var client = new GXDLMSClient
{
InterfaceType = InterfaceType.General,
UseLogicalNameReferencing = true,
ClientAddress = 0x21,
ServerAddress = 0x3,
Authentication = Authentication.None // No authentication
};
// Create and configure the connection
connection = new GXNet
{
HostName = "1111:1111::2",
Port = 3333
};
// Open the connection
connection.Open();
if (!connection.IsOpen)
{
throw new Exception("Connection could not be opened.");
}
GXReplyData reply = new GXReplyData();
// Send SNRM request and handle response
byte[] snrmRequest = client.SNRMRequest();
if (snrmRequest != null)
{
ReadDLMSPacket(connection, snrmRequest, reply);
client.ParseUAResponse(reply.Data);
Console.WriteLine("SNRM/UA negotiation succeeded.");
}
else
{
throw new Exception("SNRM request is null.");
}
// Send AARQ request and handle AARE response
foreach (byte[] aarqRequest in client.AARQRequest())
{
if (aarqRequest != null)
{
reply.Clear();
ReadDLMSPacket(connection, aarqRequest, reply);
client.ParseAAREResponse(reply.Data);
}
else
{
throw new Exception("AARQ request is null.");
}
}
Console.WriteLine("AARQ/AARE negotiation succeeded.");
Console.WriteLine("Connection established successfully!");
// Read and display data from the meter
ReadDataFromMeter(client, connection);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
// Ensure the connection is closed
if (connection != null && connection.IsOpen)
{
connection.Close();
}
}
}
// This method reads DLMS packets from the connection
private static void ReadDLMSPacket(GXNet connection, byte[] data, GXReplyData reply)
{
try
{
connection.Send(data, data.Length);
connection.Receive(new ReceiveParameters<byte[]> { AllData = true, Eop = (byte)0x7E, Count = 5, WaitTime = 1000 });
reply.Data = connection.Receive(new ReceiveParameters<byte[]> { AllData = true, Eop = (byte)0x7E, Count = 5, WaitTime = 1000 });
}
catch (Exception ex)
{
throw new Exception("Error reading DLMS packet: " + ex.Message);
}
}
// Reads data from the meter and displays it on the console
private static void ReadDataFromMeter(GXDLMSClient client, GXNet connection)
{
try
{
// Send the get objects request
byte[] getObjectsRequest = client.GetObjectsRequest();
GXReplyData reply = new GXReplyData();
ReadDLMSPacket(connection, getObjectsRequest, reply);
// Parse the response
var objects = client.ParseObjects(reply.Data, true);
foreach (var obj in objects)
{
// Read the value of each object
// Assuming 'Read' method requires object reference and possibly other parameters
var value = client.Read(obj.ObjectType, obj.LogicalName, null); // Adjust arguments as needed
Console.WriteLine($"Object: {obj.LogicalName}, Value: {value}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading data from meter: {ex.Message}");
}
}
}
Severity Code Description…
Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type 'byte[][]' to 'byte[]' dlms C:\Users\Administrator.WIN-1HTSJ8KGNJH\Desktop\dlms\Program.cs 112 Active
Error CS1503 Argument 2: cannot convert from 'string' to 'Gurux.DLMS.Enums.ObjectType' dlms C:\Users\Administrator.WIN-1HTSJ8KGNJH\Desktop\dlms\Program.cs 122 Active
Error CS1503 Argument 3: cannot convert from '<null>' to 'int' dlms C:\Users\Administrator.WIN-1HTSJ8KGNJH\Desktop\dlms\Program.cs 122 Active
Hi, Does this error occur…
Hi,
Does this error occur when you are reading objects or when reading the association view?
BR,
Mikko