def Read_ISO18000_6C_UII(self): result = { "statusCode": 0x3F, "uiiBytes": bytes([]), } readISO18000UiiCmd = bytes([]) readISO18000UiiCmd = readISO18000UiiCmd + b'\x02' #SOH readISO18000UiiCmd = readISO18000UiiCmd + self.GetNextSeqNum() #SeqNum readISO18000UiiCmd = readISO18000UiiCmd + b'\x00\x07\x30\x10\xFF\x01\x01' #Length, CMD Group, CMD Codes, Parameter bytes readISO18000UiiCmd = readISO18000UiiCmd + CRC16.CalculateCRC( readISO18000UiiCmd) if self.__isConnected: self.__portConnection.ClearReceiveBuffer() if self.__portConnection.WriteBytesToPort( readISO18000UiiCmd) == len(readISO18000UiiCmd): answer = self.__ReadAndCheckReceivedAnswer(500) if answer["receiveOk"]: answerBytes = answer["receivedBytes"] result["statusCode"] = answerBytes[4] if answerBytes[4] != 0x00: return result #if answer OK, save received bytes in answer. Last byte to save from answer is: answer length - 2 (CRC) uiiBytes = answerBytes[5:len(answerBytes) - 2] result["uiiBytes"] = uiiBytes return result
def ReadReaderID(self): result = { "statusCode":0x3F, "readerIdBytes":bytes([]), "readerID":-1 } readReaderIdCmd = b'\x01\xF6\x00' #SOH, ReaderID Command byte, Parameter length = 0 readReaderIdCmd = readReaderIdCmd + CRC16.CalculateCRC(readReaderIdCmd) if self.__isConnected: self.__portConnection.ClearReceiveBuffer() if self.__portConnection.WriteBytesToPort(readReaderIdCmd) == len(readReaderIdCmd): answer = self.__ReadAndCheckReceivedAnswer(500) if answer["receiveOk"]: answerBytes = answer["receivedBytes"] result["statusCode"] = answerBytes[1] if answerBytes[1] != 0x00: return result #if answer OK, save reader ID bytes in answer. Last byte to save from answer is: answer length - 2 (CRC) readerIdBytes = answerBytes[3:len(answerBytes)-2] #calculate reader ID serialNumber = 0 if (answerBytes[10] & 0x80) != 0: serialNumber |= answerBytes[5] << 16 | answerBytes[4] << 8 | answerBytes[3] else: serialNumber |= answerBytes[4] << 8 | answerBytes[3] result["readerIdBytes"] = readerIdBytes result["readerID"] = serialNumber return result
def Write_ISO18000_6C_Word(self, _pageNum, _wordAddr, _dataToWrite): result = { "statusCode": 0x3F, } if len(_dataToWrite) != 2: return result #Not supported! Only write 1x word possible writeISO18000WordsCmd = bytes([]) writeISO18000WordsCmd = writeISO18000WordsCmd + b'\x02' #SOH writeISO18000WordsCmd = writeISO18000WordsCmd + self.GetNextSeqNum( ) #SeqNum writeISO18000WordsCmd = writeISO18000WordsCmd + b'\x00\x0C\x30\x14\xFF\x00' #Length, CMD Group, CMD Codes, Parameter bytes writeISO18000WordsCmd = writeISO18000WordsCmd + bytes([_pageNum]) writeISO18000WordsCmd = writeISO18000WordsCmd + bytes( [(_wordAddr & 0xFF0000) >> 16, (_wordAddr & 0xFF00) >> 8, (_wordAddr & 0xFF)]) writeISO18000WordsCmd = writeISO18000WordsCmd + _dataToWrite[0:2] writeISO18000WordsCmd = writeISO18000WordsCmd + CRC16.CalculateCRC( writeISO18000WordsCmd) if self.__isConnected: self.__portConnection.ClearReceiveBuffer() if self.__portConnection.WriteBytesToPort( writeISO18000WordsCmd) == len(writeISO18000WordsCmd): answer = self.__ReadAndCheckReceivedAnswer(500) if answer["receiveOk"]: answerBytes = answer["receivedBytes"] result["statusCode"] = answerBytes[4] return result
def Read_ISO18000_6C_Words(self, _pageNum, _wordAddr, _wordCount): result = { "statusCode": 0x3F, "dataBytes": bytes([]), } readISO18000WordsCmd = bytes([]) readISO18000WordsCmd = readISO18000WordsCmd + b'\x02' #SOH readISO18000WordsCmd = readISO18000WordsCmd + self.GetNextSeqNum( ) #SeqNum readISO18000WordsCmd = readISO18000WordsCmd + b'\x00\x0B\x30\x12\xFF\x01' #Length, CMD Group, CMD Codes, Parameter bytes readISO18000WordsCmd = readISO18000WordsCmd + bytes([_pageNum]) readISO18000WordsCmd = readISO18000WordsCmd + bytes( [(_wordAddr & 0xFF0000) >> 16, (_wordAddr & 0xFF00) >> 8, (_wordAddr & 0xFF)]) readISO18000WordsCmd = readISO18000WordsCmd + bytes([_wordCount]) readISO18000WordsCmd = readISO18000WordsCmd + CRC16.CalculateCRC( readISO18000WordsCmd) if self.__isConnected: self.__portConnection.ClearReceiveBuffer() if self.__portConnection.WriteBytesToPort( readISO18000WordsCmd) == len(readISO18000WordsCmd): answer = self.__ReadAndCheckReceivedAnswer(500) if answer["receiveOk"]: answerBytes = answer["receivedBytes"] result["statusCode"] = answerBytes[4] if answerBytes[4] != 0x00: return result #if answer OK, save received bytes in answer. Last byte to save from answer is: answer length - 2 (CRC) dataBytes = answerBytes[5:len(answerBytes) - 2] result["dataBytes"] = dataBytes return result
def __ReadAndCheckReceivedAnswer(self, _timeoutMs): result = { "receiveOk":False, "receivedBytes":bytes([]), "receivedBytesLength":0 } startTime = time.perf_counter() availableBytes = 0 bytesBuffer = bytes([]) availableBytes = self.__portConnection.CheckBytesInBuffer() while (time.perf_counter() - startTime) < (_timeoutMs / 1000): if availableBytes >= 5: break availableBytes = self.__portConnection.CheckBytesInBuffer() if availableBytes >= 5: #First 5 bytes are available --> read them & save bytesBuffer = self.__portConnection.ReadBytesFromPort(availableBytes) #To continue, at least 3 bytes are needed if len(bytesBuffer) < 3: return result #Check SOH received if bytesBuffer[0] != 0x01: return result #Calculate total answer length: SOH + status + length byte + Data(length provided in LengthByte) + CRC answerTotalLength = 3 + bytesBuffer[2] + 2; bytesToRead = answerTotalLength - len(bytesBuffer) if bytesToRead > 0: #Wait for the rest of the answer availableBytes = self.__portConnection.CheckBytesInBuffer() while (time.perf_counter() - startTime) < (_timeoutMs / 1000): if availableBytes >= bytesToRead: break availableBytes = self.__portConnection.CheckBytesInBuffer() if bytesToRead >= availableBytes: #At least expected bytes available --> read them bytesBuffer = bytesBuffer + self.__portConnection.ReadBytesFromPort(bytesToRead) #Now all bytes should be read --> check length if len(bytesBuffer) != answerTotalLength: return result #Check CRC crc = CRC16.CalculateCRC(bytesBuffer) if crc[0] != 0: return result if crc[1] != 0: return result #CRC is OK --> set variables and return result["receivedBytes"] = bytesBuffer result["receivedBytesLength"] = len(bytesBuffer) result["receiveOk"] = True return result else: return result
def Write_ISO15693_Block(self, _blockNum, _dataToWrite): result = { "statusCode":0x3F, } if len(_dataToWrite) != 4: return result #Not supported by this sample code writeIso15693BlockCmd = b'\x01\x61\x05' #SOH, ISO15693 Write Block Command byte, Parameter length = 5 writeIso15693BlockCmd = writeIso15693BlockCmd + bytes([_blockNum]) writeIso15693BlockCmd = writeIso15693BlockCmd + _dataToWrite[0:4] writeIso15693BlockCmd = writeIso15693BlockCmd + CRC16.CalculateCRC(writeIso15693BlockCmd) if self.__isConnected: self.__portConnection.ClearReceiveBuffer() if self.__portConnection.WriteBytesToPort(writeIso15693BlockCmd) == len(writeIso15693BlockCmd): answer = self.__ReadAndCheckReceivedAnswer(500) if answer["receiveOk"]: answerBytes = answer["receivedBytes"] result["statusCode"] = answerBytes[1] return result
def Read_ISO15693_UID(self): result = { "statusCode":0x3F, "uidBytes":bytes([]) } readIso15693UidCmd = b'\x01\x20\x00' #SOH, ISO15693 UID Command byte, Parameter length = 0 readIso15693UidCmd = readIso15693UidCmd + CRC16.CalculateCRC(readIso15693UidCmd) if self.__isConnected: self.__portConnection.ClearReceiveBuffer() if self.__portConnection.WriteBytesToPort(readIso15693UidCmd) == len(readIso15693UidCmd): answer = self.__ReadAndCheckReceivedAnswer(500) if answer["receiveOk"]: answerBytes = answer["receivedBytes"] result["statusCode"] = answerBytes[1] if answerBytes[1] != 0x00: return result #if answer OK, save reader ID bytes in answer. Last byte to save from answer is: answer length - 2 (CRC) uidBytes = answerBytes[3:len(answerBytes)-2] result["uidBytes"] = uidBytes return result
def ReadReaderID(self): result = { "statusCode": 0x3F, "readerIdBytes": bytes([]), "readerID": -1 } readReaderIdCmd = bytes([]) readReaderIdCmd = readReaderIdCmd + b'\x02' #SOH readReaderIdCmd = readReaderIdCmd + self.GetNextSeqNum() #SeqNum readReaderIdCmd = readReaderIdCmd + b'\x00\x06\x10\x10\xFF\x01' #Length, CMD Group, CMD Codes, Parameter byte readReaderIdCmd = readReaderIdCmd + CRC16.CalculateCRC(readReaderIdCmd) if self.__isConnected: self.__portConnection.ClearReceiveBuffer() if self.__portConnection.WriteBytesToPort(readReaderIdCmd) == len( readReaderIdCmd): answer = self.__ReadAndCheckReceivedAnswer(500) if answer["receiveOk"]: answerBytes = answer["receivedBytes"] result["statusCode"] = answerBytes[4] if answerBytes[4] != 0x00: return result #if answer OK, save reader ID bytes in answer. Last byte to save from answer is: answer length - 2 (CRC) readerIdBytes = answerBytes[5:len(answerBytes) - 2] #calculate reader ID serialNumber = 0 if (answerBytes[12] & 0x80) != 0: serialNumber |= answerBytes[7] << 16 | answerBytes[ 6] << 8 | answerBytes[5] else: serialNumber |= answerBytes[6] << 8 | answerBytes[5] result["readerIdBytes"] = readerIdBytes result["readerID"] = serialNumber return result