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. Crash (endless Loop)

Crash (endless loop)

By Alex S, 18 June, 2025
Forums
Gurux.DLMS

Hi Mikko!

Please help me with the following question.

I am using the standard example from the Gurux.DLMS.Client.Example.python library.

To simplify the example, I added only the reading of the serial number of the meter. And everything works fine.

Next, I enclose the code in the endless loop, so that after each successful connection and reading of the serial number, the script does not terminate, but prompts the user to press the ENTER button to repeat the operation.

And in this case, everything goes well too.

But! If after the first (second, third) successful reading I remove the optical head from the counter, the connection fails (and this is correct). If after this I run it again, it crashes. In this case, it does not matter whether the optical head is installed on the counter or not.

Apparently, some critical exception (interruption) occurs.

I would like the infinite loop not to terminate even in this case, but just prompt the user to press the ENTER button and repeat the operation.

I even formatted the part of code in the TRY-EXCEPT construction, but for some reason this exception is not caught and the program terminates.

Here is a code with endless loop:

Alex S

3 weeks 1 day ago

class sampleclient(): …

class sampleclient():
   @classmethod
   def main(cls, args):
       
       while True:
       
           os.system('cls')
           
           try:
               print("gurux_dlms version: " + pkg_resources.get_distribution("gurux_dlms").version)
               print("gurux_net version: " + pkg_resources.get_distribution("gurux_net").version)
               print("gurux_serial version: " + pkg_resources.get_distribution("gurux_serial").version)
               print("")
           except Exception:
               #It's OK if this fails.
               print("pkg_resources not found")
           # args: the command line arguments
           reader = None
           settings = GXSettings()
           try:
               # //////////////////////////////////////
               #  Handle command line parameters.
               ret = settings.getParameters(args)
               
               if ret != 0:
                   return
               # //////////////////////////////////////
               #  Initialize connection settings.
               if not isinstance(settings.media, (GXSerial, GXNet)):
                   raise Exception("Unknown media type.")
               # //////////////////////////////////////
               reader = GXDLMSReader(settings.client, settings.media, settings.trace, settings.invocationCounter)
               settings.media.open()
               if settings.readObjects:
                   read = False
                   reader.initializeConnection()
                   if settings.outputFile and os.path.exists(settings.outputFile):
                       try:
                           c = GXDLMSObjectCollection.load(settings.outputFile)
                           settings.client.objects.extend(c)
                           if settings.client.objects:
                               read = True
                       except Exception:
                           read = False
                   if not read:
                       reader.getAssociationView()
                       
                   #Reading a serial number
                   obj = settings.client.objects.findByLN(ObjectType.NONE, "0.0.96.1.0.255")
                   SN = reader.read(obj, 2) 
                   print("")
                   print(f'Serial number: {SN}')
                   print("")
               else:
                   reader.readAll(settings.outputFile)
           except (ValueError, GXDLMSException, GXDLMSExceptionResponse, GXDLMSConfirmedServiceError) as ex:
               print(ex)
           except (KeyboardInterrupt, SystemExit, Exception) as ex:
               traceback.print_exc()
               if settings.media:
                   settings.media.close()
               reader = None
           finally:
               if reader:
                   try:
                       reader.close()
                   except Exception:
                       traceback.print_exc()
               
           print("")
           print("Press ENTER to repeat...")
           input()

Alex S

3 weeks 1 day ago

I suppose the reason should…

I suppose the reason should be looked for somewhere in the module "GXDLMSReader.py".

Terminal output when connection is successful:

gurux_dlms version: 1.0.181
gurux_net version: 1.0.19
gurux_serial version: 1.0.20
Authentication: 2
ClientAddress: 0x30
ServerAddress: 0x1
Standard: 0
Serial number: 49951604
DisconnectRequest
Press ENTER to repeat...

Terminal output when connection is fail:

gurux_dlms version: 1.0.181
gurux_net version: 1.0.19
gurux_serial version: 1.0.20
Authentication: 2
ClientAddress: 0x30
ServerAddress: 0x1
Standard: 0
Data send failed.  Try to resend 1/3
Data send failed.  Try to resend 2/3
RX: 15:18:58
Traceback (most recent call last):
 File "D:\Gurux PY\GURUX_CRASH\Gurux.DLMS.Client.Example.python\main.py", line 99, in main
   reader.initializeConnection()
 File "D:\Gurux PY\GURUX_CRASH\Gurux.DLMS.Client.Example.python\GXDLMSReader.py", line 306, in initializeConnection
   self.readDLMSPacket(data, reply)
 File "D:\Gurux PY\GURUX_CRASH\Gurux.DLMS.Client.Example.python\GXDLMSReader.py", line 118, in readDLMSPacket      
   self.readDLMSPacket2(data, reply)
 File "D:\Gurux PY\GURUX_CRASH\Gurux.DLMS.Client.Example.python\GXDLMSReader.py", line 169, in readDLMSPacket2     
   raise e
 File "D:\Gurux PY\GURUX_CRASH\Gurux.DLMS.Client.Example.python\GXDLMSReader.py", line 162, in readDLMSPacket2     
   raise TimeoutException("Failed to receive reply from the device in given time.")
gurux_common.TimeoutException.TimeoutException: Failed to receive reply from the device in given time.
Press ENTER to repeat...

 

Profile picture for user Kurumi

Kurumi

3 weeks ago

Hi, The old connection is…

Hi,

The old connection is still open. You need to wait until the inactivity time has expired before you can re-read the meter. The meter doesn't reply as long as the old connection is active.

https://gurux.fi/Gurux.DLMS.Objects.GXDLMSIecHdlcSetup

BR,
Mikko

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

Hire Us!

Latest Releases

  • Wed, 07/09/2025 - 16:41
    Gurux.Serial.Android 2.0.13
  • Wed, 07/09/2025 - 12:07
    gurux.dlms.c 9.0.2507.0901
  • Sat, 07/05/2025 - 15:04
    Gurux.DLMS.Python 1.0.188
  • Tue, 07/01/2025 - 10:09
    Gurux.DLMS.Python 1.0.187
  • Tue, 07/01/2025 - 09:54
    gurux.dlms.c 9.0.2507.0101

New forum topics

  • Authentication issues (AARQ) with WASION IM300
  • Regarding the dlms android project
  • Unable to read attribute =3 (1.0.94.91.0.255) IC=7 for Indian Metering, while I'm able to read attribute 3 for other Profile generic class
  • Register Activation class (class_id = 9) attribute index 3 data is not visible
  • Load profile
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin