interacting with a USB device in Golang

5.9k Views Asked by At

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\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)
}

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

There are 0 best solutions below