opcda

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 11 Imported by: 0

README

Go OPC DA Client

OPC_DA_WIN codecov Go Report Card Go Reference

English | 简体中文

This is an OPC DA client written in Go language, allowing you to communicate with OPC DA servers and retrieve data. OPC DA is a commonly used industrial automation communication protocol that enables data exchange between devices and control systems.

Features

  • Get all OPC DA servers
  • Connect to OPC DA server
  • Browse tags on OPC DA server
  • Synchronously read tag values
  • Asynchronously read tag values
  • Synchronously write tag values
  • Asynchronously write tag values
  • Subscribe to real-time data changes of tags
  • Robustness: Defensive nil-checks and error handling to prevent runtime panics.
  • Dependency Injection: Decoupled architecture for improved testability and mocking.
  • Zero-Dependency Testing: Pure unit tests using mocks, runnable in any environment.
  • Documentation: 100% Godoc compliance for all symbols.

Prerequisites

Before using this client, make sure you meet the following prerequisites:

  • Windows operating system with amd64/i386 architecture
  • Go version 1.20 or higher

Go 1.20 is the last version that supports Microsoft Windows 7 / 8 / Server 2008 / Server 2012. To ensure compatibility, this client will continue to support Go 1.20.

Testing is done with both Go 1.20 and the latest version of Go, covering both 32-bit and 64-bit testing.

Installation

Use the following command to install this client:

go get github.com/wends155/opcda

Types

This client provides support for the following types:

OPC Type Go Type Description
VT_BOOL bool Boolean value
VT_I1 int8 8-bit signed integer
VT_I2 int16 16-bit signed integer
VT_I4 int32 32-bit signed integer
VT_I8 int64 64-bit signed integer
VT_UI1 uint8 8-bit unsigned integer
VT_UI2 uint16 16-bit unsigned integer
VT_UI4 uint32 32-bit unsigned integer
VT_UI8 uint64 64-bit unsigned integer
VT_R4 float32 32-bit floating point number
VT_R8 float64 64-bit floating point number
VT_BSTR string String
VT_DATE time.Time Date time
VT_ARRAY|VT_BOOL []bool Boolean array
VT_ARRAY|VT_I1 []int8 8-bit signed integer array
VT_ARRAY|VT_I2 []int16 16-bit signed integer array
VT_ARRAY|VT_I4 []int32 32-bit signed integer array
VT_ARRAY|VT_I8 []int64 64-bit signed integer array
VT_ARRAY|VT_UI1 []uint8 8-bit unsigned integer array
VT_ARRAY|VT_UI2 []uint16 16-bit unsigned integer array
VT_ARRAY|VT_UI4 []uint32 32-bit unsigned integer array
VT_ARRAY|VT_UI8 []uint64 64-bit unsigned integer array
VT_ARRAY|VT_R4 []float32 32-bit floating point number array
VT_ARRAY|VT_R8 []float64 64-bit floating point number array
VT_ARRAY|VT_BSTR []string String array
VT_ARRAY|VT_DATE []time.Time Date time array

Other types are not currently supported.

Usage Examples

API Documentation

All APIs can be found in the API documentation.

Why Choose This Client

  1. There is currently no mature OPC DA client in the Go language ecosystem, and this client was developed to fill this gap.
  2. This client is written purely in Go language and supports both 32-bit and 64-bit architectures.
  3. This client also provides relatively complete functionality, including reading, writing, and subscribing to real-time data changes of tags.
Why Not konimarti/opc
  1. konimarti/opc uses the OPC DA Automation Wrapper interface, which cannot be modified for bug fixes. For example, using the Graybox DA Automation Wrapper cannot compile for 64-bit operation (HIGHENTROPYVA is enabled by default, leading to out-of-bounds memory address conversion within the Wrapper).
  2. There are memory leaks when reading string types.
  3. Insufficient functionality, such as inability to subscribe to real-time data changes of tags.
  4. go-ole has not been updated #252, making it unable to obtain millisecond timestamps.

Frequently Questions

  1. Cross-platform support

    This client only supports the Windows operating system.

  2. Multi-platform compilation

    This client cannot be compiled on non-Windows platforms because it relies on Windows platform COM interfaces. If the program needs to support multiple platforms, interfaces can be encapsulated, and non-Windows platform interfaces can be set to empty implementations.

  3. Memory leaks

    This client uses COM interfaces, and memory release has been handled internally, with fatigue testing done for all supported types. All public methods are guarded with defensive nil-checks to return errors gracefully instead of panicking on uninitialized objects.

Development Tools

To streamline common tasks, we provide helper scripts in the scripts/ directory:

  • .\scripts\gcom "message": Combines git add . and git commit -m.
  • .\scripts\gsync: Combines git pull --rebase and git push.

Testing

This project uses a combination of mock-based unit tests and server-dependent integration tests. A Makefile is provided for convenience.

Unit Tests (Mocks)

Pure unit tests that use mock providers to simulate OPC server behavior. These tests are fast, environment-agnostic, and do not require any COM initialization.

make test
# OR
go test -v ./...
Integration Tests

Tests that interact with live OPC servers or the local Windows registry. These are isolated using the integration build tag.

make integration
# OR
go test -v -tags integration ./...
Log Management

All temporary test logs and artifacts are automatically directed to the ./logs/ directory to keep the workspace clean. Use make clean to remove them.

Documentation Examples

Go Example functions are provided in the test files. They serve as runnable documentation and can be viewed via godoc.

Documentation

Rendered for windows/amd64

Index

Examples

Constants

View Source
const (
	// OPC_READABLE indicates that the item is readable.
	OPC_READABLE uint32 = 0x1

	// OPC_WRITEABLE indicates that the item is writable.
	OPC_WRITEABLE = 0x2
)
View Source
const (
	// OPC_DS_CACHE indicates that the data should be read from the cache.
	OPC_DS_CACHE com.OPCDATASOURCE = 1
	// OPC_DS_DEVICE indicates that the data should be read from the device.
	OPC_DS_DEVICE com.OPCDATASOURCE = OPC_DS_CACHE + 1
)
View Source
const (
	// OPC_NS_HIERARCHIAL indicates that the address space is hierarchical.
	OPC_NS_HIERARCHIAL com.OPCNAMESPACETYPE = 1
	// OPC_NS_FLAT indicates that the address space is flat.
	OPC_NS_FLAT com.OPCNAMESPACETYPE = OPC_NS_HIERARCHIAL + 1
)
View Source
const (
	// OPC_STATUS_RUNNING indicates that the server is running.
	OPC_STATUS_RUNNING com.OPCServerState = 1
	// OPC_STATUS_FAILED indicates that the server has failed.
	OPC_STATUS_FAILED = OPC_STATUS_RUNNING + 1
	// OPC_STATUS_NOCONFIG indicates that the server is not configured.
	OPC_STATUS_NOCONFIG = OPC_STATUS_FAILED + 1
	// OPC_STATUS_SUSPENDED indicates that the server is suspended.
	OPC_STATUS_SUSPENDED = OPC_STATUS_NOCONFIG + 1
	// OPC_STATUS_TEST indicates that the server is in test mode.
	OPC_STATUS_TEST = OPC_STATUS_SUSPENDED + 1
	// OPC_STATUS_COMM_FAULT indicates that the server has a communication fault.
	OPC_STATUS_COMM_FAULT = OPC_STATUS_TEST + 1
)
View Source
const (
	// OPC_BROWSE_UP indicates browsing up.
	OPC_BROWSE_UP com.OPCBROWSEDIRECTION = 1
	// OPC_BROWSE_DOWN indicates browsing down.
	OPC_BROWSE_DOWN com.OPCBROWSEDIRECTION = OPC_BROWSE_UP + 1
	// OPC_BROWSE_TO indicates browsing to a specific position.
	OPC_BROWSE_TO com.OPCBROWSEDIRECTION = OPC_BROWSE_DOWN + 1
)
View Source
const (
	// OPC_BRANCH indicates browsing branches.
	OPC_BRANCH com.OPCBROWSETYPE = 1
	// OPC_LEAF indicates browsing leaves.
	OPC_LEAF com.OPCBROWSETYPE = OPC_BRANCH + 1
	// OPC_FLAT indicates browsing flat.
	OPC_FLAT com.OPCBROWSETYPE = OPC_LEAF + 1
)
View Source
const (
	// OPC_ENUM_PRIVATE_CONNECTIONS indicates private connections.
	OPC_ENUM_PRIVATE_CONNECTIONS = 1
	// OPC_ENUM_PUBLIC_CONNECTIONS indicates public connections.
	OPC_ENUM_PUBLIC_CONNECTIONS = OPC_ENUM_PRIVATE_CONNECTIONS + 1
	// OPC_ENUM_ALL_CONNECTIONS indicates all connections.
	OPC_ENUM_ALL_CONNECTIONS = OPC_ENUM_PUBLIC_CONNECTIONS + 1
	// OPC_ENUM_PRIVATE indicates private enumerations.
	OPC_ENUM_PRIVATE = OPC_ENUM_ALL_CONNECTIONS + 1
	// OPC_ENUM_PUBLIC indicates public enumerations.
	OPC_ENUM_PUBLIC = OPC_ENUM_PRIVATE + 1
	// OPC_ENUM_ALL indicates all enumerations.
	OPC_ENUM_ALL = OPC_ENUM_PUBLIC + 1
)

Variables

View Source
var (
	OPCInvalidHandle   = uint32(0xC0040001)
	OPCBadType         = uint32(0xC0040004)
	OPCPublic          = uint32(0xC0040005)
	OPCBadRights       = uint32(0xC0040006)
	OPCUnknownItemID   = uint32(0xC0040007)
	OPCInvalidItemID   = uint32(0xC0040008)
	OPCInvalidFilter   = uint32(0xC0040009)
	OPCUnknownPath     = uint32(0xC004000A)
	OPCRange           = uint32(0xC004000B)
	OPCDuplicateName   = uint32(0xC004000C)
	OPCUnsupportedRate = uint32(0x0004000D)
	OPCClamp           = uint32(0x0004000E)
	OPCInuse           = uint32(0x0004000F)
	OPCInvalidConfig   = uint32(0xC0040010)
	OPCNotFound        = uint32(0xC0040011)
	OPCInvalidPID      = uint32(0xC0040203)
)
View Source
var IID_CATID_OPCDAServer10 = windows.GUID{
	Data1: 0x63D5F430,
	Data2: 0xCFE4,
	Data3: 0x11d1,
	Data4: [8]byte{0xB2, 0xC8, 0x00, 0x60, 0x08, 0x3B, 0xA1, 0xFB},
}

IID_CATID_OPCDAServer10 is the CATID for OPC DA 1.0 servers.

View Source
var IID_CATID_OPCDAServer20 = windows.GUID{
	Data1: 0x63D5F432,
	Data2: 0xCFE4,
	Data3: 0x11d1,
	Data4: [8]byte{0xB2, 0xC8, 0x00, 0x60, 0x08, 0x3B, 0xA1, 0xFB},
}

IID_CATID_OPCDAServer20 is the CATID for OPC DA 2.0 servers.

