modbus

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

Variables

This section is empty.

Functions

func NewModbusDriver

func NewModbusDriver() driver.Driver

Types

type AddressInfo

type AddressInfo struct {
	Point         model.Point
	RegType       model.RegisterType
	Offset        uint16
	RegisterCount uint16 // 该点位占用的寄存器数
}

AddressInfo 用于存储点位的地址信息

type Decoder

type Decoder interface {
	Decode(point model.Point, raw []byte) (any, string, error)
	Encode(point model.Point, value any) ([]uint16, error)
	ParseAddress(addr string) (model.RegisterType, uint16, error)
	GetRegisterCount(dataType string) uint16
}

Decoder 接口定义

type DeviceState

type DeviceState string
const (
	StateOnline     DeviceState = "ONLINE"
	StateDegraded   DeviceState = "DEGRADED"
	StateOffline    DeviceState = "OFFLINE"
	StateRecovering DeviceState = "RECOVERING"
	StateProbing    DeviceState = "PROBING"
)

type DeviceStateMachine

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

func NewDeviceStateMachine

func NewDeviceStateMachine() *DeviceStateMachine

func (*DeviceStateMachine) GetState

func (sm *DeviceStateMachine) GetState() DeviceState

func (*DeviceStateMachine) OnFailure

func (sm *DeviceStateMachine) OnFailure()

func (*DeviceStateMachine) OnSuccess

func (sm *DeviceStateMachine) OnSuccess()

func (*DeviceStateMachine) SetProbing

func (sm *DeviceStateMachine) SetProbing()

func (*DeviceStateMachine) SetRunning

func (sm *DeviceStateMachine) SetRunning()

type MetricsRecorder

type MetricsRecorder interface {
	RecordRequest(channelID string, success bool, duration time.Duration, errorType string)
	RecordReconnect(channelID string)
	RecordConnectionStart(channelID string)
	RecordError(channelID string, errType, code, message string)
	RecordPointDebug(channelID, pointID string, raw []byte, parsed any, quality string)
	RecordCycle(channelID string, success bool)
}

MetricsRecorder 指标记录器接口

type ModbusDriver

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

func (*ModbusDriver) Connect

func (d *ModbusDriver) Connect(ctx context.Context) error

func (*ModbusDriver) Disconnect

func (d *ModbusDriver) Disconnect() error

func (*ModbusDriver) GetConnectionController added in v0.0.7

func (d *ModbusDriver) GetConnectionController() *core.ConnectionController

func (*ModbusDriver) GetConnectionMetrics

func (d *ModbusDriver) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string, lastDisconnectTime time.Time)

func (*ModbusDriver) GetMetrics

func (d *ModbusDriver) GetMetrics() model.ChannelMetrics

func (*ModbusDriver) Health

func (d *ModbusDriver) Health() driver.HealthStatus

func (*ModbusDriver) Init

func (d *ModbusDriver) Init(config model.DriverConfig) error

func (*ModbusDriver) ReadPoints

func (d *ModbusDriver) ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)

func (*ModbusDriver) SetDeviceConfig

func (d *ModbusDriver) SetDeviceConfig(config map[string]any) error

func (*ModbusDriver) SetSlaveID

func (d *ModbusDriver) SetSlaveID(slaveID uint8) error

func (*ModbusDriver) WritePoint

func (d *ModbusDriver) WritePoint(ctx context.Context, point model.Point, value any) error

type ModbusTransport

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

ModbusTransport 实现 Transport 接口

func NewModbusTransport

func NewModbusTransport(cfg model.DriverConfig) *ModbusTransport

func (*ModbusTransport) Connect

func (t *ModbusTransport) Connect(ctx context.Context) error

func (*ModbusTransport) DetectMTU

func (t *ModbusTransport) DetectMTU(ctx context.Context) (uint16, error)

DetectMTU performs a simple binary-search-like probe to determine a safely readable register count

func (*ModbusTransport) Disconnect

func (t *ModbusTransport) Disconnect() error

func (*ModbusTransport) GetConnectionMetrics

func (t *ModbusTransport) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string, lastDisconnectTime time.Time)

GetConnectionMetrics 获取连接指标

func (*ModbusTransport) IsConnected

func (t *ModbusTransport) IsConnected() bool

func (*ModbusTransport) IsReconnectExhausted added in v0.0.7

func (t *ModbusTransport) IsReconnectExhausted() bool

func (*ModbusTransport) NeedProbeCheck

func (t *ModbusTransport) NeedProbeCheck() bool

func (*ModbusTransport) ProbeConnection

func (t *ModbusTransport) ProbeConnection()

func (*ModbusTransport) ReadCoil

func (t *ModbusTransport) ReadCoil(ctx context.Context, offset uint16) (bool, error)

func (*ModbusTransport) ReadCustom

func (t *ModbusTransport) ReadCustom(ctx context.Context, funcCode byte, offset uint16, count uint16) ([]byte, error)

ReadCustom 使用自定义功能码读取数据(暂不支持)

func (*ModbusTransport) ReadDiscreteInput

func (t *ModbusTransport) ReadDiscreteInput(ctx context.Context, offset uint16) (bool, error)

func (*ModbusTransport) ReadRegisters

func (t *ModbusTransport) ReadRegisters(ctx context.Context, regType string, offset uint16, count uint16) ([]byte, error)

func (*ModbusTransport) RecordFailure

func (t *ModbusTransport) RecordFailure(err error)

func (*ModbusTransport) RecordSuccess

