Git Product home page Git Product logo

Comments (2)

zakaria-chahboun avatar zakaria-chahboun commented on May 18, 2024 2

So the answer is:

package main

import (
	"fmt"

	"github.com/google/gousb"
)

func main() {
	ctx := gousb.NewContext()
	defer ctx.Close()

	devices, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
		for _, config := range desc.Configs {
			for _, inter := range config.Interfaces {
				for _, setting := range inter.AltSettings {
					// Check if the interface class of USB is `Printer`
					if setting.Class == gousb.ClassPrinter {
						return true
					}
				}
			}
		}
		return false
	})

	if err != nil {
		panic(err)
	}

	// Check if printers exists
	if len(devices) == 0 {
		fmt.Println("No Printer Found!")
		return
	}

	fmt.Println("Printers: ")
	for _, device := range devices {
		name, _ := device.Product()
		vid := device.Desc.Vendor
		pid := device.Desc.Product
		port := device.Desc.Port
		fmt.Println(name, vid, pid, port)
	}
}

Output:

❯ go run .
Printers: 
USB Printing Support 0483 070b 2

Finally!

from gousb.

zagrodzki avatar zagrodzki commented on May 18, 2024 1

This is as expected (presumably). In the Go code you're looking at the class of the device (Class field in https://pkg.go.dev/github.com/google/gousb#DeviceDesc). "per-interface" means that different interfaces of this device have different classes. See the description of class 00h in https://www.usb.org/defined-class-codes#anchor_BaseClass00h

What you're looking at in the lsusb output is the interface class of a particular setting of a particular interface on that USB device, which would be reflected in the Class field of https://pkg.go.dev/github.com/google/gousb#InterfaceSetting (that's a different type). See the intro to devices, configurations and interfaces in https://pkg.go.dev/github.com/google/gousb#hdr-A_Short_Tutorial.

from gousb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.