Re: ftdi chip + ctypes + ftd2xx... need help with reading chipid!(now with the right source code!)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Egor Zindy

    Re: ftdi chip + ctypes + ftd2xx... need help with reading chipid!(now with the right source code!)

    Egor Zindy wrote:
    Dear List,
    >
    This one is way beyond my comprehension skills, I just don't
    understand what I'm doing wrong.
    >
    I am trying to read the chipid from an FTDI chip based USB key
    (DLP-D, http://www.ftdichip.com/Products/Eva...Kits/DLP-D.htm ),
    using:
    >
    - the ftd2xx module http://pypi.python.org/pypi/ftd2xx/0.1
    - the ftd2xx.dll which comes with the driver install
    - the chipid dll (1.1.0) from here:

    - a ctypes interface I wrote by hand (only 7 functions to wrap, I
    thought it'd be easy!)
    >
    The ftd2xx is used for testing, to open / close the device.
    >
    My Problem is that neither of the following two wrapped functions
    (with the exact same arguments) return the right result (full
    chipid.py library attached):
    >
    def FTID_GetDeviceL ocationID(Devic eIndex):
    n = DWORD()
    status = ftchipid.FTID_G etDeviceLocatio nID(DeviceIndex ,
    ctypes.byref(n) )
    >
    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)
    >
    return n.value
    >
    def FTID_GetDeviceC hipID(DeviceInd ex):
    n = DWORD()
    status = ftchipid.FTID_G etDeviceChipID( DeviceIndex,
    ctypes.byref(n) )
    >
    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)
    >
    return n.value
    >
    * On my machine (XP, 32 bits), if I plug two keys in, I can get the
    device chip id from the device with index=1. The one with index=0
    always gives the message "Invalid device handle."
    * I get the wrong location id as well, 0 instead of 0x21...
    * the FTID_GetNumDevi ces function also uses a byref, c_ulong and works.
    * FTDI's win32 console example returns the right results (and uses c
    unsigned longs) - available from

    >
    Any help appreciated!
    >
    Regards,
    Egor
    >

    #!/usr/bin/env python

    """
    A generic chipid library based on ctypes

    This module handles most of the functions in FTChipID.dll

    PROBLEM:
    FTID_GetDeviceL ocationID doesn't work...
    FTID_GetDeviceC hipID doesn't work...
    Well, not exactly true.
    Works with 2 devices plugged in and for device with id 1.
    Does not work for device with id 0... What am I doing wrong?!?!?!?!?! ?!

    How does it work?

    relies on ctypes and ftchipid.dll, was only tested in windows XP
    the chipid dll (1.1.0) from here: http://www.ftdichip.com/Projects/FTDIChip-ID.htm
    for testing, need the the ftd2xx module http://pypi.python.org/pypi/ftd2xx/0.1
    and the ftd2xx.dll, which comes with the driver install


    The latest version of this file is stored in my google code repository:


    Copyright (c) 2008 Egor Zindy <ezindy@gmail.c om>

    Released under the MIT licence.
    """
    # Permission is hereby granted, free of charge, to any person obtaining a
    # copy of this software and associated documentation files (the "Software") ,
    # to deal in the Software without restriction, including without limitation
    # the rights to use, copy, modify, merge, publish, distribute, sublicense,
    # and/or sell copies of the Software, and to permit persons to whom the
    # Software is furnished to do so, subject to the following conditions:
    #
    # The above copyright notice and this permission notice shall be included in
    # all copies or substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    # DEALINGS IN THE SOFTWARE.

    __author__ = "Egor Zindy <ezindy@gmail.c om>"
    __copyright__ = "Copyright (c) 2008 Egor Zindy"
    __license__ = "MIT"
    __version__ = "0.1"
    __date= "2008-07-10"
    __status__ = "Alpha"

    import ctypes
    import ctypes.wintypes
    import sys

    STRING = ctypes.c_char_p

    if sys.platform == 'win32':
    DWORD = ctypes.wintypes .DWORD
    else:
    DWORD = ctypes.c_ulong

    #calling the dll
    ftchipid=ctypes .windll.ftchipi d

    class DeviceError(Exc eption):
    """Exceptio n class for status messages"""
    def __init__(self, value):
    self.value = value

    def __str__(self):
    return repr(self.value )

    def FTID_GetNumDevi ces():
    n = DWORD()
    status = ftchipid.FTID_G etNumDevices(ct ypes.byref(n))

    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)

    return n.value

    def FTID_GetDeviceS erialNumber(Dev iceIndex):
    s = ctypes.create_s tring_buffer(25 6)
    status = ftchipid.FTID_G etDeviceSerialN umber(DeviceInd ex, s, 256)

    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)

    return s.value

    def FTID_GetDeviceL ocationID(Devic eIndex):
    n = DWORD()
    status = ftchipid.FTID_G etDeviceLocatio nID(DeviceIndex , ctypes.byref(n) )

    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)

    return n.value

    def FTID_GetDeviceC hipID(DeviceInd ex):
    n = DWORD()
    status = ftchipid.FTID_G etDeviceChipID( DeviceIndex, ctypes.byref(n) )

    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)

    return n.value

    def FTID_GetDeviceD escription(Devi ceIndex):
    s = ctypes.create_s tring_buffer(25 6)
    status = ftchipid.FTID_G etDeviceDescrip tion(DeviceInde x, s, 256)

    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)

    return s.value

    def FTID_GetDllVers ion():
    s = ctypes.create_s tring_buffer(25 6)
    status = ftchipid.FTID_G etDllVersion(s, 256)

    if status != FTID_SUCCESS:
    raise DeviceError,FTI D_GetErrorCodeS tring("EN",stat us)

    return s.value

    def FTID_GetErrorCo deString(lpLang uage, ErrorCode):
    sl = STRING(lpLangua ge)
    s = ctypes.create_s tring_buffer(25 6)
    status = ftchipid.FTID_G etErrorCodeStri ng(sl,ErrorCode ,s,256)

    return s.value

    #ChipID errors...
    FTID_SUCCESS = 0
    FTID_INVALID_HA NDLE = 1
    FTID_DEVICE_NOT _FOUND = 2
    FTID_DEVICE_NOT _OPENED = 3
    FTID_IO_ERROR = 4
    FTID_INSUFFICIE NT_RESOURCES = 5

    FTID_BUFER_SIZE _TOO_SMALL = 20
    FTID_PASSED_NUL L_POINTER = 21
    FTID_INVALID_LA NGUAGE_CODE = 22
    FTID_INVALID_ST ATUS_CODE = 0xFFFFFFFF

    #testing...
    if __name__ == '__main__':
    import ftd2xx
    device = ftd2xx.open()

    if 1:
    print "\nDll version",
    v = FTID_GetDllVers ion()
    print v

    n = FTID_GetNumDevi ces()
    print "\nNumber of devices:",n

    for i in range(n):
    if 0:
    print "\nChip ID:",
    v = FTID_GetDeviceC hipID(i)
    print i,"0x%08lX" % v

    if 1:
    print "\nLocation ID:",
    v = FTID_GetDeviceL ocationID(i)
    print i,"0x%08lX" % v

    if 1:
    print "\nSerial number:",
    v = FTID_GetDeviceS erialNumber(i)
    print i,v

    if 1:
    print "\nDescription: ",
    v = FTID_GetDeviceD escription(i)
    print i,v

    device.close()


Working...