s7

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	S7AreaDB = 0x84 // 数据块
	S7AreaMK = 0x83 // 标志存储器 (M区)
	S7AreaPE = 0x81 // 输入过程映像 (I区)
	S7AreaPA = 0x80 // 输出过程映像 (Q区)
	S7AreaTM = 0x1D // 定时器
	S7AreaCT = 0x1C // 计数器
)

S7 区域常量

View Source
const (
	S7WLBit     = 0x01
	S7WLByte    = 0x02
	S7WLWord    = 0x04
	S7WLDWord   = 0x06
	S7WLReal    = 0x08
	S7WLCounter = 0x1C
	S7WLTimer   = 0x1D
)

S7 字长常量

View Source
const (
	ConnTypePG      = 1 // 编程设备连接
	ConnTypeOP      = 2 // 操作面板连接
	ConnTypeS7Basic = 3 // 基本S7连接
)

S7 连接类型

Variables

This section is empty.

Functions

func CSVToPoints

func CSVToPoints(csvPoints []CSVPointConfig) ([]model.Point, error)

CSVToPoints 将CSV配置转换为Point列表

func ConvertCSVToS7Address

func ConvertCSVToS7Address(ioAddress string) (string, error)

ConvertCSVToS7Address 将CSV I/O地址转换为S7地址格式 输入: Device1.DB1.BOOL.7006.7 -> 输出: DB1.DBX7006.7 输入: Device1.DB1.REAL.7500 -> 输出: DB1.DBD7500 输入: Device1.DB1.LREAL.7500 -> 输出: DB1.DBD7500 输入: Device1.Q.BOOL.1.3 -> 输出: Q1.3 输入: Device1.I.BOOL.0.0 -> 输出: I0.0

func ConvertCSVTypeToS7DataType

func ConvertCSVTypeToS7DataType(csvType string) string

ConvertCSVTypeToS7DataType 将CSV数据类型转换为S7数据类型

func DataTypeToWordLen

func DataTypeToWordLen(dataType string) int

DataTypeToWordLen 将数据类型字符串映射到S7 WordLen

func NewS7Driver

func NewS7Driver() driver.Driver

Types

type CSVPointConfig

type CSVPointConfig struct {
	TagName     string // 标签名
	Type        string // 数据类型 (BOOL, LREAL, REAL, DWORD)
	Description string // 描述
	IOAddress   string // I/O地址 (Device1.DB1.BOOL.7006.7)
	Unit        string // 单位
	DataGroup   string // 数据分组
	ReadOnly    bool   // 是否只读
}

CSVPointConfig CSV导入的点位配置

func ParseCSVFile

func ParseCSVFile(filePath string) ([]CSVPointConfig, error)

ParseCSVFile 解析CSV文件,返回点位配置列表

func ParseCSVReader

func ParseCSVReader(reader io.Reader) ([]CSVPointConfig, error)

ParseCSVReader 从reader解析CSV数据

type Client

type Client = gos7.Client

Client 接口别名,方便mock测试

type DataGroupStats

type DataGroupStats struct {
	Group  string
	Count  int
	Points []CSVPointConfig
}

DataGroupStats 数据分组统计

func GroupByDataGroup

func GroupByDataGroup(csvPoints []CSVPointConfig) []DataGroupStats

GroupByDataGroup 按数据分组统计

type PointGroup

type PointGroup struct {
	Area     int
	DBNumber int
	Points   []pointWithArea
}

PointGroup 点位分组,按Area+DBNumber分组

type S7Area

type S7Area struct {
	Area     int  // S7AreaDB, S7AreaMK, etc.
	DBNumber int  // 数据块号(仅DB区域有效)
	ByteOff  int  // 字节偏移
	BitOff   int  // 位偏移(仅位操作有效)
	WordLen  int  // 字长:S7WLBit, S7WLByte, S7WLWord, S7WLDWord, S7WLReal
	IsBit    bool // 是否为位操作
}

S7Area S7地址区域信息

type S7ClientHandler

type S7ClientHandler interface {
	gos7.ClientHandler
	Connect() error
	Close() error
	Timeout() time.Duration
	SetTimeout(timeout time.Duration)
	IdleTimeout() time.Duration
	SetIdleTimeout(timeout time.Duration)
}

S7ClientHandler 扩展gos7.ClientHandler,添加连接管理方法

type S7Decoder

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

S7Decoder S7地址解码器

func NewS7Decoder

