Search code examples
pythoncctypesraspberry-pi2rfid

SAAT-500 Series Active RFID in Python using C code PROJECT


We are building a project using active RFID, and this RFID needs to be coded on Python to use it on Raspberry PI3

SAAT RFID already has DLL file, RFIDAPI.lib and RFIDAPIEXPORT.h and various API calling function

For instance and the basic code I need to execute

bool SAAT_TCPInit (void** pHandle,char *pHostName,int nsocketPort)
HANDLE hp; if(!SAAT_TCPInit(&hp,”192.168.0.238”,7086) ) 
{ 
    printf("reader initialization failed!\n"); return false; 
} 

How can I convert this code into Python to get into RFID?

enter image description here


Solution

  • Untested, but illustrates what you need to do. I assumed Python 3, but Python 2 is similar.

    #!python3
    import ctypes
    
    # import the DLL
    dll = ctypes.CDLL('RFIDAPI')
    
    # declare the argument and return value types
    dll.SAAT_TCPInit.argtypes = ctypes.POINTER(ctypes.c_void_p),ctypes.c_char_p,ctypes.c_int)
    dll.SAAT_TCPInit.restype = ctypes.c_bool
    
    # For the output parameter, create an instance to be passed by reference.
    hp = ctypes.c_void_p()
    if not dll.SAAT_TCPInit(ctypes.byref(hp),b'192.168.0.238',7086):
        print('reader initialization failed!')
    

    Note the byte string for the IP. In Python 3 byte strings are the correct input for c_char_p.