View Source
var IID_IOPCDataCallback = windows.GUID{
	Data1: 0x39c13a70,
	Data2: 0x011e,
	Data3: 0x11d0,
	Data4: [8]byte{0x96, 0x75, 0x00, 0x20, 0xaf, 0xd8, 0xad, 0xb3},
}

IID_IOPCDataCallback is the GUID for the IOPCDataCallback interface.

View Source
var IID_IOPCShutdown = windows.GUID{
	Data1: 0xF31DFDE1,
	Data2: 0x07B6,
	Data3: 0x11d2,
	Data4: [8]byte{0xB2, 0xD8, 0x00, 0x60, 0x08, 0x3B, 0xA1, 0xFB},
}

IID_IOPCShutdown is the GUID for the IOPCShutdown interface.

Functions

func DataAddRef

func DataAddRef(this unsafe.Pointer) uintptr

DataAddRef handles the AddRef COM method.

func DataOnCancelComplete

func DataOnCancelComplete(this unsafe.Pointer, dwTransid uint32, hGroup uint32) uintptr

DataOnCancelComplete handles the OnCancelComplete COM callback.

func DataOnDataChange

func DataOnDataChange(this unsafe.Pointer, dwTransid uint32, hGroup uint32, hrMasterquality int32, hrMastererror int32, dwCount uint32, phClientItems unsafe.Pointer, pvValues unsafe.Pointer, pwQualities unsafe.Pointer, pftTimeStamps unsafe.Pointer, pErrors unsafe.Pointer) uintptr

DataOnDataChange handles the OnDataChange COM callback.

func DataOnReadComplete

func DataOnReadComplete(this unsafe.Pointer, dwTransid uint32, hGroup uint32, hrMasterquality int32, hrMastererror int32, dwCount uint32, phClientItems unsafe.Pointer, pvValues unsafe.Pointer, pwQualities unsafe.Pointer, pftTimeStamps unsafe.Pointer, pErrors unsafe.Pointer) uintptr

DataOnReadComplete handles the OnReadComplete COM callback.

func DataOnWriteComplete

func DataOnWriteComplete(this unsafe.Pointer, dwTransid uint32, hGroup uint32, hrMastererr int32, dwCount uint32, pClienthandles unsafe.Pointer, pErrors unsafe.Pointer) uintptr

DataOnWriteComplete handles the OnWriteComplete COM callback.

func DataQueryInterface

func DataQueryInterface(this unsafe.Pointer, iid *windows.GUID, punk *unsafe.Pointer) uintptr

DataQueryInterface handles the QueryInterface COM method.

func DataRelease

func DataRelease(this unsafe.Pointer) uintptr

DataRelease handles the Release COM method.

func NewOPCWrapperError

func NewOPCWrapperError(info string, err error) error

func ShutdownAddRef

func ShutdownAddRef(this unsafe.Pointer) uintptr

func ShutdownQueryInterface

func ShutdownQueryInterface(this unsafe.Pointer, iid *windows.GUID, punk *unsafe.Pointer) uintptr

func ShutdownRelease

func ShutdownRelease(this unsafe.Pointer) uintptr

func ShutdownRequest

func ShutdownRequest(this *com.IUnknown, pReason *uint16) uintptr

Types

type CCancelCompleteCallBackData

type CCancelCompleteCallBackData struct {
	TransID     uint32
	GroupHandle uint32
}

CCancelCompleteCallBackData holds data for the OnCancelComplete event.

type CDataChangeCallBackData

type CDataChangeCallBackData struct {
	TransID           uint32
	GroupHandle       uint32
	MasterQuality     int32
	MasterErr         int32
	ItemClientHandles []uint32
	Values            []interface{}
	Qualities         []uint16
	TimeStamps        []time.Time
	Errors            []int32
}

CDataChangeCallBackData holds data for the OnDataChange event.

type CReadCompleteCallBackData

type CReadCompleteCallBackData struct {
	TransID           uint32
	GroupHandle       uint32
	MasterQuality     int32
	MasterErr         int32
	ItemClientHandles []uint32
	Values            []interface{}
	Qualities         []uint16
	TimeStamps        []time.Time
	Errors            []int32
}

CReadCompleteCallBackData holds data for the OnReadComplete event.

type CWriteCompleteCallBackData

type CWriteCompleteCallBackData struct {
	TransID           uint32
	GroupHandle       uint32
	MasterErr         int32
	ItemClientHandles []uint32
	Errors            []int32
}

CWriteCompleteCallBackData holds data for the OnWriteComplete event.

type CancelCompleteCallBackData

type CancelCompleteCallBackData struct {
	TransID     uint32
	GroupHandle uint32
}

type DataChangeCallBackData

type DataChangeCallBackData struct {
	TransID           uint32
	GroupHandle       uint32
	MasterQuality     int32
	MasterErr         error
	ItemClientHandles []uint32
	Values            []interface{}
	Qualities         []uint16
	TimeStamps        []time.Time
	Errors            []error
}

type DataEventReceiver

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

DataEventReceiver handles COM callbacks for data events.

func NewDataEventReceiver

func NewDataEventReceiver(
	dataChangeReceiver chan *CDataChangeCallBackData,
	readCompleteReceiver chan *CReadCompleteCallBackData,
	writeCompleteReceiver chan *CWriteCompleteCallBackData,
	cancelCompleteReceiver chan *CCancelCompleteCallBackData,
) *DataEventReceiver

NewDataEventReceiver creates a new DataEventReceiver.

type DataEventReceiverVtbl

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

DataEventReceiverVtbl defines the VTable for the DataEventReceiver COM object.

type OPCBrowser

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

OPCBrowser represents a browser for OPC item IDs.

func NewOPCBrowser

func NewOPCBrowser(parent *OPCServer) (*OPCBrowser, error)

NewOPCBrowser creates a new OPCBrowser instance.

func (*OPCBrowser) GetAccessRights

func (b *OPCBrowser) GetAccessRights() uint32

GetAccessRights returns the requested access rights that apply to the ShowLeafs methods.

func (*OPCBrowser) GetCount

func (b *OPCBrowser) GetCount() int

GetCount returns the number of items in the collection.

func (*OPCBrowser) GetCurrentPosition

func (b *OPCBrowser) GetCurrentPosition() (string, error)

GetCurrentPosition returns the current position in the tree.

func (*OPCBrowser) GetDataType

func (b *OPCBrowser) GetDataType() uint16

GetDataType returns the requested data type that applies to ShowLeafs methods. This property defaults to com.VT_EMPTY, which means that any data type is acceptable.

func (*OPCBrowser) GetFilter

func (b *OPCBrowser) GetFilter() string

GetFilter returns the filter that applies to ShowBranches and ShowLeafs methods.

func (*OPCBrowser) GetItemID

func (b *OPCBrowser) GetItemID(leaf string) (string, error)

GetItemID gives a name and returns a valid ItemID that can be passed to OPCItems Add method.

func (*OPCBrowser) GetOrganization

func (b *OPCBrowser) GetOrganization() (com.OPCNAMESPACETYPE, error)

GetOrganization returns either OPCHierarchical or OPCFlat.

func (*OPCBrowser) Item

func (b *OPCBrowser) Item(index int) (string, error)

Item returns the name of the item at the specified index. index is 0-based.

func (*OPCBrowser) MoveDown

func (b *OPCBrowser) MoveDown(name string) error

MoveDown moves down into this branch.

func (*OPCBrowser) MoveTo

func (b *OPCBrowser) MoveTo(branches []string) error

MoveTo moves to an absolute position.

func (*OPCBrowser) MoveToRoot

func (b *OPCBrowser) MoveToRoot()

MoveToRoot moves up to the first level in the tree.

func (*OPCBrowser) MoveUp

func (b *OPCBrowser) MoveUp() error

MoveUp moves up one level in the tree.

func (*OPCBrowser) Release

func (b *OPCBrowser) Release()

Release releases the OPCBrowser.

func (*OPCBrowser) SetAccessRights

func (b *OPCBrowser) SetAccessRights(accessRights uint32) error

SetAccessRights sets the requested access rights that apply to the ShowLeafs methods.

func (*OPCBrowser) SetDataType

func (b *OPCBrowser) SetDataType(dataType uint16)

SetDataType sets the requested data type that applies to ShowLeafs methods.

func (*OPCBrowser) SetFilter

func (b *OPCBrowser) SetFilter(filter string)

SetFilter sets the filter that applies to ShowBranches and ShowLeafs methods.

func (*OPCBrowser) ShowBranches

func (b *OPCBrowser) ShowBranches() error

ShowBranches fills the collection with names of the branches at the current browse position.

func (*OPCBrowser) ShowLeafs

func (b *OPCBrowser) ShowLeafs(flat bool) error

ShowLeafs fills the collection with the names of the leafs at the current browse position.

Example (Mock)
// Initialize browser with mock address space
mock := newMockBrowserProvider()
browser := newOPCBrowserWithProvider(mock, nil)

// Navigate to Folder1
browser.MoveDown("Folder1")

// Show leafs in Folder1
browser.ShowLeafs(false)
count := browser.GetCount()
for i := 0; i < count; i++ {
	name, _ := browser.Item(i)
	fmt.Println("Leaf:", name)
}

// Get fully qualified ItemID
itemID, _ := browser.GetItemID("Item1")
fmt.Println("ItemID:", itemID)
Output:
Leaf: Item1
Leaf: Item2
ItemID: Folder1.Item1

type OPCError

type OPCError struct {
	ErrorCode    int32
	ErrorMessage string
}

func (*OPCError) Error

func (e *OPCError) Error() string

type OPCGroup

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

OPCGroup represents a group of OPC items.

func NewOPCGroup

func NewOPCGroup(
	opcGroups *OPCGroups,
	iUnknown *com.IUnknown,
	clientGroupHandle uint32,
	serverGroupHandle uint32,
	groupName string,
	revisedUpdateRate uint32,
) (*OPCGroup, error)

NewOPCGroup creates a new OPCGroup instance.

func (*OPCGroup) AsyncCancel

func (g *OPCGroup) AsyncCancel(cancelID uint32) error

AsyncCancel Request that the server cancel an outstanding transaction. An AsyncCancelComplete event will occur indicating whether or not the cancel succeeded.

func (*OPCGroup) AsyncRead

func (g *OPCGroup) AsyncRead(
	serverHandles []uint32,
	clientTransactionID uint32,
) (cancelID uint32, errs []error, err error)

AsyncRead Read one or more items in a group. The results are returned via the AsyncReadComplete event associated with the OPCGroup object.

func (*OPCGroup) AsyncRefresh

func (g *OPCGroup) AsyncRefresh(
	source com.OPCDATASOURCE,
	clientTransactionID uint32,
) (cancelID uint32, err error)

AsyncRefresh Generate an event for all active items in the group (whether they have changed or not). Inactive items are not included in the callback. The results are returned via the DataChange event associated with the OPCGroup object.

func (*OPCGroup) AsyncWrite

func (g *OPCGroup) AsyncWrite(
	serverHandles []uint32,
	values []interface{},
	clientTransactionID uint32,
) (cancelID uint32, errs []error, err error)

AsyncWrite Write one or more items in a group. The results are returned via the AsyncWriteComplete event associated with the OPCGroup object.

func (*OPCGroup) GetClientHandle

func (g *OPCGroup) GetClientHandle() uint32