func (t *ModbusTransport) RecordSuccess()

func (*ModbusTransport) SetMetricsRecorder

func (t *ModbusTransport) SetMetricsRecorder(recorder MetricsRecorder, channelID string)

SetMetricsRecorder 设置指标收集器

func (*ModbusTransport) SetUnitID

func (t *ModbusTransport) SetUnitID(id uint8)

func (*ModbusTransport) WriteCoil

func (t *ModbusTransport) WriteCoil(ctx context.Context, offset uint16, value bool) error

func (*ModbusTransport) WriteRegister

func (t *ModbusTransport) WriteRegister(ctx context.Context, offset uint16, value uint16) error

func (*ModbusTransport) WriteRegisters

func (t *ModbusTransport) WriteRegisters(ctx context.Context, offset uint16, values []uint16) error

type PointDecoder

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

PointDecoder 实现 Decoder 接口

func NewPointDecoder

func NewPointDecoder(byteOrder4 string, startAddress int, addressBase uint16) *PointDecoder

func (*PointDecoder) Decode

func (d *PointDecoder) Decode(point model.Point, raw []byte) (any, string, error)

Decode 解码原始字节数据

func (*PointDecoder) EnableDataformatDecoder

func (d *PointDecoder) EnableDataformatDecoder(enable bool)

func (*PointDecoder) Encode

func (d *PointDecoder) Encode(point model.Point, value any) ([]uint16, error)

Encode 将值编码为寄存器数组(用于写入)

func (*PointDecoder) GetRegisterCount

func (d *PointDecoder) GetRegisterCount(dataType string) uint16

GetRegisterCount 根据数据类型获取占用的寄存器数

func (*PointDecoder) ParseAddress

func (d *PointDecoder) ParseAddress(addr string) (model.RegisterType, uint16, error)

ParseAddress 解析Modbus点位地址 支持两种格式: 1. 标准格式:40001-50000(Holding), 30001-40000(Input), 10001-20000(Discrete), 1-10000(Coil) 2. 裸地址格式:直接使用偏移量,需配合register_type字段 3. 范围格式:如 "0-1" 表示从地址0开始的连续寄存器

type PointGroup

type PointGroup struct {
	RegType        model.RegisterType // 寄存器类型
	StartOffset    uint16             // 起始地址
	Count          uint16             // 数量
	Points         []model.Point      // 该组中的所有点位
	CustomFuncCode byte               // 自定义功能码(当RegType为RegCustom时使用)
}

PointGroup 表示一组连续的点位及其地址信息

type PointRuntime

type PointRuntime struct {
	Point         model.Point
	FailCount     int
	LastSuccess   time.Time
	State         string // OK, SKIPPED
	CooldownUntil time.Time
}

PointRuntime 点位运行态状态

type PointScheduler

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

PointScheduler 实现 Scheduler 接口

func NewPointScheduler

func NewPointScheduler(transport Transport, decoder Decoder, maxPacketSize uint16, groupThreshold uint16, instructionInterval time.Duration) *PointScheduler

func (*PointScheduler) GetDecoder

func (s *PointScheduler) GetDecoder() Decoder

func (*PointScheduler) GetSlaveID

func (s *PointScheduler) GetSlaveID() uint8

func (*PointScheduler) Read

func (s *PointScheduler) Read(ctx context.Context, points []model.Point) (map[string]model.Value, error)

func (*PointScheduler) SetGroupThreshold

func (s *PointScheduler) SetGroupThreshold(threshold uint16)

SetGroupThreshold 设置块读合并的最大地址间隙(来自 GapOptimizer)。

func (*PointScheduler) SetMaxPacketSize

func (s *PointScheduler) SetMaxPacketSize(m uint16)

SetMaxPacketSize allows updating the scheduler's maximum packet size (e.g. after MTU probe)

func (*PointScheduler) SetSlaveID

func (s *PointScheduler) SetSlaveID(slaveID uint8)

func (*PointScheduler) Write

func (s *PointScheduler) Write(ctx context.Context, point model.Point, value any) error

type RTTModel

type RTTModel struct {
	Samples map[int][]time.Duration
}

func NewRTTModel

func NewRTTModel() *RTTModel

func (*RTTModel) BestBatchSize

func (m *RTTModel) BestBatchSize() int

func (*RTTModel) Record

func (m *RTTModel) Record(size int, rtt time.Duration)

type Scheduler

type Scheduler interface {
	Read(ctx context.Context, points []model.Point) (map[string]model.Value, error)
	Write(ctx context.Context, point model.Point, value any) error
	GetDecoder() Decoder
}

Scheduler 接口定义

type Transport

type Transport interface {
	Connect(ctx context.Context) error
	Disconnect() error
	IsConnected() bool

	ReadRegisters(ctx context.Context, regType string, offset uint16, count uint16) ([]byte, error)
	ReadCoil(ctx context.Context, offset uint16) (bool, error)
	ReadDiscreteInput(ctx context.Context, offset uint16) (bool, error)
	ReadCustom(ctx context.Context, funcCode byte, offset uint16, count uint16) ([]byte, error)

	WriteRegister(ctx context.Context, offset uint16, value uint16) error
	WriteRegisters(ctx context.Context, offset uint16, values []uint16) error
	WriteCoil(ctx context.Context, offset uint16, value bool) error

	SetUnitID(id uint8)
	GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string, lastDisconnectTime time.Time)
}

Transport 接口定义

Jump to

Keyboard shortcuts

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