You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyUSB读取数显卡尺USB数据超时问题求助

Fixing USB Read Timeout for MAHR 16EXu Caliper

Hey there, let's troubleshoot that timeout error you're hitting with your MAHR 16EXu caliper. The [Errno 10060] Operation timed out message usually means your device isn't getting the setup it needs to start sending data, or there's a missing step in how you're interacting with the USB endpoints. Here's what you're likely missing:

1. Claim the USB Interface First

PyUSB doesn't automatically take control of the device's interface, which is required to access its endpoints. Without this step, your read attempts will fail because the OS might be holding the interface open for another driver.

Add this line right after finding the device:

# Claim the first (and only) interface on the device
usb.util.claim_interface(dev, 0)

Don't forget to release it when you're done to avoid leaving the device in a locked state:

usb.util.release_interface(dev, 0)

2. Most Measurement Devices Require a Trigger Command

Your caliper won't just send data spontaneously over USB—you need to tell it to send readings. It has a Bulk OUT endpoint (0x2) specifically for sending commands to the device. You'll need to look up the MAHR 16EXu's communication protocol (check the user manual or technical docs) to find the exact byte sequence to trigger a data output.

For example, many calipers use a simple query command like b'\x01' or b'\x00\x01'. Here's how to send it:

# Send a trigger command (replace with the actual bytes from MAHR's protocol)
dev.write(0x2, b'\x01')

3. Match the Read Packet Size to the Endpoint's Max

Your endpoint's wMaxPacketSize is 64 bytes (0x40), but you're trying to read 68 bytes (0x44). This mismatch can cause issues—always use a read size that's equal to or smaller than the endpoint's max packet size.

Update your read line to:

ret = dev.read(0x81, 0x40, timeout=dev.default_timeout)

4. Bonus: Use PySerial Instead of Raw PyUSB

Your device uses an FTDI chip (VID 0x0403, PID 0x6001), which creates a virtual serial port (COM port on Windows, /dev/ttyUSBx on Linux/macOS). This is way easier to work with than low-level USB endpoints.

Install pyserial first, then try this simpler approach:

import serial

# Replace with your caliper's serial port (check Device Manager on Windows, dmesg on Linux)
ser = serial.Serial('COM3', baudrate=9600, timeout=1)

try:
    while True:
        # Send trigger command (adjust based on MAHR's protocol)
        ser.write(b'\x01')
        # Read the response
        data = ser.readline()
        if data:
            print(data.decode('ascii'))
finally:
    ser.close()

Modified Full PyUSB Example

Putting it all together, here's a corrected version of your code:

import usb.core
import usb.util

dev = usb.core.find(idVendor=0x0403, idProduct=0x6001)

if dev is None:
    raise ValueError("MAHR 16EXu not found—check USB connection or drivers")

try:
    # Claim the interface
    usb.util.claim_interface(dev, 0)
    
    # Send trigger command (replace with actual protocol bytes)
    dev.write(0x2, b'\x01')
    
    # Read with correct packet size
    while True:
        ret = dev.read(0x81, 0x40, timeout=5000)
        print(ret)
except usb.core.USBError as e:
    print(f"Error: {e}")
finally:
    # Clean up
    usb.util.release_interface(dev, 0)

The timeout was almost certainly caused by not claiming the interface and not sending a trigger command to prompt the caliper to send data. Using PySerial will save you a lot of low-level USB hassle, since FTDI chips are designed to work with serial libraries.

内容的提问来源于stack exchange,提问作者Sator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:04:11