GetClientHandle returns the client handle associated with the group.

func (*OPCGroup) GetDeadband

func (g *OPCGroup) GetDeadband() (float32, error)

GetDeadband returns the deadband for the group. A deadband is expressed as percent of full scale (legal values 0 to 100).

func (*OPCGroup) GetIsActive

func (g *OPCGroup) GetIsActive() bool

GetIsActive returns whether the group is active.

func (*OPCGroup) GetLocaleID

func (g *OPCGroup) GetLocaleID() (uint32, error)

GetLocaleID returns the locale identifier for the group.

func (*OPCGroup) GetName

func (g *OPCGroup) GetName() string

GetName returns the name of the group.

func (*OPCGroup) GetParent

func (g *OPCGroup) GetParent() *OPCGroups

GetParent returns a reference to the parent OPCServer object.

func (*OPCGroup) GetServerHandle

func (g *OPCGroup) GetServerHandle() uint32

GetServerHandle returns the server assigned handle for the group.

func (*OPCGroup) GetTimeBias

func (g *OPCGroup) GetTimeBias() (int32, error)

GetTimeBias returns the time bias for the group. This property provides the information needed to convert the time stamp on the data back to the local time of the device.

func (*OPCGroup) GetUpdateRate

func (g *OPCGroup) GetUpdateRate() (uint32, error)

GetUpdateRate returns the update rate for the group. The fastest rate at which data change events may be fired. A slow process might cause data changes to fire at less than this rate, but they will never exceed this rate. Rate is in milliseconds. This property’s default depends on the value set in the OPCGroups Collection. Assigning a value to this property is a “request” for a new update rate. The server may not support that rate, so reading the property may result in a different rate (the server will use the closest rate it does support).

func (*OPCGroup) OPCItems

func (g *OPCGroup) OPCItems() *OPCItems

OPCItems A collection of OPCItem objects

func (*OPCGroup) RegisterCancelComplete

func (g *OPCGroup) RegisterCancelComplete(ch chan *CancelCompleteCallBackData) error

RegisterCancelComplete Register to receive cancel complete events

func (*OPCGroup) RegisterDataChange

func (g *OPCGroup) RegisterDataChange(ch chan *DataChangeCallBackData) error

RegisterDataChange Register to receive data change events

func (*OPCGroup) RegisterReadComplete

func (g *OPCGroup) RegisterReadComplete(ch chan *ReadCompleteCallBackData) error

RegisterReadComplete Register to receive read complete events

func (*OPCGroup) RegisterWriteComplete

func (g *OPCGroup) RegisterWriteComplete(ch chan *WriteCompleteCallBackData) error

RegisterWriteComplete Register to receive write complete events

func (*OPCGroup) Release

func (g *OPCGroup) Release()

Release Releases the resources used by the group

func (*OPCGroup) SetClientHandle

func (g *OPCGroup) SetClientHandle(clientHandle uint32) error

SetClientHandle sets the client handle associated with the group.

func (*OPCGroup) SetDeadband

func (g *OPCGroup) SetDeadband(deadband float32) error

SetDeadband sets the deadband for the group.

func (*OPCGroup) SetIsActive

func (g *OPCGroup) SetIsActive(isActive bool) error

SetIsActive sets whether the group is active.

func (*OPCGroup) SetLocaleID

func (g *OPCGroup) SetLocaleID(id uint32) error

SetLocaleID sets the locale identifier for the group.

func (*OPCGroup) SetName

func (g *OPCGroup) SetName(name string) error

SetName sets the name of the group.

func (*OPCGroup) SetTimeBias

func (g *OPCGroup) SetTimeBias(timeBias int32) error

SetTimeBias sets the time bias for the group.

func (*OPCGroup) SetUpdateRate

func (g *OPCGroup) SetUpdateRate(updateRate uint32) error

SetUpdateRate sets the update rate for the group.

func (*OPCGroup) SyncRead

func (g *OPCGroup) SyncRead(source com.OPCDATASOURCE, serverHandles []uint32) ([]*com.ItemState, []error, error)

SyncRead reads the value, quality and timestamp information for one or more items in a group.

func (*OPCGroup) SyncWrite

func (g *OPCGroup) SyncWrite(serverHandles []uint32, values []interface{}) ([]error, error)

SyncWrite Writes values to one or more items in a group

type OPCGroups

type OPCGroups struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewOPCGroups

func NewOPCGroups(opcServer *OPCServer) *OPCGroups

func (*OPCGroups) Add

func (gs *OPCGroups) Add(szName string) (*OPCGroup, error)

Add Creates a new OPCGroup object and adds it to the collections

func (*OPCGroups) GetCount

func (gs *OPCGroups) GetCount() int

GetCount Required property for collections.

func (*OPCGroups) GetDefaultGroupDeadband

func (gs *OPCGroups) GetDefaultGroupDeadband() float32

GetDefaultGroupDeadband get the default deadband for OPCGroups created using Groups.Add

func (*OPCGroups) GetDefaultGroupIsActive

func (gs *OPCGroups) GetDefaultGroupIsActive() bool

GetDefaultGroupIsActive get the default active state for OPCGroups created using Groups.Add

func (*OPCGroups) GetDefaultGroupLocaleID

func (gs *OPCGroups) GetDefaultGroupLocaleID() uint32

GetDefaultGroupLocaleID get the default locale for OPCGroups created using Groups.Add.

func (*OPCGroups) GetDefaultGroupTimeBias

func (gs *OPCGroups) GetDefaultGroupTimeBias() int32

GetDefaultGroupTimeBias get the default time bias for OPCGroups created using Groups.Add.

func (*OPCGroups) GetDefaultGroupUpdateRate

