term

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2021 License: BSD-2-Clause Imports: 5 Imported by: 1

README

term

import "github.com/kckrinke/go-term"

This package is a clone of "github.com/pkg/term" with only a few changes in order to support CDK.

Package term manages POSIX terminals. As POSIX terminals are connected to, or emulate, a UART, this package also provides control over the various UART and serial line parameters.

func CBreakMode

func CBreakMode(t *Term) error

CBreakMode places the terminal into cbreak mode.

func RawMode

func RawMode(t *Term) error

RawMode places the terminal into raw mode.

func Speed

func Speed(baud int) func(*Term) error

Speed sets the baud rate option for the terminal.

type Term

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

Term represents an asynchronous communications port.

func Open
func Open(name string, options ...func(*Term) error) (*Term, error)

Open opens an asynchronous communications port.

func (*Term) Available
func (t *Term) Available() (int, error)

Available returns how many bytes are unused in the buffer.

func (*Term) Buffered
func (t *Term) Buffered() (int, error)

Buffered returns the number of bytes that have been written into the current buffer.

func (*Term) Close
func (t *Term) Close() error

Close closes the device and releases any associated resources.

func (*Term) DTR
func (t *Term) DTR() (bool, error)

DTR returns the state of the DTR (data terminal ready) signal.

func (*Term) Flush
func (t *Term) Flush() error

Flush flushes both data received but not read, and data written but not transmitted.

func (*Term) RTS
func (t *Term) RTS() (bool, error)

RTS returns the state of the RTS (data terminal ready) signal.

func (*Term) Read
func (t *Term) Read(b []byte) (int, error)

Read reads up to len(b) bytes from the terminal. It returns the number of bytes read and an error, if any. EOF is signaled by a zero count with err set to io.EOF.

func (*Term) Restore
func (t *Term) Restore() error

Restore restores the state of the terminal captured at the point that the terminal was originally opened.

func (*Term) SendBreak
func (t *Term) SendBreak() error

SendBreak sends a break signal.

func (*Term) SetCbreak
func (t *Term) SetCbreak() error

SetCbreak sets cbreak mode.

func (*Term) SetDTR
func (t *Term) SetDTR(v bool) error

SetDTR sets the DTR (data terminal ready) signal.

func (*Term) SetOption
func (t *Term) SetOption(options ...func(*Term) error) error

SetOption takes one or more option function and applies them in order to Term.

func (*Term) SetRTS
func (t *Term) SetRTS(v bool) error

SetRTS sets the RTS (data terminal ready) signal.

func (*Term) SetRaw
func (t *Term) SetRaw() error

SetRaw sets raw mode.

func (*Term) SetSpeed
func (t *Term) SetSpeed(baud int) error

SetSpeed sets the receive and transmit baud rates.

func (*Term) Write
func (t *Term) Write(b []byte) (int, error)

Write writes len(b) bytes to the terminal. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).


Generated by godoc2md

Documentation

Overview

Package term manages POSIX terminals. As POSIX terminals are connected to, or emulate, a UART, this package also provides control over the various UART and serial line parameters.

Index

Examples

Constants

View Source
const (
	NONE     = iota // flow control off
	XONXOFF         // software flow control
	HARDWARE        // hardware flow control
)
View Source
const (
	// CBaudMask is the logical of CBAUD and CBAUDEX, except
	// that those values were not exposed via the syscall
	// package.  Many of these values will be redundant, but
	// this long definition ensures we are portable if some
	// architecture defines different values for them (unlikely).
	CBaudMask = unix.B50 |
		unix.B75 |
		unix.B110 |
		unix.B134 |
		unix.B150 |
		unix.B200 |
		unix.B300 |
		unix.B600 |
		unix.B1200 |
		unix.B1800 |
		unix.B2400 |
		unix.B4800 |
		unix.B9600 |
		unix.B19200 |
		unix.B38400 |
		unix.B57600 |
		unix.B115200 |
		unix.B230400 |
		unix.B460800 |
		unix.B500000 |
		unix.B576000 |
		unix.B921600 |
		unix.B1000000 |
		unix.B1152000 |
		unix.B1500000 |
		unix.B2000000 |
		unix.B2500000 |
		unix.B3000000 |
		unix.B3500000 |
		unix.B4000000
)

Variables

This section is empty.

Functions

func CBreakMode

func CBreakMode(t *Term) error

CBreakMode places the terminal into cbreak mode.

func FlowControl

func FlowControl(kind int) func(*Term) error

FlowControl sets the flow control option for the terminal.

func RawMode

func RawMode(t *Term) error

RawMode places the terminal into raw mode.

func ReadTimeout

func ReadTimeout(d time.Duration) func(*Term) error

ReadTimeout sets the read timeout option for the terminal.

func SetAttr

func SetAttr(modifier func(*unix.Termios) uintptr) func(*Term) error

