usb

package module
v0.0.0-...-43bda72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Barcode scanner module

Golang module for working with a barcode scanner

You must first install libusb-1.0. This is pretty straightforward on linux. The cgo package should be able to find it if you install it in the default manner or use your distribution's package manager. For Ubuntu:

$ sudo apt-get install libusb-1.0-0-dev

You may need to grant the current user access to the USB device. This is usually done by adding a file to /etc/udev/rules.d called 50-usb-scale.conf and contents like this:

SUBSYSTEM=="usb", ATTR{idVendor}=="HEX1", ATTR{idProduct}=="HEX2", MODE="0666"

Where HEX1 and HEX2 are replaced by the Vendor and Product ID respectively.

Instead, to match the interface type, you can try replacing ATTR{idVendor}=="HEX1", ATTR{idProduct}=="HEX2" with a match for bInterfaceClass equal to 03 (HID):

SUBSYSTEM=="usb", ATTR{bInterfaceClass}=="03", MODE="0666"

Then apply udev rules and reboot the platform:

$ sudo udevadm control --reload-rules && udevadm trigger

Documentation

Index

Constants

View Source
const (
	ScannerModeUnknown = 0
	/*
		bInterfaceClass 		3 Human Interface Device
		bInterfaceSubClass 		1 Boot Interface Subclass
		bInterfaceProtocol      1 Keyboard
		iInterface             	15 HID Keyboard Emulation
	*/
	ScannerModeHIDKeyboardEmulation = 1
	/*
		bDeviceClass            0
		bInterfaceClass         3 Human Interface Device
		bInterfaceSubClass      0
		bInterfaceProtocol      0
		iInterface             33 HID POS
	*/
	ScannerModeHIDDevice = 2
	/*
	   bDeviceClass          239 Miscellaneous Device
	   bInterfaceClass         2 Communications
	   bInterfaceSubClass      2 Abstract (modem)
	   bInterfaceProtocol      1 AT-commands
	   iInterface             41 CDC-ACM Comm
	*/
	ScannerModeCOMEmulation = 3
)

Variables

This section is empty.

Functions

func NewUsbContext

func NewUsbContext() *gousb.Context

Types

type DeviceDesc

type DeviceDesc struct {
	ManufacturerDesc string
	ProductDesc      string
	Serial           string
	gousb.DeviceDesc
}

DeviceDesc is an extended representation of a USB device descriptor (gousb.DeviceDesc).

func GetUsbDevices

func GetUsbDevices(ctx *gousb.Context) ([]DeviceDesc, error)

GetUsbDevices enum all usb devices

func (*DeviceDesc) GetScanner

func (dd *DeviceDesc) GetScanner(ctx *gousb.Context) (*Scanner, error)

GetScanner returns a device with vendor, product and serial values

type Scanner

type Scanner struct {
	// open device
	*gousb.Device
	// device info
	Info ScannerInfo
}

Scanner is a representation of a barcode scanner.

func GetScanner

func GetScanner(ctx *gousb.Context, Vendor gousb.ID, Product gousb.ID, Serial string) (*Scanner, error)

GetScanner returns a device with vendor, product and serial values

func (*Scanner) Read

func (s *Scanner) Read() ([]byte, error)

type ScannerInfo

type ScannerInfo struct {
	Config        int
	Interface     int
	Setup         int
	Endpoint      int
	Class         gousb.Class
	SubClass      gousb.Class
	Protocol      gousb.Protocol
	MaxPacketSize int
	Mode          ScannerMode
}

ScannerInfo this is extended information of Scanner

type ScannerMode

type ScannerMode uint8

func (ScannerMode) String

func (sm ScannerMode) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL