def initializeConnection(self): print("Standard: " + str(self.client.standard)) if self.client.ciphering.security != Security.NONE: print("Security: " + str(self.client.ciphering.security)) print("System title: " + GXCommon.toHex(self.client.ciphering.systemTitle)) print("Authentication key: " + GXCommon.toHex(self.client.ciphering.authenticationKey)) print("Block cipher key: " + GXCommon.toHex(self.client.ciphering.blockCipherKey)) if self.client.ciphering.dedicatedKey: print("Dedicated key: " + GXCommon.toHex(self.client.ciphering.dedicatedKey)) self.updateFrameCounter() self.initializeOpticalHead() 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: try: for it in self.client.getApplicationAssociationRequest(): self.readDLMSPacket(it, reply) self.client.parseApplicationAssociationResponse(reply.data) except GXDLMSException as ex: #Invalid password. raise GXDLMSException(AssociationResult.PERMANENT_REJECTED, SourceDiagnostic.AUTHENTICATION_FAILURE)
def initializeConnection(self): print("Standard: " + str(self.client.standard)) if self.client.ciphering.security != Security.NONE: print("Security: " + str(self.client.ciphering.security)) print("System title: " + GXCommon.toHex(self.client.ciphering.systemTitle)) print("Authentication key: " + GXCommon.toHex(self.client.ciphering.authenticationKey)) print("Block cipher key: " + GXCommon.toHex(self.client.ciphering.blockCipherKey)) if self.client.ciphering.dedicatedKey: print("Dedicated key: " + GXCommon.toHex(self.client.ciphering.dedicatedKey)) self.updateFrameCounter() self.initializeOpticalHead() 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)
def printData(cls, value, offset): sb = ' ' * 2 * offset if isinstance(value, list): print(sb + "{") offset = offset + 1 #Print received data. for it in value: cls.printData(it, offset) print(sb + "}") offset = offset - 1 elif isinstance(value, bytearray): #Print value. print(sb + GXCommon.toHex(value)) else: #Print value. print(sb + str(value))
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)