func (gs *OPCGroups) GetDefaultGroupUpdateRate() uint32

GetDefaultGroupUpdateRate get the default update rate (in milliseconds) for OPCGroups created using Groups.Add

func (*OPCGroups) GetOPCGroup

func (gs *OPCGroups) GetOPCGroup(serverHandle uint32) (*OPCGroup, error)

GetOPCGroup Returns an OPCGroup by server handle

func (*OPCGroups) GetOPCGroupByName

func (gs *OPCGroups) GetOPCGroupByName(name string) (*OPCGroup, error)

GetOPCGroupByName Returns an OPCGroup by name

func (*OPCGroups) GetParent

func (gs *OPCGroups) GetParent() *OPCServer

GetParent Returns reference to the parent OPCServer object.

func (*OPCGroups) Item

func (gs *OPCGroups) Item(index int32) (*OPCGroup, error)

Item Returns an OPCGroup by ItemSpecifier. ItemSpecifier is the name or 0-based index into the collection

func (*OPCGroups) ItemByName

func (gs *OPCGroups) ItemByName(name string) (*OPCGroup, error)

ItemByName Returns an OPCGroup by name

func (*OPCGroups) Release

func (gs *OPCGroups) Release() error

Release Releases the resources used by the collection and the items it contains.

func (*OPCGroups) Remove

func (gs *OPCGroups) Remove(serverHandle uint32) error

Remove Removes an OPCGroup from the collection

func (*OPCGroups) RemoveAll

func (gs *OPCGroups) RemoveAll() error

RemoveAll Removes all OPCGroups from the collection

func (*OPCGroups) RemoveByName

func (gs *OPCGroups) RemoveByName(name string) error

RemoveByName Removes an OPCGroup from the collection by name

func (*OPCGroups) SetDefaultGroupDeadband

func (gs *OPCGroups) SetDefaultGroupDeadband(defaultDeadband float32)

SetDefaultGroupDeadband set the default deadband for OPCGroups created using Groups.Add

func (*OPCGroups) SetDefaultGroupIsActive

func (gs *OPCGroups) SetDefaultGroupIsActive(defaultActive bool)

SetDefaultGroupIsActive set the default active state for OPCGroups created using Groups.Add

func (*OPCGroups) SetDefaultGroupLocaleID

func (gs *OPCGroups) SetDefaultGroupLocaleID(defaultLocaleID uint32)

SetDefaultGroupLocaleID set the default locale for OPCGroups created using Groups.Add.

func (*OPCGroups) SetDefaultGroupTimeBias

func (gs *OPCGroups) SetDefaultGroupTimeBias(defaultGroupTimeBias int32)

SetDefaultGroupTimeBias set the default time bias for OPCGroups created using Groups.Add.

func (*OPCGroups) SetDefaultGroupUpdateRate

func (gs *OPCGroups) SetDefaultGroupUpdateRate(defaultGroupUpdateRate uint32)

SetDefaultGroupUpdateRate set the default update rate (in milliseconds) for OPCGroups created using Groups.Add

type OPCItem

type OPCItem struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

OPCItem represents a single OPC item.

func NewOPCItem

func NewOPCItem(
	parent *OPCItems,
	tag string,
	result com.TagOPCITEMRESULTStruct,
	clientHandle uint32,
	accessPath string,
	isActive bool,
) *OPCItem

NewOPCItem creates a new OPCItem instance (internal constructor).

func (*OPCItem) GetAccessPath

func (i *OPCItem) GetAccessPath() string

GetAccessPath returns the access path for the item.

func (*OPCItem) GetAccessRights

func (i *OPCItem) GetAccessRights() uint32

GetAccessRights returns the access rights for the item.

func (*OPCItem) GetCanonicalDataType

func (i *OPCItem) GetCanonicalDataType() com.VT

GetCanonicalDataType returns the canonical data type for the item.

func (*OPCItem) GetClientHandle

func (i *OPCItem) GetClientHandle() uint32

GetClientHandle returns the client handle for the item.

func (*OPCItem) GetEUInfo

func (i *OPCItem) GetEUInfo() (interface{}, error)

GetEUInfo returns the EU info for the item.

func (*OPCItem) GetEUType

func (i *OPCItem) GetEUType() (int, error)

GetEUType returns the EU type for the item.

func (*OPCItem) GetIsActive

func (i *OPCItem) GetIsActive() bool

GetIsActive returns the active state for the item.

func (*OPCItem) GetItemID

func (i *OPCItem) GetItemID() string

GetItemID returns the item ID for the item.

func (*OPCItem) GetParent

func (i *OPCItem) GetParent() *OPCItems

GetParent returns a reference to the parent OPCItems object.

func (*OPCItem) GetQuality

func (i *OPCItem) GetQuality() uint16

GetQuality returns the latest quality read from the server.

func (*OPCItem) GetRequestedDataType

func (i *OPCItem) GetRequestedDataType() com.VT

GetRequestedDataType returns the requested data type for the item.

func (*OPCItem) GetServerHandle

func (i *OPCItem) GetServerHandle() uint32

GetServerHandle returns the server handle for the item.

func (*OPCItem) GetTimestamp

func (i *OPCItem) GetTimestamp() time.Time

GetTimestamp returns the latest timestamp read from the server.

func (*OPCItem) GetValue

func (i *OPCItem) GetValue() interface{}

GetValue returns the latest value read from the server.

func (*OPCItem) Read

func (i *OPCItem) Read(source com.OPCDATASOURCE) (interface{}, uint16, time.Time, error)

Read reads the value, quality and timestamp for the item.

func (*OPCItem) Release

func (i *OPCItem) Release()

Release Releases the OPCItem object

