serial

package module
v0.0.0-...-50434d9 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2017 License: Apache-2.0 Imports: 7 Imported by: 3

README

This is a package that allows you to read from and write to serial ports in Go.

OS support

Currently this package works only on OS X, Linux and Windows. It could probably be ported to other Unix-like platforms simply by updating a few constants; get in touch if you are interested in helping and have hardware to test with. Windows would likely be a lot more work.

Installation

Simply use go install:

go install github.com/jacobsa/go-serial/serial

To update later:

go install -u github.com/jacobsa/go-serial/serial

Use

Set up a serial.OpenOptions struct, then call serial.Open. For example:

    import "fmt"
    import "log"
    import "github.com/jacobsa/go-serial/serial"

    ...

    // Set up options.
    options := serial.OpenOptions{
      PortName: "/dev/tty.usbserial-A8008HlV",
      BaudRate: 19200,
      DataBits: 8,
      StopBits: 1,
      MinimumReadSize: 4,
    }

    // Open the port.
    port, err := serial.Open(options)
    if err != nil {
      log.Fatalf("serial.Open: %v", err)
    }

    // Make sure to close it later.
    defer port.Close()

    // Write 4 bytes to the port.
    b := []byte{0x00, 0x01, 0x02, 0x03}
    n, err := port.Write(b)
    if err != nil {
      log.Fatalf("port.Write: %v", err)
    }

    fmt.Println("Wrote", n, "bytes.")

See the documentation for the OpenOptions struct in serial.go for more information on the supported options.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OpenOptions

type OpenOptions struct {
	// The name of the port, e.g. "/dev/tty.usbserial-A8008HlV".
	PortName string

	// The baud rate for the port.
	BaudRate uint

	// The number of data bits per frame. Legal values are 5, 6, 7, and 8.
	DataBits uint

	// The number of stop bits per frame. Legal values are 1 and 2.
	StopBits uint

	// The type of parity bits to use for the connection. Currently parity errors
	// are simply ignored; that is, bytes are delivered to the user no matter
	// whether they were received with a parity error or not.
	ParityMode ParityMode

	InterCharacterTimeout uint
	MinimumReadSize       uint

	// Enable hardware flow control (CTS/RTS).
	HardwareFlowControl bool

	// Use to enable RS485 mode -- probably only valid on some Linux platforms
	Rs485Enable bool

	// Set to true for logic level high during send
	Rs485RtsHighDuringSend bool

	// Set to true for logic level high after send
	Rs485RtsHighAfterSend bool

	// set to receive data during sending
	Rs485RxDuringTx bool

	// RTS delay before send
	Rs485DelayRtsBeforeSend int

	// RTS delay after send
	Rs485DelayRtsAfterSend int
}

OpenOptions is the struct containing all of the options necessary for opening a serial port.

type ParityMode

type ParityMode int

Valid parity values.

const (
	PARITY_NONE ParityMode = 0
	PARITY_ODD  ParityMode = 1
	PARITY_EVEN ParityMode = 2
)

type Serial

type Serial interface {
	io.ReadWriteCloser

	Flush() error
	SetBaudRate(baudRate uint) error
	SetReadTimeout(timeout time.Duration) error
	SetRTS(active bool) error
	SetDTR(active bool) error
	SetRTSDTR(rtsActive, dtrActive bool) error
	SetBreak(active bool) error
}

func Open

func Open(options OpenOptions) (Serial, error)

Open creates an Serial based on the supplied options struct.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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