serialport

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: MIT Imports: 3 Imported by: 0

README

serialport-go

A Go package that allows you to easily access serial ports.

Usage

serialport package provides some common serial port operations: Open, Close, Read and Write, etc.

First you should prepare a serial port configuration Config:

type Config struct {
	BaudRate int
	DataBits int
	StopBits int
	Parity   int
	Timeout  time.Duration
}

If you don't know much about serial port configuration, you can use the default configuration DefaultConfig:

// DefaultConfig returns a default serial port configuration:
//     115200 bps baudrate
//     8 data bits
//     1 stop bit
//     no parity
//     100 ms timeout
func DefaultConfig() Config {
	return Config{
		BaudRate: BR115200,
		DataBits: DB8,
		StopBits: SB1,
		Parity:   PN,
		Timeout:  100 * time.Millisecond,
	}
}

Then, call Open to open a serial port:

func Open(name string, cfg Config) (sp *SerialPort, err error) {
	/* Code ... */
}

When Open is successful, it will return a *SerialPort, which you can use to access the serial port.

Example

A simple serial port echo application:

package main

import (
	"fmt"
	"log"

	"github.com/dsyx/serialport-go"
)

func main() {
	sp, err := serialport.Open("COM3", serialport.DefaultConfig())
	if err != nil {
		log.Fatalln(err)
	}
	defer sp.Close()

	buf := make([]byte, 64)
	for {
		n, _ := sp.Read(buf)
		fmt.Printf("read(%d): %s\n", n, string(buf[:n]))
		sp.Write(buf[:n])
	}
}

Documentation

Overview

Package serialport allows you to easily access serial ports

Index

Constants

View Source
const (
	BR110    = 110    // 110 bps
	BR300    = 300    // 300 bps
	BR600    = 600    // 600 bps
	BR1200   = 1200   // 1200 bps
	BR2400   = 2400   // 2400 bps
	BR4800   = 4800   // 4800 bps
	BR9600   = 9600   // 9600 bps
	BR14400  = 14400  // 14400 bps
	BR19200  = 19200  // 19200 bps
	BR38400  = 38400  // 38400 bps
	BR57600  = 57600  // 57600 bps
	BR115200 = 115200 // 115200 bps
	BR128000 = 128000 // 128000 bps
	BR256000 = 256000 // 256000 bps
)

BaudRate

View Source
const (
	DB5 = 5 // 5 data bits
	DB6 = 6 // 6 data bits
	DB7 = 7 // 7 data bits
	DB8 = 8 // 8 data bits
)

DataBits

View Source
const (
	SB1   = 1  // 1 stop bit
	SB1_5 = 15 // 1.5 stop bits
	SB2   = 2  // 2 stop bits
)

StopBits

View Source
const (
	PN = 0 // No parity
	PO = 1 // Odd parity
	PE = 2 // Even parity
	PM = 3 // Mark parity
	PS = 4 // Space parity
)

Parity

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	BaudRate int
	DataBits int
	StopBits int
	Parity   int
	Timeout  time.Duration
}

Config for serial port configuration:

BaudRate is the baud rate of serial transmission
DataBits is the number of bits per character
StopBits is the number of stop bits
Parity is a method of detecting errors in transmission
Timeout is the serial port Read() timeout

func DefaultConfig added in v0.9.9

func DefaultConfig() Config

DefaultConfig returns a default serial port configuration:

115200 bps baudrate
8 data bits
1 stop bit
no parity
100 ms timeout

type SerialPort added in v0.9.9

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

A SerialPort is a serial port. This must be instantiated by calling Open() and not manually.

func Open

func Open(name string, cfg Config) (sp *SerialPort, err error)

Open opens a serial port.

func (*SerialPort) Close added in v0.9.9

func (sp *SerialPort) Close() error

Close close the serial port.

func (*SerialPort) Config added in v0.9.9

func (sp *SerialPort) Config() (cfg Config, err error)

Config returns the configuration of the serial port.

func (*SerialPort) Flush added in v1.0.0

func (sp *SerialPort) Flush() error

Flush flushes both data received but not read, and data written but not transmitted.

func (*SerialPort) Read added in v0.9.9

func (sp *SerialPort) Read(b []byte) (n int, err error)

Read reads up to len(b) bytes from the serial port. It returns the number of bytes (0 <= n <= len(b)) read from the serial port and any errors encountered. Note:

Timeout < 100 ms: Read blocks until at least one byte is readable;
Timeout > 100 ms: Read blocks until at least one byte is read or timeout.

func (*SerialPort) SetConfig added in v0.9.9

func (sp *SerialPort) SetConfig(cfg Config) error

SetConfig Set the serial port according to Config.

func (*SerialPort) Write added in v0.9.9

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

Write writes len(b) bytes to the serial port. It returns the number of bytes (0 <= n <= len(b)) written to the serial port and any errors encountered.

Jump to

Keyboard shortcuts

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