mockserialport

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: MIT Imports: 8 Imported by: 1

README

mockserialport

Easily create a virtual serial port command line program for test or development.

Docs

Depends on the socat executable. You must install socat first.

This also works on Android if you have Android's socat.

Make sure you have permissions to create device file. For example, you can specify socat extra opts like this: user=1001,group=1001,mode=666.

Usage

import "github.com/caiguanhao/mockserialport"
import "go.bug.st/serial"

opts := &mockserialport.Options{
	InputFile:  "ttyIN",
	OutputFile: "ttyOUT",
	PidFile:    "socat.pid",
	SocatPath:  "/usr/local/bin/socat",
	BaudRate:   57600,
	ExtraOpts:  "",
	Verbose:    true,
	Open: func(path string, baudrate int) (mockserialport.Port, error) {
		return serial.Open(path, &serial.Mode{
			BaudRate: baudrate,
		})
	},
	Process: func(mock *mockserialport.Mock, input []byte) []byte {
		switch string(input) {
		case "hello":
			mock.Write([]byte("world"))
		case "foo":
			mock.Write([]byte("bar"))
		}
		return nil
	},
}
opts.SetFlags(flag.CommandLine)
flag.Parse()
mock := mockserialport.New(opts)
if err := mock.Start(); err != nil {
	panic(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flag

type Flag interface {
	StringVar(*string, string, string, string)
	IntVar(*int, string, int, string)
}

See flag.FlagSet

type Mock

type Mock struct {
	Options *Options
	Port    Port
	// contains filtered or unexported fields
}

Mock contains options and the serial port

func New

func New(opts *Options) *Mock

Create a new Mock.

func (*Mock) Read

func (m *Mock) Read() error

Read and process data from the virtual serial port.

func (*Mock) Start

func (m *Mock) Start() error

Start() executes StartSocat() and Read(), which starts a socat process and read data from the virtual serial port.

func (*Mock) StartSocat

func (m *Mock) StartSocat() error

Kill previous socat process (if any) and then start a socat process and write process ID to the pid file.

func (*Mock) Terminate

func (m *Mock) Terminate() error

Terminate current socat process and remove pid file.

func (*Mock) Write

func (m *Mock) Write(b []byte) error

Write data to the virtual serial port.

type Options

type Options struct {
	InputFile  string // device file for your program to open for read and write
	OutputFile string // device file for this program to open for read and write
	PidFile    string // file to store the process id of socat, defaults to "socat.pid"
	SocatPath  string // path to the socat executable, defaults to "socat"
	BaudRate   int    // baud rate (1200/2400/4800/9600/19200/38400/57600/115200)
	ExtraOpts  string // extra options of socat
	Verbose    bool   // whether to print log to stderr

	// Function is called to open serial port
	Open func(path string, baudrate int) (Port, error)

	// Function is called when new bytes is read. Return any
	// unprocessed bytes for later use
	Process func(*Mock, []byte) []byte
}

Mock options

func (*Options) SetFlags

func (opts *Options) SetFlags(flag Flag)

Helper method to set up flags for flag set.

func (*Options) SetFlagsPrefix added in v1.0.1

func (opts *Options) SetFlagsPrefix(flag Flag, prefix string)

Helper method to set up flags (with prefix) for flag set.

func (*Options) SocatCommandArgs

func (opts *Options) SocatCommandArgs() []string

Return command line arguments of socat.

type Port

type Port interface {
	Read(p []byte) (n int, err error)
	Write(p []byte) (n int, err error)
}

Serial port interface

Jump to

Keyboard shortcuts

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