func NewS7Decoder() *S7Decoder

NewS7Decoder 创建S7解码器

func (*S7Decoder) DecodeValue

func (d *S7Decoder) DecodeValue(buffer []byte, area *S7Area, dataType string) (interface{}, error)

DecodeValue 从字节缓冲区解码值

func (*S7Decoder) EncodeValue

func (d *S7Decoder) EncodeValue(buffer []byte, area *S7Area, dataType string, value interface{}) error

EncodeValue 将值编码到字节缓冲区

func (*S7Decoder) ParseAddress

func (d *S7Decoder) ParseAddress(addr string) (*S7Area, error)

ParseAddress 解析S7地址字符串 支持格式:

DB1.DBD0    -> DB双字 (float32/int32/uint32)
DB1.DBW2    -> DB字 (int16/uint16)
DB1.DBX0.1  -> DB位 (bool)
DB1.DBB4    -> DB字节 (int8/uint8)
M0.0        -> M区位 (bool)
MD0         -> M区双字
MW0         -> M区字
MB0         -> M区字节
I0.0        -> 输入位 (bool)
ID0         -> 输入双字
Q0.0        -> 输出位 (bool)
QD0         -> 输出双字
T0          -> 定时器
C0          -> 计数器

func (*S7Decoder) ReadSizeForArea

func (d *S7Decoder) ReadSizeForArea(area *S7Area) int

ReadSizeForArea 根据区域信息确定需要读取的字节数

type S7Driver

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

S7Driver S7协议驱动(真实实现)

func (*S7Driver) Connect

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

func (*S7Driver) Disconnect

func (d *S7Driver) Disconnect() error

func (*S7Driver) GetConnectionMetrics

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

func (*S7Driver) GetMetrics

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

GetMetrics 返回S7驱动的详细指标

func (*S7Driver) Health

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

func (*S7Driver) Init

func (d *S7Driver) Init(cfg model.DriverConfig) error

func (*S7Driver) ReadPoints

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

func (*S7Driver) SetDeviceConfig

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

func (*S7Driver) SetSlaveID

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

func (*S7Driver) WritePoint

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

type S7Scheduler

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

S7Scheduler S7调度器,负责批量分组读写

func NewS7Scheduler

func NewS7Scheduler(transport *S7Transport, decoder *S7Decoder, cfg map[string]any) *S7Scheduler

NewS7Scheduler 创建S7调度器

func (*S7Scheduler) GetStats

func (s *S7Scheduler) GetStats() (total, success, failure int64)

GetStats 获取统计信息

func (*S7Scheduler) ReadPoints

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

ReadPoints 批量读取点位

func (*S7Scheduler) WritePoint

func (s *S7Scheduler) WritePoint(ctx context.Context, p model.Point, value interface{}) error

WritePoint 写入单个点位

type S7Transport

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

S7Transport S7传输层,封装gos7连接管理

func NewS7Transport

func NewS7Transport(cfg map[string]any) *S7Transport

NewS7Transport 创建S7传输层实例

func (*S7Transport) Connect

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

Connect 建立S7 TCP连接

func (*S7Transport) Disconnect

func (t *S7Transport) Disconnect() error

Disconnect 断开连接

func (*S7Transport) GetClient

func (t *S7Transport) GetClient() gos7.Client

GetClient 获取gos7客户端

func (*S7Transport) GetConnectionMetrics

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

GetConnectionMetrics 获取连接指标

func (*S7Transport) GetHealthStatus

func (t *S7Transport) GetHealthStatus() (connected bool, failCount int32, maxFailCount int32, lastSuccess time.Time)

GetHealthStatus 获取健康状态信息

func (*S7Transport) IsConnected

func (t *S7Transport) IsConnected() bool

IsConnected 是否已连接

func (*S7Transport) NeedProbeCheck

func (t *S7Transport) NeedProbeCheck() bool

NeedProbeCheck 检查是否需要轻量探测(低频采集场景补偿)

func (*S7Transport) ProbeConnection

func (t *S7Transport) ProbeConnection() bool

ProbeConnection 轻量探测连接是否存活

func (*S7Transport) RecordFailure

func (t *S7Transport) RecordFailure(err error)

RecordFailure 记录采集失败,达到阈值时断开连接

func (*S7Transport) RecordSuccess

func (t *S7Transport) RecordSuccess()

RecordSuccess 记录采集成功,重置失败计数

Jump to

Keyboard shortcuts

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