Documentation
¶
Overview ¶
Package ethercat implements EtherCAT master driver for EdgeX gateway. Provides PDO periodic snapshot read/write, CoE SDO parameter access, bus topology scanning, and slave enumeration via anviod/EtherCAT library.
Index ¶
- type EtherCATDecoder
- type EtherCATDriver
- func (d *EtherCATDriver) Connect(ctx context.Context) error
- func (d *EtherCATDriver) Disconnect() error
- func (d *EtherCATDriver) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, ...)
- func (d *EtherCATDriver) Health() driver.HealthStatus
- func (d *EtherCATDriver) Init(cfg model.DriverConfig) error
- func (d *EtherCATDriver) ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)
- func (d *EtherCATDriver) ResetDeviceCollection(deviceID string)
- func (d *EtherCATDriver) Scan(ctx context.Context, params map[string]any) (any, error)
- func (d *EtherCATDriver) SetDeviceConfig(config map[string]any) error
- func (d *EtherCATDriver) SetSlaveID(slaveID uint8) error
- func (d *EtherCATDriver) WritePoint(ctx context.Context, point model.Point, value any) error
- type EtherCATScheduler
- type EtherCATTransport
- func (t *EtherCATTransport) Connect(ctx context.Context) error
- func (t *EtherCATTransport) Disconnect()
- func (t *EtherCATTransport) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, ...)
- func (t *EtherCATTransport) GetSchedulerMetrics() (totalRequests, successCount, failureCount int64)
- func (t *EtherCATTransport) IsConnected() bool
- func (t *EtherCATTransport) RegisterSlaveSnapshot(position, txSize, rxSize int)
- func (t *EtherCATTransport) RemoveSlaveSnapshot(position int)
- func (t *EtherCATTransport) ResetDeviceCollection()
- func (t *EtherCATTransport) SetDeviceConfig(devCfg deviceConfig)
- type ParsedAddress
- type ScanResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EtherCATDecoder ¶
type EtherCATDecoder struct{}
EtherCATDecoder handles type-aware encoding/decoding of PDO/SDO byte slices. Supports all standard industrial data types with configurable byte order.
func NewEtherCATDecoder ¶
func NewEtherCATDecoder() *EtherCATDecoder
NewEtherCATDecoder creates a new decoder instance.
func (*EtherCATDecoder) ByteSize ¶
func (d *EtherCATDecoder) ByteSize(dataType string) int
ByteSize returns the expected byte size for a given data type string. Returns 0 for unknown types.
func (*EtherCATDecoder) DecodeValue ¶
func (d *EtherCATDecoder) DecodeValue(data []byte, dataType string, addr *ParsedAddress) (any, error)
DecodeValue decodes raw bytes into a typed Go value based on dataType and endianness. For bit addresses, the appropriate bit is extracted from the byte at offset.
func (*EtherCATDecoder) EncodeValue ¶
func (d *EtherCATDecoder) EncodeValue(value any, dataType string, addr *ParsedAddress) ([]byte, error)
EncodeValue encodes a Go value into a byte slice based on dataType and endianness.
type EtherCATDriver ¶
type EtherCATDriver struct {
// contains filtered or unexported fields
}
func NewEtherCATDriver ¶
func NewEtherCATDriver() *EtherCATDriver
NewEtherCATDriver creates a new EtherCAT driver instance.
func (*EtherCATDriver) Connect ¶
func (d *EtherCATDriver) Connect(ctx context.Context) error
Connect initializes the EtherCAT master and starts the PDO cycle thread. Delegates to transport.Connect which uses ConnectionManager for retry/backoff.
func (*EtherCATDriver) Disconnect ¶
func (d *EtherCATDriver) Disconnect() error
Disconnect stops the PDO cycle thread and closes the master. Idempotent — safe to call multiple times.
func (*EtherCATDriver) GetConnectionMetrics ¶
func (d *EtherCATDriver) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string, lastDisconnectTime time.Time)
GetConnectionMetrics returns connection statistics for diagnostics.
func (*EtherCATDriver) Health ¶
func (d *EtherCATDriver) Health() driver.HealthStatus
Health returns the current health status of the EtherCAT master. Returns HealthStatusGood when the master is connected (OP state). Returns HealthStatusBad when disconnected or in error state.
func (*EtherCATDriver) Init ¶
func (d *EtherCATDriver) Init(cfg model.DriverConfig) error
Init parses channel configuration and creates transport/scheduler components. Does NOT establish network connection (deferred to Connect).
func (*EtherCATDriver) ReadPoints ¶
func (d *EtherCATDriver) ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)
ReadPoints reads values for the given points from PDO snapshots or SDO mailbox. PDO reads are zero-wait (atomic snapshot memory reads). SDO reads are synchronous with independent timeout. Each point is processed independently — failures are per-point, not batch-aborting.
func (*EtherCATDriver) ResetDeviceCollection ¶
func (d *EtherCATDriver) ResetDeviceCollection(deviceID string)
ResetDeviceCollection clears the device's PDO snapshot cache. Called by ScanEngine when points are added or removed for a device. Implements driver.DeviceCollectionResetter interface.
func (*EtherCATDriver) Scan ¶
Scan performs a bus scan and returns discovered slaves. Implements driver.Scanner interface.
func (*EtherCATDriver) SetDeviceConfig ¶
func (d *EtherCATDriver) SetDeviceConfig(config map[string]any) error
SetDeviceConfig parses and stores per-device (slave) configuration. Called by ChannelManager when switching device context.
func (*EtherCATDriver) SetSlaveID ¶
func (d *EtherCATDriver) SetSlaveID(slaveID uint8) error
SetSlaveID is a no-op for EtherCAT. EtherCAT uses positional addressing, not slave ID.
func (*EtherCATDriver) WritePoint ¶
WritePoint writes a value to a single point. PDO writes go to the RxPDO buffer for next-cycle delivery. SDO writes are synchronous via CoE mailbox.
type EtherCATScheduler ¶
type EtherCATScheduler struct {
// contains filtered or unexported fields
}
func NewEtherCATScheduler ¶
func NewEtherCATScheduler(transport *EtherCATTransport, decoder *EtherCATDecoder) *EtherCATScheduler
NewEtherCATScheduler creates a new scheduler instance.
func (*EtherCATScheduler) GetTransport ¶
func (s *EtherCATScheduler) GetTransport() *EtherCATTransport
GetTransport returns the underlying transport for metrics access.
func (*EtherCATScheduler) ReadPoints ¶
func (s *EtherCATScheduler) ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)
ReadPoints reads values for the given points. PDO points are read from the transport's atomic snapshot (zero-wait). SDO points are read via CoE mailbox with independent timeout. Each point is processed independently — a failure on one point does not abort the entire batch; failed points are returned with Quality="Bad".
func (*EtherCATScheduler) WritePoint ¶
WritePoint writes a value to a single point. PDO points are encoded and written to the RxPDO buffer for next-cycle delivery. SDO points are written synchronously via CoE mailbox.
type EtherCATTransport ¶
type EtherCATTransport struct {
// contains filtered or unexported fields
}
func NewEtherCATTransport ¶
func NewEtherCATTransport(chCfg channelConfig) *EtherCATTransport
NewEtherCATTransport creates a new transport instance.
func (*EtherCATTransport) Connect ¶
func (t *EtherCATTransport) Connect(ctx context.Context) error
Connect initializes the master and starts the PDO cycle thread. Must be called via ConnectionManager.EnsureConnected for single-owner guarantees.
func (*EtherCATTransport) Disconnect ¶
func (t *EtherCATTransport) Disconnect()
Disconnect stops the PDO cycle thread and closes the master. Idempotent — safe to call multiple times.
func (*EtherCATTransport) GetConnectionMetrics ¶
func (t *EtherCATTransport) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string)
GetConnectionMetrics returns connection statistics.
func (*EtherCATTransport) GetSchedulerMetrics ¶
func (t *EtherCATTransport) GetSchedulerMetrics() (totalRequests, successCount, failureCount int64)
GetSchedulerMetrics returns scheduler statistics.
func (*EtherCATTransport) IsConnected ¶
func (t *EtherCATTransport) IsConnected() bool
IsConnected returns whether the master is in OP state.
func (*EtherCATTransport) RegisterSlaveSnapshot ¶
func (t *EtherCATTransport) RegisterSlaveSnapshot(position, txSize, rxSize int)
RegisterSlaveSnapshot creates PDO snapshot entries for a slave at the given position.
func (*EtherCATTransport) RemoveSlaveSnapshot ¶
func (t *EtherCATTransport) RemoveSlaveSnapshot(position int)
RemoveSlaveSnapshot removes PDO snapshot entries for a slave.
func (*EtherCATTransport) ResetDeviceCollection ¶
func (t *EtherCATTransport) ResetDeviceCollection()
ResetDeviceCollection clears all PDO snapshots (called when points are added/removed).
func (*EtherCATTransport) SetDeviceConfig ¶
func (t *EtherCATTransport) SetDeviceConfig(devCfg deviceConfig)
SetDeviceConfig updates the current device configuration.
type ParsedAddress ¶
type ParsedAddress struct {
Position int // slave position on bus (1..N)
IsSDO bool // true if SDO (CoE mailbox) access
PDOType string // "Tx" (input/read) or "Rx" (output/write), only for PDO
Offset int // byte offset in PDO image, only for PDO
Bit int // bit offset 0-7, -1 if not a bit address
Index uint16 // object dictionary index, only for SDO
SubIndex uint16 // object dictionary sub-index, only for SDO
Endian string // "BE" (default) or "LE"
}
ParsedAddress holds the decoded components of an EtherCAT point address.
PDO address format: POSITION:PDO:OFFSET[.BIT][#ENDIAN] SDO address format: POSITION:SDO:0xINDEX:0xSUBINDEX[#ENDIAN]
func ParseAddress ¶
func ParseAddress(s string) (*ParsedAddress, error)
ParseAddress parses an EtherCAT point address string.
Valid formats:
- PDO: "1:Tx:0" → slave 1 TxPDO offset 0
- PDO: "1:Tx:2.3" → slave 1 TxPDO offset 2 bit 3
- PDO: "2:Rx:4#LE" → slave 2 RxPDO offset 4, little-endian
- SDO: "1:SDO:0x6041:0" → slave 1 SDO 0x6041 sub 0
- SDO: "1:SDO:0x6064:0#BE" → slave 1 SDO 0x6064 sub 0, big-endian
func (*ParsedAddress) String ¶
func (a *ParsedAddress) String() string
String returns the canonical address string representation.
type ScanResult ¶
type ScanResult struct {
Position int `json:"position"`
VendorID string `json:"vendor_id"`
ProductCode string `json:"product_code"`
Revision string `json:"revision"`
TxPDOSize int `json:"tx_pdo_size"`
RxPDOSize int `json:"rx_pdo_size"`
}
ScanResult holds the result of a bus scan operation.