serial

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2020 License: MIT Imports: 6 Imported by: 1

README

POSIX Serial Port

POSIX serial port for Go.

Copyright (c) 2020 Peter Hagelund

This software is licensed under the MIT License

See LICENSE.txt

Installing

go get -u github.com/peterhagelund/go-serial
Using modules

In go.mod:

require "githib.com/peterhagelund/go-serial" v0.4.2

Using

package main

import (
	"github.com/peterhagelund/go-serial"
)

func main() {
	port, err := serial.NewPort("/dev/tty.usbserial-AC01A7BB", serial.BaudRate9600, serial.ParityNone, serial.DataBits8, serial.StopBits1)
	if err != nil {
		panic(err)
	}
	defer port.Close()
	port.Write([]byte("Hello World via serial\n"))
	data := make([]byte, 10)
	n, err := port.Read(data)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Read %d bytes\n", n)
	if n > 0 {
		fmt.Println(string(data))
	}
}

Documentation

Overview

Package serial provides an implementation of a POSIX serial port along with an implementation of net.Conn.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaudRate

type BaudRate byte

BaudRate is the baud rate type.

const (
	// BaudRate0 is a baud rate of 0 bps
	BaudRate0 BaudRate = iota
	// BaudRate50 is a baud rate of 50 bps
	BaudRate50
	// BaudRate75 is a baud rate of 75 bps
	BaudRate75
	// BaudRate110 is a baud rate of 110 bps
	BaudRate110
	// BaudRate134 is a baud rate of 134 bps
	BaudRate134
	// BaudRate150 is a baud rate of 150 bps
	BaudRate150
	// BaudRate200 is a baud rate of 200 bps
	BaudRate200
	// BaudRate300 is a baud rate of 300 bps
	BaudRate300
	// BaudRate600 is a baud rate of 600 bps
	BaudRate600
	// BaudRate1200 is a baud rate of 1200 bps
	BaudRate1200
	// BaudRate1800 is a baud rate of 1800 bps
	BaudRate1800
	// BaudRate2400 is a baud rate of 2400 bps
	BaudRate2400
	// BaudRate4800 is a baud rate of 4800 bps
	BaudRate4800
	// BaudRate7200 is a baud rate of 7200 bps
	BaudRate7200
	// BaudRate9600 is a baud rate of 9600 bps
	BaudRate9600
	// BaudRate14400 is a baud rate of 14400 bps
	BaudRate14400
	// BaudRate19200 is a baud rate of 19200 bps
	BaudRate19200
	// BaudRate28800 is a baud rate of 28800 bps
	BaudRate28800
	// BaudRate38400 is a baud rate of 38400 bps
	BaudRate38400
	// BaudRate57600 is a baud rate of 57600 bps
	BaudRate57600
	// BaudRate100000 is a baud rate of 100000 bps
	BaudRate100000
	// BaudRate115200 is a baud rate of 115200 bps
	BaudRate115200
	// BaudRate230400 is a baud rate of 230400 bps
	BaudRate230400
)

type DataBits

type DataBits byte

DataBits is the data bits type.

const (
	// DataBits5 signifies communications with 5-bit data words.
	DataBits5 DataBits = iota
	// DataBits6 signifies communications with 6-bit data words.
	DataBits6
	// DataBits7 signifies communications with 7-bit data words.
	DataBits7
	// DataBits8 signifies communications with 8-bit data words.
	DataBits8
)

type Parity

type Parity byte

Parity is the partity type.

const (
	// ParityNone signifies communications without parity checks.
	ParityNone Parity = iota
	// ParityEven signifies communications with even parity.
	ParityEven
	// ParityOdd signifies communications with odd parity.
	ParityOdd
)

type Port

type Port interface {
	// Path returns the path.
	Path() string
	// BaudRate returns the current baud rate.
	BaudRate() BaudRate
	// SetBaudRate changes the baud rate.
	SetBaudRate(baudRate BaudRate) error
	// Parity returns the current parity check setting.
	Parity() Parity
	// SetParity changes the parity check setting.
	SetParity(parity Parity) error
	// DataBits returns the current data bits setting.
	DataBits() DataBits
	// SetDataBits changes the data bits setting.
	SetDataBits(dataBits DataBits) error
	// StopBits returns the current stop bits setting.
	StopBits() StopBits
	// SetStopBits changes the stop bits setting.
	SetStopBits(stopBits StopBits) error
	// SetDeadline changes the read and write deadlines.
	SetDeadline(time.Time) error
	// SetReadDeadline changes the read deadline.
	SetReadDeadline(time.Time) error
	// SetWriteDeadline changes the write deadline.
	SetWriteDeadline(time.Time) error
	io.Reader
	io.Writer
	io.Closer
}

Port defines the interface for a POSIX serial port.

func NewPort

func NewPort(path string, baudRate BaudRate, parity Parity, dataBits DataBits, stopBits StopBits) (Port, error)

NewPort creates and returns a new serial port.

type PortAddr added in v0.2.0

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

PortAddr is a net.Addr implementation for a serial.Port

func (*PortAddr) Network added in v0.2.0

func (addr *PortAddr) Network() string

Network returns the network name ("serial")

func (*PortAddr) String added in v0.2.0

func (addr *PortAddr) String() string

String returns the string representation of the addres (port path)

type PortConn added in v0.2.0

type PortConn interface {
	net.Conn
	Port() Port
}

PortConn is a net.Conn specialization for a serial.Port

func Dial

func Dial(path string, baudRate BaudRate, parity Parity, dataBits DataBits, stopBits StopBits) (PortConn, error)

Dial creates a connection using a serial port.

type StopBits

type StopBits byte

StopBits is the stop bits type.

const (
	// StopBits1 signifies communications with 1 stop bit.
	StopBits1 StopBits = iota
	// StopBits2 signifies communications with 2 stop bits.
	StopBits2
)

Jump to

Keyboard shortcuts

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