sim

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package sim provides a synthetic driver.Driver that generates values from simple time-based waveforms, so silk HMIs can run, be demoed and be tested without any real PLC or field device attached (the "仿真" / simulation mode familiar from FameView and other SCADA tools).

A point's Address encodes the waveform and its parameters as "<wave>:<params>":

sine:min=0,max=100,period=10s   smooth 0.5+0.5*sin, one full cycle per period
ramp:min=0,max=100,period=5s    linear sawtooth min->max, repeating each period
toggle:period=2s                bool, flips every period
const:42                        a fixed number
random:min=0,max=100            a value in [min,max], deterministic per instant

Params are comma-separated key=value pairs; min and max default to 0 and 100, period defaults to 1s when omitted. const takes a single bare number instead of key=value pairs. period is any Go duration (time.ParseDuration).

Waveform values track elapsed = Now()-start, where start is captured in NewSim and Now is an injectable clock (time.Now in production, a pinned function in tests). WritePoint records an override keyed by Address; a subsequent ReadPoint of that Address returns the written value instead of the waveform, so a read-write point behaves like a holding register once set.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sim

type Sim struct {
	// Now is the clock used for elapsed time. Defaults to time.Now in NewSim;
	// tests replace it with a pinned function to control the waveform phase.
	Now func() time.Time
	// contains filtered or unexported fields
}

Sim is a hardware-free driver.Driver. Connect/Close are no-ops. ReadPoint derives a value from the point's Address waveform (or a prior WritePoint override); WritePoint stores an override under the Address.

func NewSim

func NewSim() *Sim

NewSim builds a simulator with the real clock and the current time as its waveform origin.

func (*Sim) Close

func (s *Sim) Close() error

Close is a no-op.

func (*Sim) Connect

func (s *Sim) Connect() error

Connect is a no-op: the simulator has no device to reach.

func (*Sim) ReadPoint

func (s *Sim) ReadPoint(p driver.TagPoint) (interface{}, error)

ReadPoint returns the override for p.Address if one was written, otherwise the point's waveform value at the current elapsed time. It errors if the Address is not a valid "<wave>:<params>" spec.

func (*Sim) WritePoint

func (s *Sim) WritePoint(p driver.TagPoint, v interface{}) error

WritePoint records v as an override for p.Address; later reads of that Address return v until overwritten.

Jump to

Keyboard shortcuts

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