serial_svc

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandManager

type CommandManager interface {
	GetSessionByAssetID(assetID int64) (CommandSession, bool)
}

CommandManager 是供 AI 查找活跃串口会话使用的最小管理器接口。

type CommandSession

type CommandSession interface {
	ExecCommand(command string, silenceTimeout, maxTimeout time.Duration) (string, error)
}

CommandSession 是供 AI 串口命令执行使用的最小会话接口。

type ConnectConfig

type ConnectConfig struct {
	PortPath    string
	BaudRate    int
	DataBits    int
	StopBits    string
	Parity      string
	FlowControl string
	AssetID     int64
}

ConnectConfig 串口连接配置

type Manager

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

Manager 管理所有串口会话

func NewManager

func NewManager() *Manager

NewManager 创建串口会话管理器

func (*Manager) CloseAll

func (m *Manager) CloseAll()

CloseAll 关闭所有活跃串口会话。

func (*Manager) Connect

func (m *Manager) Connect(cfg ConnectConfig) (string, error)

Connect 打开串口连接,返回 sessionId。调用方通过 SetCallbacks 设置回调。 若未在宽限时间内设置回调,会话会自动关闭并清理,避免悬挂。

func (*Manager) Disconnect

func (m *Manager) Disconnect(sessionID string)

Disconnect 断开串口连接

func (*Manager) GetSession

func (m *Manager) GetSession(sessionID string) (*Session, bool)

GetSession 获取活跃会话

func (*Manager) GetSessionByAssetID

func (m *Manager) GetSessionByAssetID(assetID int64) (CommandSession, bool)

GetSessionByAssetID 根据资产 ID 查找活跃的串口会话

func (*Manager) ListPorts

func (m *Manager) ListPorts() ([]SerialPortInfo, error)

ListPorts 列出系统可用串口

func (*Manager) SetCallbacks

func (m *Manager) SetCallbacks(sessionID string, onData func(data []byte), onClosed func(sessionID string))

SetCallbacks 设置会话的数据和关闭回调(在 Connect 返回 sessionId 后调用)。 回调设置完成后才启动读取 goroutine,避免 SetCallbacks 调用前的数据丢失。

func (*Manager) TestConnection

func (m *Manager) TestConnection(ctx context.Context, cfg ConnectConfig) error

TestConnection opens a serial port and immediately closes it. It returns as soon as ctx is canceled; if the underlying open call completes after cancellation, the temporary session is still closed in the background.

type SerialPortInfo

type SerialPortInfo struct {
	Name         string `json:"name"`
	DisplayName  string `json:"displayName"`
	ProductID    string `json:"productId,omitempty"`
	VendorID     string `json:"vendorId,omitempty"`
	SerialNumber string `json:"serialNumber,omitempty"`
}

SerialPortInfo 可用串口信息

type Session

type Session struct {
	ID      string
	AssetID int64
	// contains filtered or unexported fields
}

Session 表示一个活跃的串口终端会话

func (*Session) Close

func (s *Session) Close()

Close 关闭串口会话

func (*Session) ExecCommand

func (s *Session) ExecCommand(command string, silenceTimeout, maxTimeout time.Duration) (string, error)

ExecCommand 向串口发送命令并收集输出。 适用于 AI 工具调用场景:写入命令后等待输出静默(silenceTimeout)或达到最大等待时间。

func (*Session) IsClosed

func (s *Session) IsClosed() bool

IsClosed 检查是否已关闭

func (*Session) Resize

func (s *Session) Resize(cols, rows int) error

Resize 调整终端尺寸(串口无实际作用,保留接口兼容)

func (*Session) Write

func (s *Session) Write(data []byte) error

Write 向串口写入数据(用户输入)

Jump to

Keyboard shortcuts

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