import time
from gurux_common import ReceiveParameters
from gurux_common import IGXMediaListener
from gurux_common.enums.TraceLevel import TraceLevel
from gurux_serial import GXSerial
#pylint: disable=no-self-argument
class sampleclient(IGXMediaListener):
def __init__(self):
#List available serial ports.
print("Available ports:")
print(str(GXSerial.getPortNames()))
#Define End Of Packet char.
#eop = '\r'
#eop = '\~'
#eop = None
eop = 0x7E
#eop = ''
#TODO: Update correct port and serial port settings.
media = GXSerial("COM3")
#Start to listen events from the media.
media.addListener(self)
media.
#Show all traces.
media.trace = TraceLevel.VERBOSE
#Set EOP for the media.
media.eop = eop
try:
#Open the connection.
media.open()
r = ReceiveParameters()
r.eop = eop
#Minimium amount of bytes to receive.
r.count = 100
#Wait reply for 2 seconds.
r.waitTime = 1000
############################
#Send data synchronously.
with media.getSynchronous():
list1 = [0x7E, 0xA0, 0x07, 0x03, 0x21, 0x53, 0x03, 0xC7, 0x7E]
list1 = bytearray(list1)
#media.send("Hello world!")
media.send(list1)
#Send EOP
#media.send('\r')
ret = media.receive(r)
if ret:
print(str(r.reply.decode("ascii")))
else:
raise Exception("Failed to receive reply from the server.")
############################
#Send async data.
media.send("Notify from the meter!\r")
#Wait 1 second to receive reply from the server.
time.sleep(1)
except Exception as ex:
print(ex)
media.close()
media.removeListener(self)
def onError(self, sender, ex):
"""
Represents the method that will handle the error event of a Gurux
component.
sender : The source of the event.
ex : An Exception object that contains the event data.
"""
print("Error has occured. " + str(ex))
def onReceived(self, sender, e):
"""Media component sends received data through this method.
sender : The source of the event.
e : Event arguments.
"""
print("New data is received. " + str(e))
def onMediaStateChange(self, sender, e):
"""Media component sends notification, when its state changes.
sender : The source of the event.
e : Event arguments.
"""
print("Media state changed. " + str(e))
def onTrace(self, sender, e):
"""Called when the Media is sending or receiving data.
sender : The source of the event.
e : Event arguments.
"""
print("trace:" + str(e))
def onPropertyChanged(self, sender, e):
"""
Event is raised when a property is changed on a component.
sender : The source of the event.
e : Event arguments.
"""
print("Property {!r} has hanged.".format(str(e)))
if __name__ == '__main__':
sampleclient()
Hi, You need to use DLMS…
Hi,
You need to use DLMS client example for that.
https://github.com/Gurux/Gurux.DLMS.Python/tree/master/Gurux.DLMS.Clien…
BR,
Mikko