serial

package module
v0.0.0-...-2b7eac9 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2020 License: BSD-3-Clause Imports: 12 Imported by: 0

README

Serial

Golang package for serial port

GoDoc

A Go package that allow you to read and write from the serial port.

This is a forked repo written by @tarm.

Usage

package main
 
import (
	"time"
	"github.com/argandas/serial"
)

func main() {
    sp := serial.New()
    err := sp.Open("COM1", 9600)
    if err != nil {
        panic(err)
    }
    defer sp.Close()
    sp.Println("AT")
    sp.WaitForRegexTimeout("OK.*", time.Second * 10)
}

NonBlocking Mode

By default the returned serial port reads in blocking mode. Which means Read() will block until at least one byte is returned. If that's not what you want, specify a positive ReadTimeout and the Read() will timeout returning 0 bytes if no bytes are read. Please note that this is the total timeout the read operation will wait and not the interval timeout between two bytes.

	sp := serial.New()
    err := sp.Open("COM1", 9600, time.Second * 5)

Documentation

Index

Constants

View Source
const EOL_DEFAULT byte = '\n'

End of line character (AKA EOL), newline character (ASCII 10, CR, '\n'). is used by default.

Variables

This section is empty.

Functions

This section is empty.

Types

type Port

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

func (*Port) Close

func (p *Port) Close() (err error)

func (*Port) Flush

func (p *Port) Flush() error

Discards data written to the port but not transmitted, or data received but not read

func (*Port) Read

func (p *Port) Read(b []byte) (n int, err error)

func (*Port) Write

func (p *Port) Write(b []byte) (n int, err error)

type SerialPort

type SerialPort struct {
	Verbose bool
	// contains filtered or unexported fields
}

func New

func New() *SerialPort

func (*SerialPort) Available

func (sp *SerialPort) Available() int

Available return the total number of available unread bytes on the serial buffer.

func (*SerialPort) Close

func (sp *SerialPort) Close() error

This method close the current Serial Port.

func (*SerialPort) EOL

func (sp *SerialPort) EOL(c byte)

Change end of line character (AKA EOL), newline character (ASCII 10, LF, '\n') is used by default.

func (*SerialPort) Open

func (sp *SerialPort) Open(name string, baud int, timeout ...time.Duration) error

func (*SerialPort) Print

func (sp *SerialPort) Print(str string) error

This method prints data trough the serial port.

func (*SerialPort) Printf

func (sp *SerialPort) Printf(format string, args ...interface{}) error

Printf formats according to a format specifier and print data trough the serial port.

func (*SerialPort) Println

func (sp *SerialPort) Println(str string) error

Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, CR, '\r') and a newline character (ASCII 10, LF, '\n').

func (*SerialPort) Read

func (sp *SerialPort) Read() (byte, error)

Read the first byte of the serial buffer.

func (*SerialPort) ReadLine

func (sp *SerialPort) ReadLine() (string, error)

Read first available line from serial port buffer.

Line is delimited by the EOL character, newline character (ASCII 10, LF, '\n') is used by default.

The text returned from ReadLine does not include the line end ("\r\n" or '\n').

func (*SerialPort) SendFile

func (sp *SerialPort) SendFile(filepath string) error

This method send a binary file trough the serial port. If EnableLog is active then this method will log file related data.

func (*SerialPort) WaitForRegexTimeout

func (sp *SerialPort) WaitForRegexTimeout(exp string, timeout time.Duration) (string, error)

Wait for a defined regular expression for a defined amount of time.

func (*SerialPort) Write

func (sp *SerialPort) Write(data []byte) (n int, err error)

This method prints data trough the serial port.

Jump to

Keyboard shortcuts

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