uartreg

package
v3.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package uartreg defines the UART registry for UART ports discovered on the host.

Example
package main

import (
	"flag"
	"log"

	"periph.io/x/periph/conn/physic"
	"periph.io/x/periph/experimental/conn/uart"
	"periph.io/x/periph/experimental/conn/uart/uartreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// How a command line tool may let the user choose an UART port, yet default
	// to the first bus known.
	name := flag.String("uart", "", "UART port to use")
	flag.Parse()
	p, err := uartreg.Open(*name)
	if err != nil {
		log.Fatal(err)
	}
	defer p.Close()

	c, err := p.Connect(115200*physic.Hertz, uart.One, uart.NoParity, uart.RTSCTS, 8)
	if err != nil {
		log.Fatal(err)
	}
	if err := c.Tx([]byte("cmd"), nil); err != nil {
		log.Fatal(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(name string) (uart.PortCloser, error)

Open opens an UART port by its name, an alias or its number and returns an handle to it.

Specify the empty string "" to get the first available port. This is the recommended default value unless an application knows the exact port to use.

Each port can register multiple aliases, each leading to the same port handle.

Example
package main

import (
	"log"

	"periph.io/x/periph/experimental/conn/uart/uartreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// On linux, the following calls will likely open the same bus.
	_, _ = uartreg.Open("/dev/ttyUSB0")
	_, _ = uartreg.Open("UART0")
	_, _ = uartreg.Open("0")
}
Output:

func Register

func Register(name string, aliases []string, number int, o Opener) error

Register registers an UART port.

Registering the same port name twice is an error, e.g. o.Name(). o.Number() can be -1 to signify that the port doesn't have an inherent "port number". A good example is a port provided over a FT232R device connected on an USB bus. In this case, the port name should be created from the serial number of the device for unique identification.

func Unregister

func Unregister(name string) error

Unregister removes a previously registered UART port.

This can happen when an UART port is exposed via an USB device and the device is unplugged.

Types

type Opener

type Opener func() (uart.PortCloser, error)

Opener opens an handle to a port.

It is provided by the actual port driver.

type Ref

type Ref struct {
	// Name of the port.
	//
	// It must not be a sole number. It must be unique across the host.
	Name string
	// Aliases are the alternative names that can be used to reference this port.
	Aliases []string
	// Number of the port or -1 if the port doesn't have any "native" number.
	//
	// Ports provided by the CPU normally have a 0 based number. Ports provided
	// via an addon (like over USB) generally are not numbered.
	Number int
	// Open is the factory to open an handle to this UART port.
	Open Opener
}

Ref references an UART port.

It is returned by All() to enumerate all registered ports.

func All

func All() []*Ref

All returns a copy of all the registered references to all know UART ports available on this host.

The list is sorted by the port name.

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"periph.io/x/periph/experimental/conn/uart"
	"periph.io/x/periph/experimental/conn/uart/uartreg"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Enumerate all UART ports available and the corresponding pins.
	fmt.Print("UART ports available:\n")
	for _, ref := range uartreg.All() {
		fmt.Printf("- %s\n", ref.Name)
		if ref.Number != -1 {
			fmt.Printf("  %d\n", ref.Number)
		}
		if len(ref.Aliases) != 0 {
			fmt.Printf("  %s\n", strings.Join(ref.Aliases, " "))
		}

		b, err := ref.Open()
		if err != nil {
			fmt.Printf("  Failed to open: %v", err)
		}
		if p, ok := b.(uart.Pins); ok {
			fmt.Printf("  RX : %s", p.RX())
			fmt.Printf("  TX : %s", p.TX())
			fmt.Printf("  RTS: %s", p.RTS())
			fmt.Printf("  CTS: %s", p.CTS())
		}
		if err := b.Close(); err != nil {
			fmt.Printf("  Failed to close: %v", err)
		}
	}
}
Output:

Jump to

Keyboard shortcuts

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