serial

package
v0.1.87 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2024 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package serial provides utilities for searching for and working with serial based devices.

Index

Constants

View Source
const (
	TypeUnknown    = "unknown"
	TypeArduino    = "arduino"
	TypeJetson     = "nvidia-jetson"
	TypeNumatoGPIO = "numato-gpio"
)

The known device types.

Variables

View Source
var Open = func(devicePath string) (io.ReadWriteCloser, error) {
	options := goserial.OpenOptions{
		PortName:        devicePath,
		BaudRate:        9600,
		DataBits:        8,
		StopBits:        1,
		MinimumReadSize: 1,
	}

	device, err := goserial.Open(options)
	if err != nil {
		return nil, err
	}

	return device, nil
}

Open attempts to open a serial device on the given path. It's a variable in case you need to override it during tests.

View Source
var Search = func(filter SearchFilter) []Description {
	serialDeviceDescs := searchUSB(filter)
	if filter.Type != "" && filter.Type != TypeJetson {
		return serialDeviceDescs
	}
	devicesDir, err := os.Open(devPath)
	if err != nil {
		return serialDeviceDescs
	}
	defer utils.UncheckedErrorFunc(devicesDir.Close)
	devices, err := devicesDir.Readdir(0)
	if err != nil {
		return serialDeviceDescs
	}
	for _, dev := range devices {
		if strings.HasPrefix(dev.Name(), "ttyTHS") {
			serialDeviceDescs = append(serialDeviceDescs, Description{
				Type: TypeJetson,
				Path: filepath.Join(devPath, dev.Name()),
			})
		}
	}
	return serialDeviceDescs
}

Search uses linux device APIs to find all applicable serial devices. It's a variable in case you need to override it during tests.

Functions

This section is empty.

Types

type Description

type Description struct {
	Type Type
	Path string
}

Description describes a specific serial device/.

type SearchFilter

type SearchFilter struct {
	Type Type
}

SearchFilter specifies how to find a specific device. Right now it only supports filtering by type.

type Type

type Type string

Type identifies a specific serial device type, like an arduino.

Jump to

Keyboard shortcuts

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