libserial

package module
v0.0.0-...-f187770 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

libserial

Build Status GoDoc GoReportCard codecov

Serial library for golang (no cgo)

Prerequisite

  • Go 1.9+ (for type alias)
  • Git (required by Go)

Supported Platform

  • darwin (tested on amd64, thanks to travis)
    • arm64 amd64 arm 386
  • linux (tested on amd64, thanks to travis)
    • all go supported arch
  • windows (not tested, see issue #1)
    • all go supported arch
  • freebsd (not tested)
    • all go supported arch
  • netbsd (not tested)
    • all go supported arch
  • openbsd (not tested)
    • all go supported arch

Usage

TL;DR: you can find a demo cli app in cmd/libserial/main.go

0.Get this package with go get or git clone

go get -u github.com/goiiot/libserial
# git clone https://github.com/goiiot/libserial

1.Import this package

import (
    // ...
    "github.com/goiiot/libserial"
)

2.Open serial connection and check error

// open serial port with default settings (9600 8N1)
conn, err := libserial.Open("/dev/serial0")

if err != nil {
    panic("hmm, how cloud it fail")
}

Note: You can add options when opening serial port, see godoc - Option

3.Read/Write data from serial connection

buf := make([]byte, 64)

_, err := conn.Read(buf[:])
if err != nil { }

_, err := conn.Write([]byte("{data}"))
if err != nil { }

Command line demo

You can download and install libserial to your $GOPATH/bin for quick demo test (GOPATH required)

go get -u github.com/goiiot/libserial/cmd/libserial

References

LICENSE

GitHub license

Copyright Go-IIoT (https://github.com/goiiot)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

View Source
const (
	ParityMark  = Parity(unix.CMSPAR)
	ParitySpace = 0
)
View Source
const (
	StopBitOne = StopBit(0) // default to stop bit one
	StopBitTwo = StopBit(unix.CSTOPB)

	ParityNone = Parity(0)
	ParityOdd  = Parity(unix.PARODD)
	ParityEven = unix.PARENB // enable parity will default to even mode

)

Variables

View Source
var (
	// ErrDeviceNameEmpty happens when opening a device with empty name
	ErrDeviceNameEmpty = errors.New("device name should not be empty")
)

Functions

This section is empty.

Types

type Option

type Option func(c *SerialPort) error

Option for serial conn options

func WithBaudRate

func WithBaudRate(rate int) Option

WithBaudRate set serial baud rate default is 9600

func WithDataBits

func WithDataBits(d int) Option

WithDataBits set the data bits for SerialPort available values are {5, 6, 7, 8} default is 8

func WithHardwareFlowControl

func WithHardwareFlowControl(enable bool) Option

WithHardwareFlowControl enable hardware flow control

func WithParity

func WithParity(p Parity) Option

WithParity set parity mode available values are {ParityNone, ParityOdd, ParityEven} default is ParityNone

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) Option

WithReadTimeout set timeout timer for read operations if no read timeout set, use blocking read

func WithSoftwareFlowControl

func WithSoftwareFlowControl(enable bool) Option

WithSoftwareFlowControl enable software flow control

func WithStopBits

func WithStopBits(s StopBit) Option

WithStopBits set stop bits for SerialPort port available values are {StopBitOne, StopBitOneHalf, StopBitTwo} default is StopBitOne

type Parity

type Parity uint32

type SerialPort

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

SerialPort of serial

func Open

func Open(device string, options ...Option) (*SerialPort, error)

Open serial port

func (*SerialPort) Close

func (s *SerialPort) Close() error

Close serial connection

func (*SerialPort) File

func (s *SerialPort) File() *os.File

File returns the underlying file handler for this serial port

func (*SerialPort) Flush

func (s *SerialPort) Flush() error

Flush serial input/output queue

func (*SerialPort) Read

func (s *SerialPort) Read(data []byte) (int, error)

Read bytes from serial connection

func (*SerialPort) Write

func (s *SerialPort) Write(data []byte) (int, error)

Write bytes to serial connection

type StopBit

type StopBit uint32

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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