Using secs4go for GEM Host and Equipment Development
- 1. 1. Project Layout
- 2. 2. HSMS Connection Basics
- 3. 3. Creating a GEM Handler
- 4. 4. Equipment Development Workflow
- 5. 5. Host Development Workflow
- 6. 6. Handling Callbacks and Extensions
- 7. 7. Logging HSMS/GEM Traffic
- 8. 8. Interoperability Checks
- 9. 9. Common Pitfalls & Tips
- 10. 10. Getting Started Quickly
This guide walks through the essential concepts required to build a GEM-compliant host or equipment application with the secs4go project.
1. Project Layout
1 | secs4go/ |
2. HSMS Connection Basics
Create an hsms.HsmsProtocol for the peer you want to connect to. The constructor accepts:
address/portof the remote peeractive(truefor host/client,falsefor equipment/server)- HSMS
sessionID name(used only for logging)
1 | protocol := hsms.NewHsmsProtocol("127.0.0.1", 5000, false, 0x200, "eqp") |
sessionID is the application session used for SECS-II data once the connection is SELECTED. Control packets such as select.req may legally use the wildcard 0xFFFF; the Go stack automatically adopts the negotiated session in that case.
3. Creating a GEM Handler
Wrap the HSMS layer with a GEM handler:
1 | handler, err := gem.NewGemHandler(gem.Options{ |
Key options:
DeviceType:gem.DeviceHostorgem.DeviceEquipment- Equipment-only fields:
MDLN,SOFTREV EstablishCommunicationWait,InitialControlState,InitialOnlineModeLogging: controls on-the-wire diagnostics (see section 7)
Activate the handler with handler.Enable() and optionally wait for handler.WaitForCommunicating(timeout).
4. Equipment Development Workflow
-
Register status/data variables
1
2
3
4
5
6sv, _ := gem.NewStatusVariable(1001, "Temperature", "C",
gem.WithStatusValueProvider(func() (ast.ItemNode, error) {
return ast.NewUintNode(4, 25), nil
}),
)
handler.RegisterStatusVariable(sv) -
Register equipment constants supply min/max/default values and update callbacks as needed.
-
Declare collection events & reports
handler.RegisterCollectionEventandhandler.RegisterDataVariabledefine CEIDs and VIDs the host can subscribe to. -
Remote commands install a handler via
handler.SetRemoteCommandHandlerto process S2F41 requests. -
Process programs store recipes with
handler.RegisterProcessProgramor override upload/download callbacks. -
Alarms register with
handler.RegisterAlarmand raise viahandler.RaiseAlarm.
The equipment example (runPythonHostEquipment in example/gem_pytest/main.go) demonstrates these capabilities, including a CEID that reports both status and data variables.
5. Host Development Workflow
-
Query metadata & values use
RequestStatusVariableInfo,RequestStatusVariables,RequestEquipmentConstantInfo, etc. -
Define and link reports always clear stale definitions first:
1
2
3
4
5
6
7handler.DefineReports() // S2F33 clear
handler.DefineReports(gem.ReportDefinitionRequest{
ReportID: 4001,
VIDs: []interface{}{1011, 2001},
})
handler.LinkEventReports(gem.EventReportLinkRequest{CEID: 3001, ReportIDs: []interface{}{4001}})
handler.EnableEventReports(true, 3001) -
Collection event snapshots
handler.RequestCollectionEventReport(ceid)wraps S6F15/S6F16. -
Process program upload/download
UploadProcessProgram(S7F3) andRequestProcessProgram(S7F5). Handle non-zero ACKs gracefully. -
Remote commands
handler.SendRemoteCommand("START", params)returns HCACK;0is success,4means acknowledged, finish later.
6. Handling Callbacks and Extensions
- Custom stream handlers
handler.RegisterStreamFunctionHandler(stream, function, fn)to intercept specific SECS-II messages,nilto remove. - Default stream handler
handler.RegisterDefaultStreamHandler(fn)installs a fallback. - Event subscriptions
handler.Events()exposes typed callbacks, e.g.EventReportReceived. - Timeout tuning
protocol.Timeouts()exposes setters for T3/T5/T6/T7/T8 and linktest intervals.
7. Logging HSMS/GEM Traffic
Detailed wire logging can be toggled through gem.LoggingOptions:
1 | handler, err := gem.NewGemHandler(gem.Options{ |
LoggingModeSMLprints human-readable SML (default when enabled).LoggingModeBinaryprints hexadecimal HSMS frames.LoggingModeBothprints SML followed by the hex dump.- Provide a custom
io.WriterviaLoggingOptions.Writerto redirect output. - Adjust behaviour at runtime with
protocol.ConfigureLogging.
8. Interoperability Checks
Run the built-in tests:
1 | cd secs4go |
The gem package integration test exercises host?equipment flows.
9. Common Pitfalls & Tips
- Report redefinition equipment persists S2F33 definitions; clear them before redefining.
- Session ID control packets may use
0xFFFF; the Go stack adapts automatically. - ACK/HCACK handling non-zero codes are normal; design retries or fallbacks instead of panicking.
- Linktest enable linktest for long-lived connections.
10. Getting Started Quickly
- Go-only loopback:
go run ./example/example1_passive_equipmentandgo run ./example/example2_active_host --scenario all. - Interop with Python (secsgem):
- Equipment:
python samples/gem_equipment.py - Host:
go run example/gem_pytest/main.go --role host
- Equipment:
- Observe the logs (optionally with logging enabled) to see each GEM capability exercised.
From here, adapt the examples by registering your own data sources, process program handlers, or remote commands to build production-ready equipment or host applications.
本文标题:Using secs4go for GEM Host and Equipment Development
文章作者:小师
发布时间:2025-09-20
最后更新:2025-09-20
原始链接:chunlife.top/2025/09/20/User-Guide-for-secs4go/
版权声明:本站所有文章均采用知识共享署名4.0国际许可协议进行许可