func (*OPCItem) SetClientHandle

func (i *OPCItem) SetClientHandle(clientHandle uint32) error

SetClientHandle sets the client handle for the item.

func (*OPCItem) SetIsActive

func (i *OPCItem) SetIsActive(isActive bool) error

SetIsActive sets the active state for the item.

func (*OPCItem) SetRequestedDataType

func (i *OPCItem) SetRequestedDataType(requestedDataType com.VT) error

SetRequestedDataType sets the requested data type for the item.

func (*OPCItem) Write

func (i *OPCItem) Write(value interface{}) error

Write writes a value to the item.

type OPCItems

type OPCItems struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

OPCItems represents a collection of OPC items.

func NewOPCItems

func NewOPCItems(
	parent *OPCGroup,
	itemMgt itemMgtProvider,
	provider serverProvider,
) *OPCItems

NewOPCItems creates a new OPCItems instance.

func (*OPCItems) AddItem

func (is *OPCItems) AddItem(tag string) (*OPCItem, error)

AddItem adds an item to the group.

func (*OPCItems) AddItems

func (is *OPCItems) AddItems(tags []string) ([]*OPCItem, []error, error)

AddItems adds multiple items to the collection.

func (*OPCItems) GetCount

func (is *OPCItems) GetCount() int

GetCount returns the number of items in the collection.

func (*OPCItems) GetDefaultAccessPath

func (is *OPCItems) GetDefaultAccessPath() string

GetDefaultAccessPath returns the default AccessPath that will be used in calls to Add.

func (*OPCItems) GetDefaultActive

func (is *OPCItems) GetDefaultActive() bool

GetDefaultActive returns the default active state for OPCItems created using Items.Add.

func (*OPCItems) GetDefaultRequestedDataType

func (is *OPCItems) GetDefaultRequestedDataType() com.VT

GetDefaultRequestedDataType returns the requested data type that will be used in calls to Add.

func (*OPCItems) GetOPCItem

func (is *OPCItems) GetOPCItem(serverHandle uint32) (*OPCItem, error)

GetOPCItem returns the OPCItem by serverHandle.

func (*OPCItems) GetParent

func (is *OPCItems) GetParent() *OPCGroup

GetParent returns a reference to the parent OPCGroup object.

func (*OPCItems) Item

func (is *OPCItems) Item(index int32) (*OPCItem, error)

Item returns the item by index.

func (*OPCItems) ItemByName

func (is *OPCItems) ItemByName(name string) (*OPCItem, error)

ItemByName returns the item by name.

func (*OPCItems) Release

func (is *OPCItems) Release()

Release releases the OPCItems collection and all associated resources.

func (*OPCItems) Remove

func (is *OPCItems) Remove(serverHandles []uint32)

Remove removes an OPCItem from the collection.

func (*OPCItems) SetActive

func (is *OPCItems) SetActive(serverHandles []uint32, active bool) []error

SetActive allows activation and deactivation of individual OPCItems in the OPCItems collection.

func (*OPCItems) SetClientHandles

func (is *OPCItems) SetClientHandles(serverHandles []uint32, clientHandles []uint32) []error

SetClientHandles changes the client handles for one or more items in the collection.

func (*OPCItems) SetDataTypes

func (is *OPCItems) SetDataTypes(serverHandles []uint32, requestedDataTypes []com.VT) []error

SetDataTypes changes the requested data type for one or more items in the collection.

func (*OPCItems) SetDefaultAccessPath

func (is *OPCItems) SetDefaultAccessPath(defaultAccessPath string)

SetDefaultAccessPath sets the default AccessPath that will be used in calls to Add.

func (*OPCItems) SetDefaultActive

func (is *OPCItems) SetDefaultActive(defaultActive bool)

SetDefaultActive sets the default active state for OPCItems created using Items.Add.

func (*OPCItems) SetDefaultRequestedDataType

func (is *OPCItems) SetDefaultRequestedDataType(defaultRequestedDataType com.VT)

SetDefaultRequestedDataType sets the requested data type that will be used in calls to Add.

func (*OPCItems) Validate

func (is *OPCItems) Validate(tags []string, requestedDataTypes *[]com.VT, accessPaths *[]string) ([]error, error)

Validate determines if one or more OPCItems could be successfully created via the Add method (but does not add them).

type OPCServer

type OPCServer struct {
	Name string // Name is the ProgID of the server.
	Node string // Node is the network node name where the server resides.
	// contains filtered or unexported fields
}

OPCServer represents a connection to an OPC DA server.

func Connect

func Connect(progID, node string) (opcServer *OPCServer, err error)

Connect establishes a connection to the OPC server. It returns an OPCServer instance and an error if connection fails.

Example (Error)
// Attempt to connect to a non-existent server
_, err := Connect("NonExistent.ProgID.1", "localhost")
if err != nil {
	fmt.Println("Caught expected connection error")
}
Output:
Caught expected connection error

func (*OPCServer) CreateBrowser

func (s *OPCServer) CreateBrowser() (*OPCBrowser, error)

CreateBrowser creates an OPCBrowser object.

Example (Error)
// Attempt to create a browser from a nil server
var server *OPCServer
_, err := server.CreateBrowser()
if err != nil {
	fmt.Println("Caught expected browser creation error")
}
Output:
Caught expected browser creation error

func (*OPCServer) Disconnect

func (s *OPCServer) Disconnect() error

Disconnect disconnects from the OPC server.

func (*OPCServer) GetBandwidth

func (s *OPCServer) GetBandwidth() (uint32, error)

GetBandwidth returns the bandwidth of the server.

func (*OPCServer) GetBuildNumber

func (s *OPCServer) GetBuildNumber() (uint16, error)

