gxserial

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: GPL-2.0 Imports: 17 Imported by: 0

README

See An Gurux for an overview.

Join the Gurux Community or follow @Gurux for project updates.

With gurux.serial component you can send data easily syncronously or asyncronously using serial port connection.

Open Source gxserial media component, made by Gurux Ltd, is a part of GXMedias set of media components, which programming interfaces help you implement communication by chosen connection type. Gurux media components also support the following connection types: serial port.

For more info check out Gurux.

We are updating documentation on Gurux web page.

If you have problems you can ask your questions in Gurux Forum.

You can get source codes from http://www.github.com/gurux or add reference to your project:

go get github.com/Gurux/gxcommon-go
go get github.com/Gurux/gxserial-go

Simple example

Before use you must set following settings:

  • PortName
  • BaudRate
  • DataBits
  • Parity
  • StopBits

It is also good to add listener to listen following events.

  • onError
  • onReceived
  • onMediaStateChange
  • onTrace
  • onPropertyChanged
media := gxserial.NewGXSerial("COM1", gxserial.BaudRate9600, 8, gxserial.ParityNone, gxserial.StopBitsOne)
media.open()

Data is send with send command:

media.Send("Hello World!", "")

In default mode received data is coming as asynchronously from OnReceived event. Event listener is added like this:

media.SetOnReceived(func(m gxcommon.IGXMedia, e gxcommon.ReceiveEventArgs) {
	fmt.Printf("Async data: %s\n", e.String())
})

Data can be also send as syncronous if needed.

defer media.GetSynchronous()()
err = media.Send("Hello World!\n", "")
if err != nil {
    fmt.Fprintln(os.Stderr, "error:", err)
    return
}
r := gxcommon.NewReceiveParameters[string]()
r.EOP = "\n"
r.WaitTime = *w
r.Count = 0
ret, err := media.Receive(r)
if err != nil {
    fmt.Fprintln(os.Stderr, "error returned:", err)
    return
}
if ret {
    fmt.Printf("Sync data: %s\n", r.Reply)
}

Documentation

Overview

Package gxserial provides serial port based media for Gurux components. It implements the common IGXMedia-style contract: open/close a connection, send/receive data (optionally framed by an EOP marker), and emit events for received data, errors, tracing and state changes.

Features

  • Configurable serial settings (port, baud rate, data bits, parity, stop bits)
  • Synchronous request/response and asynchronous receive callbacks
  • Framing: optional EOP (End Of Packet) marker (byte, string or []byte).
  • Timeouts: connection and I/O timeouts via time.Duration.
  • Tracing: configurable trace level/mask for sent/received/error/info.
  • Events: Received, Error, Trace and MediaState callbacks.
  • Concurrency: safe for concurrent reads/writes; Close unblocks pending I/O.

Construction

Use NewGXSerial to create a connection with used serial port. Additional options (such as EOP, tracing) can be configured through setters.

Example

media := gxserial.NewGXSerial("COM1", gxserial.BaudRate9600, 8, gxserial.ParityNone, gxserial.StopBitsOne)

media.SetOnReceived(func(m IGXMedia, e ReceiveEventArgs) {
    // handle e.Data(), e.SenderInfo()
})
media.SetOnError(func(m IGXMedia, err error) {
    // log/handle error
})

if err := media.Open(); err != nil {
    // handle connect error
}
defer media.Close()

// send bytes; receive happens via the callback or via a blocking Receive.
_, _ = media.Send([]byte{0x01, 0x02, 0x03})

EOP framing

When an EOP is configured, incoming bytes are buffered until the marker is observed. The marker can be a single byte (e.g. 0x7E), a string (e.g. "OK"), or an arbitrary byte slice. Disable EOP to read raw stream data.

Errors and timeouts

Network and protocol errors are returned from calls or routed to Error handlers. Timeouts follow Go conventions (context/deadline or Duration-based configuration). Error messages are lowercased per Go style guidelines.

Notes

The zero value of GXNet is not ready for use; always construct via NewGXNet. Long-running work in event handlers should be offloaded to a separate goroutine to avoid blocking I/O paths.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPortNames

func GetPortNames() ([]string, error)

GetPortNames retrurns list of available serial ports.

Types

type GXSerial

type GXSerial struct {
	Port string
	// contains filtered or unexported fields
}

GXSerial holds connection configuration and tracing settings for a network media.

func NewGXSerial

func NewGXSerial(port string,
	baudRate gxcommon.BaudRate,
	dataBits int,
	parity gxcommon.Parity,
	stopBits gxcommon.StopBits) *GXSerial

NewGXSerial creates a GXSerial configured with the given serial port.

func (*GXSerial) BaudRate

func (g *GXSerial) BaudRate() gxcommon.BaudRate

BaudRate returns the used baud rate.

func (*GXSerial) Close

func (g *GXSerial) Close() error

Close implements IGXMedia

