This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] USB2ANY:尝试输出器件的序列号时、输出一个负数

Guru**** 1810460 points
Other Parts Discussed in Thread: USB2ANY
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/amplifiers-group/amplifiers/f/amplifiers-forum/1424856/usb2any-when-trying-to-output-the-serial-number-of-the-device-it-outputs-a-negative-number

器件型号:USB2ANY

工具与软件:

我正在尝试学习如何使用该器件、以便我们可以将其用于未来的项目。 目前、我只尝试了文档中列出的基本函数。 虽然设备似乎已被识别、但序列号的输出为-49或-2、通常为-49。 当我提到文档时,负数应该是错误代码,-49是写入超时。 我在做什么错? 我可以提供的唯一其他信息可能是值得注意的是、它也卡在"闪烁闪烁暂停"模式、前面有 LED。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    使用ctypes.c_buffer()ctypes.create_string_buffer()(而不是ctypes.c_long()参考)获取序列号。

    以下摘录来自内部库(U2A_ERRWrapper用于__getattr__应用包装器函数、该函数检查 API 调用中的负状态代码并提高可读错误):

    class USB2ANY:
        u2adll = U2A_ERRWrapper(ctypes.WinDLL(_DLL_PATH))
        
        @classmethod
        def _FindControllers(cls) -> int:
            '''
            Scan the USB, and return the total number of USB2ANY found.
            '''
            return cls.u2adll.u2aFindControllers()
    
        @classmethod
        def _GetSerialNumber(cls, index: int) -> ctypes.c_buffer:
            '''
            Returns a ctypes.c_buffer containing the ascii bytes corresponding to 
            the serial number of a USB2ANY at the given index, with respect to 
            the ordering determined by _FindControllers function.
            '''
            buf = ctypes.c_buffer(40) # `40` is from SERNUM_LEN in USB2ANY_SDK.h
            status = cls.u2adll.u2aGetSerialNumber(index, buf)
            return buf
    
        @classmethod
        def Controllers(cls) -> List[ctypes.c_buffer]:
            '''
            Returns a list of ctypes.c_buffers containing the serial numbers of 
            all detected USB2ANY.
            '''
            return [cls._GetSerialNumber(i)
                    for i in range(cls._FindControllers())]
    
        def Open(self, controller: Optional[ctypes.c_buffer] = None) -> None:
            '''
            Acquires a handle to the controller specified by the serial number. 
            If controller argument is omitted, the handle will be acquired for 
            the first USB2ANY enumerated.
        
            On failure, the handle is not assigned, and an exception is raised.
            '''
            if controller is None: # use an empty buffer to select first U2A
                controller = ctypes.c_buffer(0)
            self._handle = self.u2adll.u2aOpen(controller)

    I2C 函数太多了、API 手册的讲解不够到位。 有关事务函数的更详尽说明、请访问: https://e2e.ti.com/support/amplifiers-group/amplifiers/f/amplifiers-forum/1389829/usb2any-multibyte-i2c-transactions

    请注意、如果您的应用在 I2C 总线上没有上拉电阻器、您可能需要启用 USB2ANY 的板载上拉电阻器、这需要启用3.3V 电源(u2aPower_Enable(HANDLE, 1, 2, 2))。