GetBuildNumber returns the build number of the server.

func (*OPCServer) GetClientName

func (s *OPCServer) GetClientName() string

GetClientName returns the client name of the client.

func (*OPCServer) GetCurrentTime

func (s *OPCServer) GetCurrentTime() (time.Time, error)

GetCurrentTime returns the current time from the server.

func (*OPCServer) GetErrorString

func (s *OPCServer) GetErrorString(errorCode int32) (string, error)

GetErrorString converts an error number to a readable string.

func (*OPCServer) GetItemProperties

func (s *OPCServer) GetItemProperties(itemID string, propertyIDs []uint32) (data []interface{}, itemErrors []error, err error)

GetItemProperties returns a list of the current data values for the passed ID codes.

func (*OPCServer) GetLastUpdateTime

func (s *OPCServer) GetLastUpdateTime() (time.Time, error)

GetLastUpdateTime returns the last update time from the server.

func (*OPCServer) GetLocaleID

func (s *OPCServer) GetLocaleID() (uint32, error)

GetLocaleID returns the current locale ID.

func (*OPCServer) GetMajorVersion

func (s *OPCServer) GetMajorVersion() (uint16, error)

GetMajorVersion returns the major part of the server version number.

func (*OPCServer) GetMinorVersion

func (s *OPCServer) GetMinorVersion() (uint16, error)

GetMinorVersion returns the minor part of the server version number.

func (*OPCServer) GetOPCGroups

func (s *OPCServer) GetOPCGroups() *OPCGroups

GetOPCGroups returns the collection of OPCGroup objects.

func (*OPCServer) GetServerName

func (s *OPCServer) GetServerName() string

GetServerName returns the server name of the server that the client connected to via Connect().

func (*OPCServer) GetServerNode

func (s *OPCServer) GetServerNode() string

GetServerNode returns the node name of the server that the client connected to via Connect().

func (*OPCServer) GetServerState

func (s *OPCServer) GetServerState() (com.OPCServerState, error)

GetServerState returns the server's state.

func (*OPCServer) GetStartTime

func (s *OPCServer) GetStartTime() (time.Time, error)

GetStartTime returns the time the server started running.

func (*OPCServer) GetVendorInfo

func (s *OPCServer) GetVendorInfo() (string, error)

GetVendorInfo returns the vendor information string for the server.

func (*OPCServer) LookupItemIDs

func (s *OPCServer) LookupItemIDs(itemID string, propertyIDs []uint32) ([]string, []error, error)

LookupItemIDs returns a list of ItemIDs (if available) for each of the passed ID codes. have not tested because simulator return error

func (*OPCServer) QueryAvailableLocaleIDs

func (s *OPCServer) QueryAvailableLocaleIDs() ([]uint32, error)

QueryAvailableLocaleIDs returns the available LocaleIDs for this server/client session.

func (*OPCServer) QueryAvailableProperties

func (s *OPCServer) QueryAvailableProperties(itemID string) (pPropertyIDs []uint32, ppDescriptions []string, ppvtDataTypes []uint16, err error)

QueryAvailableProperties returns a list of ID codes and Descriptions for the available properties for this ItemID.

func (*OPCServer) RegisterServerShutDown

func (s *OPCServer) RegisterServerShutDown(ch chan string) error

RegisterServerShutDown registers server shut down event.

func (*OPCServer) SetClientName

func (s *OPCServer) SetClientName(clientName string) error

SetClientName sets the client name of the client.

func (*OPCServer) SetLocaleID

func (s *OPCServer) SetLocaleID(localeID uint32) error

SetLocaleID sets the locale ID.

type OPCWrapperError

type OPCWrapperError struct {
	Err  error
	Info string
}

func (*OPCWrapperError) Error

func (e *OPCWrapperError) Error() string

func (*OPCWrapperError) Unwrap

func (e *OPCWrapperError) Unwrap() error

type PropertyDescription

type PropertyDescription struct {
	PropertyID   int32
	Description  string
	DataType     int16
	AccessRights int16
}

PropertyDescription describes an OPC item property.

type ReadCompleteCallBackData

type ReadCompleteCallBackData struct {
	TransID           uint32
	GroupHandle       uint32
	MasterQuality     int32
	MasterErr         error
	ItemClientHandles []uint32
	Values            []interface{}
	Qualities         []uint16
	TimeStamps        []time.Time
	Errors            []error
}

type ServerInfo

type ServerInfo struct {
	ProgID       string        // ProgID is the Program ID of the server.
	ClsStr       string        // ClsStr is the CLSID string representation.
	VerIndProgID string        // VerIndProgID is the Version Independent ProgID.
	ClsID        *windows.GUID // ClsID is the unique Class ID of the server.
}

ServerInfo contains information about an OPC DA server.

func GetOPCServers

func GetOPCServers(node string) ([]*ServerInfo, error)

GetOPCServers enumerates available OPC servers on a node. It employs a fallback strategy: IOPCServerList2 (V2) -> IOPCServerList (V1) -> Registry.

type ShutdownEventReceiver

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

func NewShutdownEventReceiver

func NewShutdownEventReceiver() *ShutdownEventReceiver

func (*ShutdownEventReceiver) AddReceiver

func (er *ShutdownEventReceiver) AddReceiver(ch chan string)

type ShutdownEventReceiverVtbl

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

type WriteCompleteCallBackData

type WriteCompleteCallBackData struct {
	TransID           uint32
	GroupHandle       uint32
	MasterErr         error
	ItemClientHandles []uint32
	Errors            []error
}

Directories

Path Synopsis
example
asyncread command
asyncwrite command
browse command
read command
serverlist command
subscribe command
write command

Jump to

Keyboard shortcuts

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