serial

package module
v0.0.0-...-99be097 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2014 License: BSD-3-Clause Imports: 10 Imported by: 7

README

GoSerial

A simple go package to allow you to read and write from the serial port as a stream of bytes.

Details

It aims to have the same API on all platforms, including windows. As an added bonus, the windows package does not use cgo, so you can cross compile for windows from another platform. Unfortunately goinstall does not currently let you cross compile so you will have to do it manually:

GOOS=windows make clean install

Currently there is very little in the way of configurability. You can set the baud rate. Then you can Read(), Write(), or Close() the connection. Read() will block until at least one byte is returned. Write is the same. There is currently no exposed way to set the timeouts, though patches are welcome.

Currently all ports are opened with 8 data bits, 1 stop bit, no parity, no hardware flow control, and no software flow control. This works fine for many real devices and many faux serial devices including usb-to-serial converters and bluetooth serial ports.

You may Read() and Write() simulantiously on the same connection (from different goroutines).

Usage

package main

import (
        "github.com/tarm/goserial"
        "log"
)

func main() {
        c := &serial.Config{Name: "COM45", Baud: 115200}
        s, err := serial.OpenPort(c)
        if err != nil {
                log.Fatal(err)
        }
        
        n, err := s.Write([]byte("test"))
        if err != nil {
                log.Fatal(err)
        }
        
        buf := make([]byte, 128)
        n, err = s.Read(buf)
        if err != nil {
                log.Fatal(err)
        }
        log.Print("%q", buf[:n])
}

Possible Future Work

  • better tests (loopback etc)

Documentation

Index

Constants

View Source
const (
	ParityModeNone = 0
	ParityModeOdd  = 1
	ParityModeEven = 2
)
View Source
const (
	ByteSize5 = 5
	ByteSize6 = 6
	ByteSize7 = 7
	ByteSize8 = 8
)
View Source
const (
	StopBits1 = 1
	StopBits2 = 2
)
View Source
const PREFIX = "/dev/tty"

Variables

This section is empty.

Functions

func IsName

func IsName(name string) bool

IsName returns if name mathes the pattern for serial ports. This does not mean, that there is an actual serial port with this name currently active. Use IsPort to get this information.

func IsPort

func IsPort(port string) bool

IsPort returns if port is one of the long or short names that ListPort returns.

func ListPorts

func ListPorts() []string

func ListPortsShort

func ListPortsShort() []string

func ListPortsShortLong

func ListPortsShortLong() map[string]string

Types

type Baud

type Baud int
const (
	Baud2400   Baud = 2400
	Baud4800   Baud = 4800
	Baud9600   Baud = 9600
	Baud19200  Baud = 19200
	Baud38400  Baud = 38400
	Baud57600  Baud = 57600
	Baud115200 Baud = 115200
	Baud230400 Baud = 230400
)

type ByteSize

type ByteSize int

type Connection

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

func Open

func Open(port string, baud Baud, byteSize ByteSize, parity ParityMode, stopBits StopBits, readTimeout time.Duration) (*Connection, error)

func OpenDefault

func OpenDefault(port string, baud Baud, readTimeout time.Duration) (*Connection, error)

OpenDefault calls Open with ByteSize8, ParityModeNone, StopBits1.

func (*Connection) Close

func (conn *Connection) Close() error

func (*Connection) Drain

func (conn *Connection) Drain() error

func (*Connection) Read

func (conn *Connection) Read(buf []byte) (int, error)

func (*Connection) Write

func (conn *Connection) Write(buf []byte) (int, error)

type ParityMode

type ParityMode int

type StopBits

type StopBits int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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