SetAttr returns an option function which will apply the provided modifier to a unix.Termios before using that unix.Termios to set the state of the Term. This allows a developer to manually set attributes for the terminal. Here's an example case to set a terminal into raw mode, but then re-enable the 'opost' attribute:

func EnableOutputPostprocess(a *unix.Termios) uintptr {
    a.Oflag |= unix.OPOST
    return termios.TCSANOW
}

func init() {
    t, _ = term.Open("/dev/tty")
    t.SetRaw()
    t.SetOption(term.SetAttr(EnableOutputPostprocess))
}

func Speed

func Speed(baud int) func(*Term) error

Speed sets the baud rate option for the terminal.

Types

type Term

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

Term represents an asynchronous communications port.

func Open

func Open(name string, options ...func(*Term) error) (*Term, error)

Open opens an asynchronous communications port.

Example

Open a terminal in raw mode at 19200 baud.

Open("/dev/ttyUSB0", Speed(19200), RawMode)
Output:

func (*Term) Available

func (t *Term) Available() (int, error)

Available returns how many bytes are unused in the buffer.

func (*Term) Buffered

func (t *Term) Buffered() (int, error)

Buffered returns the number of bytes that have been written into the current buffer.

func (*Term) CTS

func (t *Term) CTS() (bool, error)

CTS returns the state of the CTS (clear to send) signal.

func (*Term) Close

func (t *Term) Close() error

Close closes the device and releases any associated resources.

func (*Term) DCD

func (t *Term) DCD() (bool, error)

DCD returns the state of the DCD (data carrier detect) signal.

func (*Term) DSR

func (t *Term) DSR() (bool, error)

DSR returns the state of the DSR (data set ready) signal.

func (*Term) DTR

func (t *Term) DTR() (bool, error)

DTR returns the state of the DTR (data terminal ready) signal.

func (*Term) Flush

func (t *Term) Flush() error

Flush flushes both data received but not read, and data written but not transmitted.

func (*Term) GetSpeed

func (t *Term) GetSpeed() (int, error)

GetSpeed gets the current output baud rate.

func (*Term) RI

func (t *Term) RI() (bool, error)

RI returns the state of the RI (ring indicator) signal.

func (*Term) RTS

func (t *Term) RTS() (bool, error)

RTS returns the state of the RTS (request to send) signal.

func (*Term) Read

func (t *Term) Read(b []byte) (int, error)

Read reads up to len(b) bytes from the terminal. It returns the number of bytes read and an error, if any. EOF is signaled by a zero count with err set to io.EOF.

func (*Term) Restore

func (t *Term) Restore() error

Restore restores the state of the terminal captured at the point that the terminal was originally opened.

Example

Restore the terminal state

t, _ := Open("/dev/tty")
// mutate terminal state
t.Restore()
Output:

func (*Term) SendBreak

func (t *Term) SendBreak() error

SendBreak sends a break signal.

Example

Send Break to the remote DTE.

t, _ := Open("/dev/ttyUSB0")
for {
	time.Sleep(3 * time.Second)
	log.Println("Break...")
	t.SendBreak()
}
Output:

func (*Term) SetCbreak

func (t *Term) SetCbreak() error

SetCbreak sets cbreak mode.

func (*Term) SetDTR

func (t *Term) SetDTR(v bool) error

SetDTR sets the DTR (data terminal ready) signal.

Example

Reset an Arduino by toggling the DTR signal.

t, _ := Open("/dev/USB0")
t.SetDTR(false) // toggle DTR low
time.Sleep(250 * time.Millisecond)
t.SetDTR(true) // raise DTR, resets Ardunio
Output:

func (*Term) SetFlowControl

func (t *Term) SetFlowControl(kind int) error

SetFlowControl sets whether hardware flow control is enabled.

func (*Term) SetOption

func (t *Term) SetOption(options ...func(*Term) error) error

SetOption takes one or more option function and applies them in order to Term.

func (*Term) SetRTS

func (t *Term) SetRTS(v bool) error

SetRTS sets the RTS (data terminal ready) signal.

func (*Term) SetRaw

func (t *Term) SetRaw() error

SetRaw sets raw mode.

func (*Term) SetReadTimeout

func (t *Term) SetReadTimeout(d time.Duration) error

SetReadTimeout sets the read timeout. A zero value for d means read operations will not time out.

func (*Term) SetSpeed

func (t *Term) SetSpeed(baud int) error

SetSpeed sets the receive and transmit baud rates.

func (*Term) Winsz

func (t *Term) Winsz() (w, h int, err error)

Return the Window Size

func (*Term) Write

func (t *Term) Write(b []byte) (int, error)

Write writes len(b) bytes to the terminal. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

Directories

Path Synopsis
Package termios implements the low level termios(3) terminal line discipline facilities.
Package termios implements the low level termios(3) terminal line discipline facilities.

Jump to

Keyboard shortcuts

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