예제 #1
0
 def leer_bloques(self, tag, nro_bloque, cantidad):
     """ Envía al lector el comando para leer N bloques del tag y retorna su
         valor como string.
         Si cantidad = 0, devuelve None
         Ejemplo:
         In [10]: l.leer_bloques(t, 0, 1)
         Out[10]: '~\x01\x00\x06'
     """
     if not self.__inicializado or cantidad <=0:
         return None
     # Habilito el Flag Option
     readn_cmd = '186223' if self.TAG_TEXAS else '182223'
     readn_cmd = readn_cmd + change_byte_order(tag.serial).upper() + \
                 '%02x%02x' % (nro_bloque, cantidad-1)
     res = self._enviar_comando(readn_cmd)
     if res is not None:
         # TODO: Parsear 00 => OK, errores
         # TODO: Verificar realmente los errores!!!
         if res[:2] == '00':
             try:
                 if self.TAG_TEXAS:
                     datos = res[2:]
                     ret = ''
                     while datos:
                         datos = datos.partition('00')[2]
                         ret += binascii.a2b_hex(datos[:8])
                     return ret
                 else:
                     return binascii.a2b_hex(res[2:])
             except:
                 return None
         else:
             logger.error("Respuesta de RFID no especificada %s" % res)
     else:
         return None
예제 #2
0
 def get_multitag(self):
     """ Devuelve instancias de TAG en una lista o una lista vacía.
         ICODE2 únicamente soportado por ahora.
     """
     if not self.__inicializado:
         return None
     inventory_cmd = '14060100' # Multi slot
     self._enviar_comando(inventory_cmd, leer_respuesta=False)
     res_tags = []
     nscans = 1
     while nscans:
         for slot in range(SLOTS):
             res = self._leer_respuesta(capturar_colisiones=False)
             if res:
                 if res.startswith('017A'): # Hay conflicto el lector va a ser 2 vueltas de slots
                     nscans += 1
                     if DEBUG_RFID:
                         logger.debug('Conflicto: dos o mas tags disponibles')
                     continue
                 if res.startswith('0176'): # No hay nada
                     continue # voy al siguiente slot
                 else: # Hay un tag, parseo el string
                     res_tags.append(ICODE2(change_byte_order(res[:-4]), self,
                                     CLASE_ICODE2, self.token_id))
         nscans -= 1
     if res_tags:
         self._enviar_comando('F7FF') # Prendo el led
     else:
         self._enviar_comando('F700') # Apago el led
     if DEBUG_RFID:
         logger.debug('encontré: %i tags' % len(res_tags))
     return res_tags
예제 #3
0
 def _set_read_only(self, tag, nro_bloque):
     """ Setea un bloque del tag como de sólo lectura. """
     lock_cmd = '182022' + change_byte_order(tag.serial).upper() + \
                 '%02x' % nro_bloque
     res = self._enviar_comando(lock_cmd)
     if res:
         # TODO: Parsear 00 => OK, errores
         if res == '00':
             return True
     return None
예제 #4
0
 def is_read_only(self, tag):
     """ Chequea que si el tag en cuestión es de sólo lectura o no.
         Devuelve True en caso de que sea sólo lectura, False en caso
         contrario
     """
     if not self.__inicializado:
         return None
     security_status_cmd = '18222C' + change_byte_order(tag.serial) + \
                           '%02x%02x' % (0, tag.MAX_BLOQUE)
     res = self._enviar_comando(security_status_cmd)
     if res:
         # Devuelve un string de 0 o 1 según sea read only o no.
         try:
             return bool(int(res))
         except ValueError:
             pass
     return False
예제 #5
0
 def escribir_bloque(self, tag, nro_bloque, valor):
     """ Envía al lector el comando para escribir en un bloque del tag el
         valor que se pasa como parámetro.
         Retorna el mismo string de bytes escrito en caso de éxito, sino un
         string vacío.
     """
     if not self.__inicializado:
         return None
     # Habilito Flag Option
     write_cmd = '186221' if self.TAG_TEXAS else '182221'
     write_cmd = write_cmd + change_byte_order(tag.serial).upper() + \
                 '%02x' % nro_bloque + valor.encode('hex')
     res = self._enviar_comando(write_cmd)
     if DEBUG_RFID:
         logger.debug("devuelve: >%s<" % res)
     if res is not None:
         # TODO: Parsear 00 => OK, errores
         if res == '00':
             return valor
     return None
예제 #6
0
 def leer_bloque(self, tag, nro_bloque):
     """ Envía al lector el comando para leer un bloque del tag y retorna su
         valor como string
     """
     if not self.__inicializado:
         return None
     # Habilito el Flag Option
     read_cmd = '186220' if self.TAG_TEXAS else '182220'
     read_cmd = read_cmd + change_byte_order(tag.serial).upper() + \
                 '%02x' % nro_bloque
     res = self._enviar_comando(read_cmd)
     if res:
         # TODO: Parsear 00 => OK, errores
         limite = 4 if self.TAG_TEXAS else 2
         # TODO: Verificar realmente los errores!!!
         if res[:2] == '00':
             try:
                 return binascii.a2b_hex(res[limite:])
             except Exception, e:
                 logger.error(e)
                 return None
예제 #7
0
 def get_tag(self):
     """ Devuelve una instancia de TAG
         ICODE2 únicamente soportado por ahora.
     """
     if not self.__inicializado:
         return None
     inventory_cmd = '14260100' # One slot
     res = self._enviar_comando(inventory_cmd)
     if res:
         if res[0] == '{': # Conflicto, dos o más tags en un slot: {00}[z,40]
             self._enviar_comando('F700') # Apago el led
             if DEBUG_RFID:
                 logger.debug('Conflicto: dos o mas tags disponibles')
             return None
         elif res.startswith('0176'): # No hay nada
             self._enviar_comando('F700') # Apago el led
             return None
         else: # Hay un tag, parseo el string [78DF0E34000104E0,75]
             self._enviar_comando('F7FF') # Prendo el led
             return ICODE2(change_byte_order(res[:-4]), self, CLASE_ICODE2,
                           self.token_id)
     else:
         return None