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 ¶
- func GetPortNames() ([]string, error)
- type GXSerial
- func (g *GXSerial) BaudRate() gxcommon.BaudRate
- func (g *GXSerial) Close() error
- func (g *GXSerial) Copy(target gxcommon.IGXMedia) error
- func (g *GXSerial) DataBits() int
- func (g *GXSerial) GetBytesReceived() uint64
- func (g *GXSerial) GetBytesSent() uint64
- func (g *GXSerial) GetBytesToRead() (int, error)
- func (g *GXSerial) GetBytesToWrite() (int, error)
- func (g *GXSerial) GetEop() any
- func (g *GXSerial) GetMediaType() string
- func (g *GXSerial) GetName() string
- func (g *GXSerial) GetSettings() string
- func (g *GXSerial) GetSynchronous() func()
- func (g *GXSerial) GetTrace() gxcommon.TraceLevel
- func (g *GXSerial) IsOpen() bool
- func (g *GXSerial) IsSynchronous() bool
- func (g *GXSerial) Localize(language language.Tag)
- func (g *GXSerial) Open() error
- func (g *GXSerial) Parity() gxcommon.Parity
- func (g *GXSerial) Receive(args *gxcommon.ReceiveParameters) (bool, error)
- func (g *GXSerial) ResetByteCounters()
- func (g *GXSerial) ResetSynchronousBuffer()
- func (g *GXSerial) Send(data any, receiver string) error
- func (g *GXSerial) SetBaudRate(value gxcommon.BaudRate) error
- func (g *GXSerial) SetDataBits(value int) error
- func (g *GXSerial) SetEop(eop any)
- func (g *GXSerial) SetOnError(value gxcommon.ErrorEventHandler)
- func (g *GXSerial) SetOnMediaStateChange(value gxcommon.MediaStateHandler)
- func (g *GXSerial) SetOnReceived(value gxcommon.ReceivedEventHandler)
- func (g *GXSerial) SetOnTrace(value gxcommon.TraceEventHandler)
- func (g *GXSerial) SetParity(value gxcommon.Parity) error
- func (g *GXSerial) SetSettings(value string) error
- func (g *GXSerial) SetStopBits(value gxcommon.StopBits) error
- func (g *GXSerial) SetTrace(traceLevel gxcommon.TraceLevel) error
- func (g *GXSerial) StopBits() gxcommon.StopBits
- func (g *GXSerial) String() string
- func (g *GXSerial) Validate() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetPortNames ¶
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) GetBytesReceived ¶
GetBytesReceived implements IGXMedia
func (*GXSerial) GetBytesSent ¶
GetBytesSent implements IGXMedia
func (*GXSerial) GetBytesToRead ¶
GetBytesToRead returns the number of bytes currently available to read.
func (*GXSerial) GetBytesToWrite ¶
GetBytesToWrite returns the number of bytes currently available to write.
func (*GXSerial) GetMediaType ¶
GetMediaType implements IGXMedia
func (*GXSerial) GetSettings ¶
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) IsSynchronous ¶
IsSynchronous implements IGXMedia
func (*GXSerial) Localize ¶
Localize messages for the specified language. No errors is returned if language is not supported.
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) SetBaudRate ¶
SetBaudRate sets the used baud rate.
func (*GXSerial) SetDataBits ¶
SetDataBits sets the amount of the data bits.
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) SetSettings ¶
SetSettings implements IGXMedia
func (*GXSerial) SetStopBits ¶
SetStopBits sets the used stop bits.
func (*GXSerial) SetTrace ¶
func (g *GXSerial) SetTrace(traceLevel gxcommon.TraceLevel) error
SetTrace implements IGXMedia