title: interacting with a USB device in Golang
body: <p>I am trying to communicate with a USB device (<a href="https://rads.stackoverflow.com/amzn/click/com/B0194MNAX8" rel="nofollow noreferrer" rel="nofollow noreferrer">cottonwood RFID reader</a>) with golang under Linux.</p>
  
  <p>Here is where I am so far:</p>
  
  <ol>
  <li><p>My computer sees the hardware: I see the device in my <code>/dev/bus/usb</code></p></li>
  <li><p>The hardware works fine: the demo software they provide works seemlessly (under windows, they don't have a Linux version)</p></li>
  <li><p>It seems I can open the endpoints</p></li>
  <li><p>It seems I can write to the device</p></li>
  </ol>
  
  <p>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.</p>
  
  <p>My very simple codebase is here: </p>
  
  <pre><code>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() &amp;&amp; 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)
  }
  </code></pre>
  
  <p>First time I launch it, I get:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>Second time and all after:</p>
  
  <pre><code>2016/12/14 19:21:21 Cannot write command: libusb: timeout [code -7]
  </code></pre>
  
  <p>I tried with another library using hidraw, but it seems not to work either (probably detach usb device issue).</p>