def readDLMSPacket2(self, data, reply): if not data: return notify = GXReplyData() reply.error = 0 eop = 0x7E #In network connection terminator is not used. if self.client.interfaceType == InterfaceType.WRAPPER and isinstance( self.media, GXNet): eop = None p = ReceiveParameters() p.eop = eop p.waitTime = self.waitTime if eop is None: p.Count = 8 else: p.Count = 5 self.media.eop = eop rd = GXByteBuffer() with self.media.getSynchronous(): if not reply.isStreaming(): self.writeTrace( "TX: " + self.now() + "\t" + GXByteBuffer.hex(data), TraceLevel.VERBOSE) print("TX: " + self.now() + "\t" + GXByteBuffer.hex(data)) self.media.send(data) pos = 0 try: while not self.client.getData(rd, reply, notify): if notify.data.size != 0: if not notify.isMoreData(): t = GXDLMSTranslator() xml = t.dataToXml(notify.data) print(xml) notify.clear() continue elif not p.eop: p.count = self.client.getFrameSize(rd) while not self.media.receive(p): pos += 1 if pos == 3: raise TimeoutException( "Failed to receive reply from the device in given time." ) if rd.size == 0: print("Data send failed. Try to resend " + str(pos) + "/3") self.media.send(data, None) rd.set(p.reply) p.reply = None except Exception as e: self.writeTrace("RX: " + self.now() + "\t" + str(rd), TraceLevel.ERROR) print("RX: " + self.now() + "\t" + str(rd)) raise e self.writeTrace("RX: " + self.now() + "\t" + str(rd), TraceLevel.VERBOSE) print("RX: " + self.now() + "\t" + str(rd)) if reply.error != 0: raise GXDLMSException(reply.error)
def __init__(self): #List available serial ports. print("Available ports:") print(str(GXSerial.getPortNames())) #Define End Of Packet char. eop = '\r' #Make connection using serial port. media = GXSerial("COM10") print(str(media)) #Start to listen events from the media. media.addListener(self) #Update port here to call onPropertyChanged event. #TODO: Update correct port. media.port = "UPDATE CORRECT PORT" #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 r.count = 5 #Wait reply for 2 seconds. r.waitTime = 2000 ############################ #Send data synchronously. with media.getSynchronous(): media.send("Hello world!") #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("Server !\r") #Wait 1 second to receive reply from the server. time.sleep(1) except (KeyboardInterrupt, SystemExit, Exception) as ex: print(ex) media.close() media.removeListener(self)
def __init__(self): #Define End Of Packet char. eop = 0x7e #Make connection using TCP/IP to localhost. #Use ::1 if you want to use IP v6 address. media = GXNet(NetworkType.TCP, "localhost", 0) #Start to listen events from the media. media.addListener(self) print(str(media)) #Update port here to call onPropertyChanged event. media.port = 1000 #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 r.count = 5 #Wait reply for 2 seconds. r.waitTime = 2000 ############################ #Send data synchronously. with media.getSynchronous(): media.send("Hello world!") #Send EOP media.send(eop) 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("Server !") media.send(eop) #Wait 1 second to receive reply from the server. time.sleep(1) except (KeyboardInterrupt, SystemExit, Exception) as ex: print(ex) media.close() media.removeListener(self)
def initializeOpticalHead(self): if self.useOpticalHead and isinstance(self.media, GXSerial): p = ReceiveParameters() p.allData = False p.eop = '\n' p.waitTime = self.waitTime with self.media.getSynchronous(): data = "/?!\r\n" self.writeTrace("TX: " + self.now() + "\t" + data, TraceLevel.VERBOSE) self.media.send(data) if not self.media.receive(p): raise Exception("Failed to received reply from the media.") self.writeTrace("RX: " + self.now() + "\t" + str(p.reply), TraceLevel.VERBOSE) #If echo is used. replyStr = str(p.reply) if data == replyStr: p.reply = None if not self.media.receive(p): raise Exception( "Failed to received reply from the media.") self.writeTrace("RX: " + self.now() + "\t" + str(p.reply), TraceLevel.VERBOSE) replyStr = str(p.reply) if not replyStr or replyStr[0] != '/': raise Exception("Invalid responce : " + replyStr) baudrate = replyStr[4] if baudrate == '0': bitrate = 300 elif baudrate == '1': bitrate = 600 elif baudrate == '2': bitrate = 1200 elif baudrate == '3': bitrate = 2400 elif baudrate == '4': bitrate = 4800 elif baudrate == '5': bitrate = 9600 elif baudrate == '6': bitrate = 19200 else: raise Exception("Unknown baud rate.") print("Bitrate is : " + bitrate) #Send ACK #Send Protocol control character controlCharacter = '2'.encode() #"2" HDLC protocol procedure (Mode E) #Mode control character #"2" //(HDLC protocol procedure) (Binary mode) modeControlCharacter = '2'.encode() #Set mode E. tmp = bytearray([ 0x06, controlCharacter, baudrate, modeControlCharacter, 13, 10 ]) p.reply = None with self.media.getSynchronous(): self.media.send(tmp) self.writeTrace( "TX: " + self.now() + "\t" + GXCommon.toHex(tmp), TraceLevel.VERBOSE) p.waitTime = 200 if self.media.receive(p): self.writeTrace("RX: " + self.now() + "\t" + str(p.reply), TraceLevel.VERBOSE) self.media.close() self.media.dataBits = 8 self.media.parity = Parity.NONE self.media.stopBits = StopBits.ONE self.media.baudRate = bitrate self.media.open() #This sleep make sure that all meters can be read. time.sleep(1000)
def initializeConnection(self): self.media.open() if self.iec and isinstance(self.media, GXSerial): p = ReceiveParameters() p.allData = False p.eop = '\n' p.waitTime = self.waitTime with self.media.getSynchronous(): data = "/?!\r\n" self.writeTrace("TX: " + self.now() + "\t" + data, TraceLevel.VERBOSE) self.media.send(data) if not self.media.receive(p): raise Exception("Failed to received reply from the media.") self.writeTrace("RX: " + self.now() + "\t" + str(p.reply), TraceLevel.VERBOSE) #If echo is used. replyStr = str(p.reply) if data == replyStr: p.reply = None if not self.media.receive(p): raise Exception( "Failed to received reply from the media.") self.writeTrace("RX: " + self.now() + "\t" + str(p.reply), TraceLevel.VERBOSE) replyStr = str(p.reply) if not replyStr or replyStr[0] != '/': raise Exception("Invalid responce : " + replyStr) baudrate = replyStr[4] if baudrate == '0': bitrate = 300 elif baudrate == '1': bitrate = 600 elif baudrate == '2': bitrate = 1200 elif baudrate == '3': bitrate = 2400 elif baudrate == '4': bitrate = 4800 elif baudrate == '5': bitrate = 9600 elif baudrate == '6': bitrate = 230400 else: raise Exception("Unknown baud rate.") print("Bitrate is : " + bitrate) #Send ACK #Send Protocol control character controlCharacter = '2'.encode() #"2" HDLC protocol procedure (Mode E) #Mode control character #"2" //(HDLC protocol procedure) (Binary mode) modeControlCharacter = '2'.encode() #Set mode E. tmp = bytearray([ 0x06, controlCharacter, baudrate, modeControlCharacter, 13, 10 ]) p.reply = None with self.media.getSynchronous(): self.media.send(tmp) self.writeTrace( "TX: " + self.now() + "\t" + GXCommon.toHex(tmp), TraceLevel.VERBOSE) p.waitTime = 200 if self.media.receive(p): self.writeTrace("RX: " + self.now() + "\t" + str(p.reply), TraceLevel.VERBOSE) self.media.close() self.media.dataBits = 8 self.media.parity = Parity.NONE self.media.stopBits = StopBits.ONE self.media.baudRate = bitrate self.media.open() #This sleep make sure that all meters can be read. time.sleep(1000) reply = GXReplyData() data = self.client.snrmRequest() if data: self.readDLMSPacket(data, reply) self.client.parseUAResponse(reply.data) size = self.client.limits.maxInfoTX + 40 self.replyBuff = bytearray(size) reply.clear() self.readDataBlock(self.client.aarqRequest(), reply) self.client.parseAareResponse(reply.data) reply.clear() if self.client.authentication > Authentication.LOW: for it in self.client.getApplicationAssociationRequest(): self.readDLMSPacket(it, reply) self.client.parseApplicationAssociationResponse(reply.data)