usbserial

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2021 License: MIT Imports: 5 Imported by: 0

README

usbserial

Open serial ports on usb devices as io.ReadWriteClosers.

usbserial is a thin wrapper around gousb/libusb to help with opening usb serial devices. Primarily tested on a Huawei K3520 3g dongle.

Usage


import (
	"fmt"
	"github.com/flipb/usbserial"
)

func main() {
	device := &usbserial.Device{}
	err = device.Open(0x12d1, 0x1001)
	if err == nil {
		fmt.Printf("unable to open device: %v\n", err)
		return
	}

	// open the device's first bulk interface, on the K3520 this is usally the command port
	commandPort, err := device.Interface(0)
	if err != nil {
		fmt.Printf("unable to open command port: %v\n", err)
		return
	}
	// returned port, commandPort is an io.ReadWriteCloser
	defer commandPort.Close()

	// You can open more interfaces. K3520 has data port (voice call data) on index 1, and notify port (network notices (RING's etc.)).

	// Read some bytes
	n, err := commandPort.Read([]byte{})
	// etc...

	// If your device is an AT-speaking device, you can use github.com/flipb/at
	return
}


Dependencies

gousb (github.com/google/gousb) and libusb-1.0

cgo

gousb requires libusb-1.0 and by extension a c compiler. See github.com/google/gousb for instructions.

TODOs

Implement usb_reset to make the operating system rediscover the device.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Device

type Device struct {

	// ClaimInterfaces will claim the serial interfaces on call to Open
	ClaimInterfaces bool
	// ClaimAll will claim the entire usb device
	ClaimAll bool

	Logger Logger
	// contains filtered or unexported fields
}

Device represents a USB serial device

func (*Device) Close

func (d *Device) Close() error

Close the usb device

func (*Device) Interface

func (d *Device) Interface(usbInterfaceNum int) (*Port, error)

Interface opens one of the interaces on the device usbInterfaceNum is the index of the usb interface to open (same as reported by lsusb for instance)

func (*Device) Interfaces

func (d *Device) Interfaces() (map[int]*Port, error)

Interfaces opens all of the detected serial looking interfaces of the device

func (*Device) Open

func (d *Device) Open(Vid, Pid int) error

Open the usb device that mathes VID PID. NOTE that devices might change their PID's and/or interface configurations depending on device's internal state.

func (*Device) Reset

func (d *Device) Reset() error

Reset the USB device

func (*Device) String

func (d *Device) String() string

String returns the human readable identifier of the usb device

type Logger

type Logger interface {
	Printf(format string, part ...interface{})
}

Logger describes interface used for logging

type Port

type Port struct {
	// contains filtered or unexported fields
}

Port represents a serial interface on a usb device

func (*Port) Close

func (i *Port) Close() error

Close the port

func (*Port) Read

func (i *Port) Read(buf []byte) (int, error)

Read bytes from the serial device

func (*Port) String

func (i *Port) String() string

String returns the full description of the port

func (*Port) Write

func (i *Port) Write(buf []byte) (int, error)

Write bytes to the serial device

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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