func (*GXSerial) Copy

func (g *GXSerial) Copy(target gxcommon.IGXMedia) error

Copy implements IGXMedia

func (*GXSerial) DataBits

func (g *GXSerial) DataBits() int

DataBits returns the amount of the data bits.

func (*GXSerial) GetBytesReceived

func (g *GXSerial) GetBytesReceived() uint64

GetBytesReceived implements IGXMedia

func (*GXSerial) GetBytesSent

func (g *GXSerial) GetBytesSent() uint64

GetBytesSent implements IGXMedia

func (*GXSerial) GetBytesToRead

func (g *GXSerial) GetBytesToRead() (int, error)

GetBytesToRead returns the number of bytes currently available to read.

func (*GXSerial) GetBytesToWrite

func (g *GXSerial) GetBytesToWrite() (int, error)

GetBytesToWrite returns the number of bytes currently available to write.

func (*GXSerial) GetEop

func (g *GXSerial) GetEop() any

GetEop implements IGXMedia

func (*GXSerial) GetMediaType

func (g *GXSerial) GetMediaType() string

GetMediaType implements IGXMedia

func (*GXSerial) GetName

func (g *GXSerial) GetName() string

GetName implements IGXMedia

func (*GXSerial) GetSettings

func (g *GXSerial) GetSettings() string

GetSettings implements IGXMedia

func (*GXSerial) GetSynchronous

func (g *GXSerial) GetSynchronous() func()

GetSynchronous implements IGXMedia

func (*GXSerial) GetTrace

func (g *GXSerial) GetTrace() gxcommon.TraceLevel

GetTrace implements IGXMedia

func (*GXSerial) IsOpen

func (g *GXSerial) IsOpen() bool

IsOpen implements IGXMedia

func (*GXSerial) IsSynchronous

func (g *GXSerial) IsSynchronous() bool

IsSynchronous implements IGXMedia

func (*GXSerial) Localize

func (g *GXSerial) Localize(language language.Tag)

Localize messages for the specified language. No errors is returned if language is not supported.

func (*GXSerial) Open

func (g *GXSerial) Open() error

Open implements IGXMedia

func (*GXSerial) Parity

func (g *GXSerial) Parity() gxcommon.Parity

Parity returns used parity.

func (*GXSerial) Receive

func (g *GXSerial) Receive(args *gxcommon.ReceiveParameters) (bool, error)

Receive implements IGXMedia

func (*GXSerial) ResetByteCounters

func (g *GXSerial) ResetByteCounters()

ResetByteCounters implements IGXMedia

func (*GXSerial) ResetSynchronousBuffer

func (g *GXSerial) ResetSynchronousBuffer()

ResetSynchronousBuffer implements IGXMedia

func (*GXSerial) Send

func (g *GXSerial) Send(data any, receiver string) error

Send implements IGXMedia

func (*GXSerial) SetBaudRate

func (g *GXSerial) SetBaudRate(value gxcommon.BaudRate) error

SetBaudRate sets the used baud rate.

func (*GXSerial) SetDataBits

func (g *GXSerial) SetDataBits(value int) error

SetDataBits sets the amount of the data bits.

func (*GXSerial) SetEop

func (g *GXSerial) SetEop(eop any)

SetEop implements IGXMedia

func (*GXSerial) SetOnError

func (g *GXSerial) SetOnError(value gxcommon.ErrorEventHandler)

SetOnError implements IGXMedia

func (*GXSerial) SetOnMediaStateChange

func (g *GXSerial) SetOnMediaStateChange(value gxcommon.MediaStateHandler)

SetOnMediaStateChange implements IGXMedia

func (*GXSerial) SetOnReceived

func (g *GXSerial) SetOnReceived(value gxcommon.ReceivedEventHandler)

SetOnReceived implements IGXMedia

func (*GXSerial) SetOnTrace

func (g *GXSerial) SetOnTrace(value gxcommon.TraceEventHandler)

SetOnTrace implements IGXMedia

func (*GXSerial) SetParity

func (g *GXSerial) SetParity(value gxcommon.Parity) error

SetParity sets the used parity.

func (*GXSerial) SetSettings

func (g *GXSerial) SetSettings(value string) error

SetSettings implements IGXMedia

func (*GXSerial) SetStopBits

func (g *GXSerial) SetStopBits(value gxcommon.StopBits) error

SetStopBits sets the used stop bits.

func (*GXSerial) SetTrace

func (g *GXSerial) SetTrace(traceLevel gxcommon.TraceLevel) error

SetTrace implements IGXMedia

func (*GXSerial) StopBits

func (g *GXSerial) StopBits() gxcommon.StopBits

StopBits returns used stop bits.

func (*GXSerial) String

func (g *GXSerial) String() string

String implements IGXMedia

func (*GXSerial) Validate

func (g *GXSerial) Validate() error

Validate implements IGXMedia

Jump to

Keyboard shortcuts

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