dsjpqpdm620596 2016-12-13 03:42
浏览 897

与Golang中的USB设备进行交互

I am trying to communicate with a USB device (cottonwood RFID reader) with golang under Linux.

Here is where I am so far:

  1. My computer sees the hardware: I see the device in my /dev/bus/usb

  2. The hardware works fine: the demo software they provide works seemlessly (under windows, they don't have a Linux version)

  3. It seems I can open the endpoints

  4. It seems I can write to the device

Now, when I try to read from thehardware, I always get a timeout. I'm a total beginner on hardware, any help would be greatly appreciated, maybe the issue is actually very basic.

My very simple codebase is here:

package main

import (
    "fmt"
    "log"
    "strconv"

    "github.com/jpoirier/gousb/usb"
)

func main() {
    // Only one context should be needed for an application.  It should always be closed.
    ctx := usb.NewContext()
    defer func() {
        errCl := ctx.Close()
        if errCl != nil {
            log.Fatal(errCl)
        }
    }()

    ctx.Debug(1)

    // ListDevices is used to find the devices to open.
    devs, err := ctx.ListDevices(
        func(desc *usb.Descriptor) bool {
            if desc.Vendor == GetCottonwoodVendor() && desc.Product == GetCottonwoodProduct() {
                return true
            }
            return false
        })

    // All Devices returned from ListDevices must be closed.
    defer func() {
        for _, dev := range devs {
            errCl := dev.Close()
            if errCl != nil {
                log.Fatal(errCl)
            }
        }
    }()

    // ListDevices can occasionally  fail, so be sure to check its return value.
    if err != nil {
        log.Fatalf("list: %s", err)
    }

for _, dev := range devs {
    // Once the device has been selected from ListDevices, it is opened
    // and can be interacted with.
    // Open up two ep for read and write

    epBulkWrite, err := dev.OpenEndpoint(1, 0, 0, 2|uint8(usb.ENDPOINT_DIR_OUT))
    if err != nil {
        log.Fatalf("OpenEndpoint Write error for %v: %v", dev.Address, err)
    }

    // Poll Firmware/Hardware Version ID

    // AntennaOn
    // outAntennaPowerOnCmd := []byte{0x18, 0x03, 0xFF}
    outFirmIdCmd := []byte{0x10, 0x03, 0x00}
    // outHardIdCmd := []byte{0x10, 0x03, 0x01}

    i, err := epBulkWrite.Write(outFirmIdCmd)
    if err != nil {
        log.Fatalf("Cannot write command: %v
", err)
    }
    log.Printf("%v bytes sent", i)

    time.Sleep(1 * time.Second)

    epBulkRead, err := dev.OpenEndpoint(1, 0, 0, 1|uint8(usb.ENDPOINT_DIR_IN))
    if err != nil {
        log.Fatalf("OpenEndpoint Read error for %v: %v", dev.Address, err)
    }

    readBuffer := make([]byte, 64)
    n, errRead := epBulkRead.Read(readBuffer)
    log.Printf("read %d bytes: %v", n, readBuffer)
    if errRead != nil {
        log.Printf("error reading: %v", errRead)
        break
    }
}

// GetCottonwoodVendor returns the vendor ID of cottonwood UHF reader
func GetCottonwoodVendor() usb.ID {
    value, err := strconv.ParseUint("1325", 16, 16)
    if err != nil {
        log.Fatal(err)
    }
    return usb.ID(value)
}

// GetCottonwoodProduct returns the product ID of cottonwood UHF reader
func GetCottonwoodProduct() usb.ID {
    value, err := strconv.ParseUint("c029", 16, 16)
    if err != nil {
        log.Fatal(err)
    }
    return usb.ID(value)
}

First time I launch it, I get:

2016/12/14 19:19:18 3 bytes sent
2016/12/14 19:19:20 read 0 bytes: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
2016/12/14 19:19:20 error reading: libusb: timeout [code -7]

Second time and all after:

2016/12/14 19:21:21 Cannot write command: libusb: timeout [code -7]

I tried with another library using hidraw, but it seems not to work either (probably detach usb device issue).

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?