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. Se Connecter À Plusieurs Compteurs Sur La Même Adresse IP Et Le Même Port.

Se connecter à plusieurs compteurs sur la même adresse IP et le même port.

By anne, 30 March, 2026
Forums
Gurux.DLMS

Bonjour, j'ai 350 compteurs smartESOX de chez APATOR et 33 concentrateurs.
Cependant je n'arrive pas à me connecter à plusieurs compteurs sur la même adresse IP et le même port. J'ai donc crée un programme de test qui est composé de 3 compteurs qui partagent la même adresse ip et le même port.
Je réussi à me connecter au premier compteur mais les deux autres compteurs échouent au niveau de _reader.InitializeConnection(); .
Vous trouverez ci dessous le code de mon programme de test. j'utilise c#

*MeterConnection*

using Gurux.Common;
using Gurux.DLMS;
using Gurux.DLMS.Enums;
using Gurux.DLMS.Objects;
using Gurux.DLMS.Reader;
using Gurux.DLMS.Secure;
using Gurux.Net;
using System;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;

namespace MeterConnection
{
public class MeterConnection : IDisposable
{
private GXNet _media; // ← Plus readonly pour pouvoir recréer
private readonly GXDLMSSecureClient _client;
private GXDLMSReader _reader;
private readonly string _meterName;
private readonly string _serverIp;
private readonly int _port;
private readonly object _lock = new object();

public bool IsConnected { get; private set; }

public MeterConnection(
string serverIp,
int port,
int clientAddress,
int PhysicalAddress,
string authenticationKey,
string unicastKey,
string password,
string meterName)
{
_meterName = meterName;
_serverIp = serverIp;
_port = port;

var authKey = GXCommon.HexToBytes(authenticationKey);
var blockKey = GXCommon.HexToBytes(unicastKey);
byte[] systemTitle = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

_client = new GXDLMSSecureClient(
true, // useLogicalNameReferencing
clientAddress, // clientAddress
PhysicalAddress, // serverAddress
Authentication.HighGMAC, // authentication
null, // password
InterfaceType.HDLC
);

_client.Ciphering.Security = Security.AuthenticationEncryption;
_client.Ciphering.AuthenticationKey = authKey;
_client.Ciphering.BlockCipherKey = blockKey;
_client.ServerAddress = GXDLMSClient.GetServerAddress(1, PhysicalAddress);

//_client.Ciphering.SystemTitle = systemTitle;
}

public void Connect()
{
lock (_lock)
{
if (IsConnected) return;

Console.WriteLine($"[{_meterName}] Connexion...");

// ← CRUCIAL : Crée une NOUVELLE instance GXNet à chaque fois
_media = new GXNet(NetworkType.Tcp, _serverIp, _port);
_media.Open();

_reader = new GXDLMSReader(_client, _media, TraceLevel.Info, null);

// Augmente les timeouts pour le debug
_reader.WaitTime = 10000; // 10 secondes
_reader.RetryCount = 3;

try
{
_reader.InitializeConnection();
IsConnected = true;
Console.WriteLine($"[{_meterName}] ✅ Connecté");
}
catch (Exception ex)
{
// Nettoyage immédiat en cas d'échec
Cleanup();
throw;
}
}
}

public object Read(string logicalName, int attributeIndex)
{
lock (_lock)
{
if (!IsConnected)
throw new InvalidOperationException("Non connecté");

var obj = CreateObject(logicalName);
return _reader.Read(obj, attributeIndex);
}
}

private GXDLMSObject CreateObject(string logicalName)
{
var parts = logicalName.Split('.');
int c = int.Parse(parts[2]);

ObjectType objectType = c switch
{
0 => ObjectType.Data,
1 => ObjectType.Register,
3 => ObjectType.RegisterActivation,
4 => ObjectType.ExtendedRegister,
5 => ObjectType.DemandRegister,
7 => ObjectType.ProfileGeneric,
_ => ObjectType.Data
};

var obj = GXDLMSClient.CreateObject(objectType);
obj.LogicalName = logicalName;
return obj;
}

public void Disconnect()
{
lock (_lock)
{
if (!IsConnected) return;

Console.WriteLine($"[{_meterName}] Déconnexion...");
Cleanup();
Console.WriteLine($"[{_meterName}] ✅ Déconnecté");
}
}

/// <summary>
/// Nettoyage complet des ressources
/// </summary>
private void Cleanup()
{
// 1. Ferme le reader (Release + Disconnect)
try
{
_reader?.Close();
}
catch (Exception ex)
{
Console.WriteLine($"[{_meterName}] Warning Close: {ex.Message}");
}

// 2. Ferme explicitement la media
try
{
_media?.Close();
}
catch (Exception ex)
{
Console.WriteLine($"[{_meterName}] Warning Media.Close: {ex.Message}");
}

// 3. Dispose la media
try
{
_media?.Dispose();
}
catch (Exception ex)
{
Console.WriteLine($"[{_meterName}] Warning Media.Dispose: {ex.Message}");
}

_reader = null;
_media = null;
IsConnected = false;
}

public void Dispose()
{
Disconnect();
}
}
}

*Program*

using Gurux.DLMS;
using Gurux.DLMS.Enums;
using System;
using System.Collections.Generic;
using System.Threading.Tasks; // ← Ajout explicite

namespace MeterConnection
{
class Program
{
// ← Retourne Task pour éviter CS0161
static async System.Threading.Tasks.Task Main(string[] args)
{
const string SERVER_IP = "10.60.7.67";
const int SERVER_PORT = 81;

var meters = new[]
{

};

var connections = new List<MeterConnection>();

try
{
foreach (var m in meters)
{
var PhysicalAddress = int.Parse(m.Name.Substring(m.Name.Length - 4)) + 100;
var conn = new MeterConnection(
SERVER_IP,
SERVER_PORT,
m.ClientAddr,
PhysicalAddress,
m.AuthenticationKey,
m.UnicastKey,
m.Password,
m.Name
);

connections.Add(conn);
conn.Connect();
await System.Threading.Tasks.Task.Delay(300);
}

foreach (var conn in connections)
{
try
{
var datetime = conn.Read("0.0.1.0.0.255", 2);
Console.WriteLine($"{conn}: DateTime = {datetime}");
}
catch (Exception ex)
{
Console.WriteLine($"Erreur lecture: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Erreur: {ex}");
}
finally
{
foreach (var conn in connections)
{
conn.Dispose();
}
}

Console.WriteLine("Terminé. Appuyez sur une touche...");
Console.ReadKey();

// ← Retour explicite
return;
}
}
}

Profile picture for user Kurumi

Kurumi

2 months ago

Hi, It seems like your…

Hi,

It seems like your concentrator doesn't allow multiple connections at the same time.

You need to create a client app that establishes the connection to the meter.
Then the client application reads meters behind the concentrator one at a time.

BR,
Mikko

  • Create new account
  • Reset your password

Hire Us!

Latest Releases

  • 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
  • Thu, 05/28/2026 - 13:16
    Gurux.DLMS.Net 9.0.2605.2801
  • Thu, 05/28/2026 - 13:14
    Gurux.DLMS.Python 1.0.198

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