serial

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

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

Go to latest
Published: Sep 24, 2015 License: BSD-2-Clause Imports: 10 Imported by: 0

README

Build Status Coverage GoDoc

serial

This is an extended package for accessing the serial port. It is inspired by https://github.com/tarm/serial and several forks.

I made this library mainly for educational purposes. As you can see, I commented my code a lot, just to make myself clear what is happening.

Additionally, I will try to collect useful information in the repo-wiki

This package is currently untested.

Overview

  • Library
  • serial2http
  • Terminal

Library

The connection struct is all you need. It encapsulates all parameters necessary and all functions needed to interact with a serial device. Just initiate a connection and open it. After that, you are able to read and write from it or to flush the I/O. When you are finished, just close it.

A connection can be initiated by setting all parameters within the code or by loading all parameters from a file. The library provides several consts to set the baud rate, data and stop bits as well as the parity.

Using the Init function:

connection, err := serial.InitConnection("/dev/ttyUSB0", serial.Baud115200, serial.DataBit8, serial.StopBit1, serial.ParityNone)

Loading from a file:

connection, err := serial.LoadConnection("sample/connection.json")

The next step is to open the connection:

err := connection.Open(0)

This simply sets up the underlying file and binds the desired parameters to it. The 0 is indicating that no timeout is being used. The file is blocking until at least one byte is received.

Writing to the serial port is done by just calling the write function:

n, err := connection.Write([]byte("WriteToPort"))

This returns the count of transmitted bytes as well as the error.

Reading from the serial port requires a delimiter, which indicates the end of the transmission:

response, err := connection.Read(10)

The delimiter is device dependent. Using an ASCII table to find the correct decimal value may help at this point. The response is a []byte, which contains all data transmitted until the delimiter is reached.

Another way for reading from the port is to use a buffer.

response, err := connection.ReadToBuffer(256)

This will read all bytes into a []byte with the given size.

After finishing reading and writing, the connection can be flushed and closed:

err := connection.Flush()

This causes the I/O to discard not transmitted and unread data.

err := connection.Close()

This causes the connection to be closed.

Possible Errors
  • invalid baud rate
  • invalid data bits
  • invalid stop bits
  • invalid parity
  • connection not open
    • should only occur when trying to write or read
    • solution: execute connection.Open() before trying to write or read

serial2http

Terminal

Documentation

Index

Constants

View Source
const (
	// Baud4800 defines a transmission rate of 4800 symbols per second.
	Baud4800 = 4800
	// Baud9600 defines a transmission rate of 9600 symbols per second.
	Baud9600 = 9600
	// Baud19200 defines a transmission rate of 19200 symbols per second.
	Baud19200 = 19200
	// Baud38400 defines a transmission rate of 38400 symbols per second.
	Baud38400 = 38400
	// Baud57600 defines a transmission rate of 57600 symbols per second.
	Baud57600 = 57600
	// Baud115200 defines a transmission rate of 115200 symbols per second.
	Baud115200 = 115200
)
View Source
const (
	// DataBit5 stands for a character length of five bits.
	DataBit5 = DataBit(iota + 5)
	// DataBit6 stands for a character length of six bits.
	DataBit6
	// DataBit7 stands for a character length of seven bits.
	DataBit7
	// DataBit8 stands for a character length of eight bits.
	DataBit8
)
View Source
const (
	// StopBit1 represents a single bit being send as stopbit.
	StopBit1 = StopBit(iota + 1)
	// StopBit2 represents two bits being send as stopbit.
	StopBit2
)
View Source
const (
	// ParityNone indicates that no error detection is being used.
	ParityNone = Parity(iota)
	// ParityEven indicates that a bit is added to even out the bit count.
	ParityEven
	// ParityOdd indicates that a bit is added to provide an odd bit count.
	ParityOdd
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Baud

type Baud uint32

Baud is the unit for the symbol rate. It describes the number of symbols transmitted per second.

type Connection

type Connection struct {
	Port    string
	Baud    Baud
	DataBit DataBit
	StopBit StopBit
	Parity  Parity
	// contains filtered or unexported fields
}

Connection represents a serial connection with all parameters.

func InitConnection

func InitConnection(port string, baudrate Baud, databit DataBit,
	stopbit StopBit, parity Parity) (*Connection, error)

InitConnection provides a connection with the given parameters.

func LoadConnection

func LoadConnection(path string) (*Connection, error)

LoadConnection provides a connection with the parameters being loaded from a json file.

func (*Connection) Close

func (connection *Connection) Close() error

Close a connection.

func (*Connection) Flush

func (connection *Connection) Flush() error

Flush the connection, which causes untransmitted or not read data to be discarded.

func (*Connection) Open

func (connection *Connection) Open(timeout uint8) error

Open a connection with a read timeout.

func (*Connection) Query

func (connection *Connection) Query(request []byte,
	delimiter byte) ([]byte, error)

Query combines Write(), Read() and Flush() to improve usability.

func (*Connection) Read

func (connection *Connection) Read(delimiter byte) ([]byte, error)

Read from an open connection until the delimiter is reached.

func (*Connection) ReadToBuffer

func (connection *Connection) ReadToBuffer(size int) ([]byte, error)

ReadToBuffer reads from an open connection into a []byte buffer with the given size.

func (*Connection) Save

func (connection *Connection) Save(path string) error

Save a connection to a json file.

func (*Connection) String

func (connection *Connection) String() string

func (*Connection) Write

func (connection *Connection) Write(b []byte) (int, error)

Write a byte array to an open connection.

type DataBit

type DataBit byte

DataBit is the number of bits representing a character.

type Parity

type Parity byte

Parity is the method for detecting transmission errors.

type StopBit

type StopBit byte

StopBit is the number of bits being send at the end of every character.

Jump to

Keyboard shortcuts

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