在Golang中与USB设备进行交互

时间:2016-12-13 03:42:40

标签: linux go usb hardware

我正在尝试使用Linux下的golang与USB设备(cottonwood RFID reader)进行通信。

这是我到目前为止的地方:

  1. 我的电脑看到了硬件:我在/dev/bus/usb

  2. 中看到了该设备
  3. 硬件工作正常:他们提供的演示软件无法正常工作(在Windows下,他们没有Linux版本)

  4. 我似乎可以打开端点

  5. 好像我可以写信给设备

  6. 现在,当我尝试从硬件中读取内容时,我总是会暂停。我是硬件的初学者,任何帮助都会非常感激,也许这个问题实际上非常基础。

    我非常简单的代码库在这里:

    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\n", 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)
    }
    

    我第一次推出它时,我得到了:

    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]
    

    第二次及以后全部:

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

    我尝试使用hidraw的另一个库,但它似乎也无法工作(可能会分离usb设备问题)。

0 个答案:

没有答案