serial

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

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

Go to latest
Published: Dec 19, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-serial

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.

Installation

Simply use go get:

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

To update later:

go get -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

Overview

- @Author: Wangjun - @Date: 2021-05-21 14:44:32

- @Description: - @FilePath: copy form libs/buffer.go - hnxr

Index

Constants

This section is empty.

Variables

View Source
var ErrFull = errors.New("缓冲区空间不够")
View Source
var ErrNotEnoughData = errors.New("缓冲区没有足够的数据")
View Source
var (
	ErrNotOpen = errors.New("serial not open")
)
View Source
var ErrTimeout = errors.New("timeout")

Functions

This section is empty.

Types

type Buffer

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

高性能的循环缓存,数据0分配

func (*Buffer) AvailWrite

func (m *Buffer) AvailWrite() int

可写的大小

func (*Buffer) Len

func (m *Buffer) Len() int

缓冲区可读取字符长度( 可读的大小)

func (*Buffer) Read

func (m *Buffer) Read(p []byte) (n int, err error)

实现Read 接口

func (*Buffer) Reset

func (m *Buffer) Reset() *Buffer

func (*Buffer) Resize

func (m *Buffer) Resize(size int) *Buffer

func (*Buffer) Write

func (m *Buffer) Write(p []byte) (n int, err error)

实现Write 接口 使用前必须保证有足够的剩余空间

type Option

type Option func(*Serial)

func WithConnect

func WithConnect(conn string) Option

设置连接字符串

func WithReadBufferSize

func WithReadBufferSize(size int) Option

设置读取缓冲区的大小

func WithTimeout

func WithTimeout(ms int64) Option

单位毫秒

type Serial

type Serial struct {

	//Connect     string //linux 下 COM 会自动替换为/dev/ttyO
	ReadBufSize int  //读取数据的缓冲区
	HaveRecv    bool //是否开启数据接收线程

	TimeoutRWC
	// contains filtered or unexported fields
}

Serial 串口

func NewSerial

func NewSerial(opts ...Option) *Serial

func (*Serial) Open

func (m *Serial) Open() (err error)

Open 打开

func (*Serial) OptionString

func (m *Serial) OptionString() string

func (*Serial) Setup

func (m *Serial) Setup(opts ...Option)

type TimeoutRWC

type TimeoutRWC struct {
	io.ReadWriteCloser

	Err error
	// contains filtered or unexported fields
}

func NewTimeoutRWC

func NewTimeoutRWC(conn io.ReadWriteCloser, readSize int, timeoutMs int64) *TimeoutRWC

func (*TimeoutRWC) Close

func (m *TimeoutRWC) Close() (err error)

func (*TimeoutRWC) IsClose

func (m *TimeoutRWC) IsClose() bool

判断连接是否关闭

func (*TimeoutRWC) Peek

func (m *TimeoutRWC) Peek(n int) (data []byte, err error)

增加peek函数

func (*TimeoutRWC) Read

func (m *TimeoutRWC) Read(data []byte) (num int, err error)

带有超时机制的读取函数

func (*TimeoutRWC) ReafFull

func (m *TimeoutRWC) ReafFull(data []byte) (num int, err error)

func (*TimeoutRWC) Reset

func (m *TimeoutRWC) Reset()

重置

func (*TimeoutRWC) SetRWC

func (m *TimeoutRWC) SetRWC(conn io.ReadWriteCloser)

func (*TimeoutRWC) Setup

func (m *TimeoutRWC) Setup(conn io.ReadWriteCloser, readSize int, timeoutMs int64) *TimeoutRWC

func (*TimeoutRWC) Write

func (m *TimeoutRWC) Write(data []byte) (num int, err error)

Directories

Path Synopsis
* @Author: fuzhuang * @Date: 2023-12-11 09:23:21 * @LastEditTime: 2025-05-01 15:27:39 * @LastEditors: wangjun [email protected] * @Description: * @FilePath: \golib\serialer\test\test.go
* @Author: fuzhuang * @Date: 2023-12-11 09:23:21 * @LastEditTime: 2025-05-01 15:27:39 * @LastEditors: wangjun [email protected] * @Description: * @FilePath: \golib\serialer\test\test.go
* @Author: fuzhuang * @Date: 2023-12-11 09:23:21 * @LastEditTime: 2024-11-20 09:12:19 * @LastEditors: wangjun [email protected] * @Description: * @FilePath: \golib\serialer\test\test.go
* @Author: fuzhuang * @Date: 2023-12-11 09:23:21 * @LastEditTime: 2024-11-20 09:12:19 * @LastEditors: wangjun [email protected] * @Description: * @FilePath: \golib\serialer\test\test.go

Jump to

Keyboard shortcuts

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