📄 koc_module.bas
字号:
Attribute VB_Name = "KOC_Module"
'/////////////////////////////////////////////////////////////////////////////
'//
'// Knight OPC Client Rapid Development Toolkit -- KOCRDK
'//
'// Module File for Visual Basic 6.0
'/////////////////////////////////////////////////////////////////////////////
'//
'// Author: Knight Fox
'// Initial Date: 11/18/2001
'// Workfile: KOC_Module.bas
'// Revision: 1.5
'// Date: Oct 12, 2002 3:25p
'// Target System: Microsoft Windows NT 4.0 / 95 /98 / 2000 / XP
'// Environment: Visual Basic 6.0 / OPC DataAccess 1.0/2.0
'// Remarks:
'// Product Update: http://www.eehoo.net
'// Contact: opc@eehoo.net
'//
'/////////////////////////////////////////////////////////////////////////////
'// Copyright (C) 2002, Knight Workgroup, eehoo Technologies, Inc.
'/////////////////////////////////////////////////////////////////////////////
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// Initialization and System functions
'//
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// KOC_Init()
'//
'// This function initializes KOCRDK.dll
'//
Declare Function KOC_Init Lib "kocrdk.dll" () As Long
'//
'// KOC_Uninit()
'//
'// This function uninitializes KOCRDK.dll
'//
Declare Sub KOC_Uninit Lib "kocrdk.dll" ()
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// OPC Server functions
'//
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// KOC_GetServerCount
'//
'// returns the number of available local or remote OPC servers
'//
'// Param: BOOL bVer2 -- TRUE , find OPC 2.0 servers
'// -- FALSE, find OPC 1.0 servers
'//
'KOCRDK_API int WINAPI KOC_GetServerCount(LPCSTR MachineName,BOOL bVer2 = true);
Declare Function KOC_GetServerCount Lib "kocrdk.dll" (ByRef MachineName As Byte, ByVal Ver2 As Long) As Long
'
'//
'// KOC_GetServerName
'//
'// Used to iterate through the server list obtained with KOC_GetServerCount()
'// User Buffer pointed to by pBuf is filled with the Server name at index of the Server List
'// A returned value of FALSE indicates that the index is invalid.
'//
'KOCRDK_API BOOL WINAPI KOC_GetServerName(int index, char *pBuf, int BufSize);
Declare Function KOC_GetServerName Lib "kocrdk.dll" (ByVal index As Integer, ByRef buf As Byte, ByVal BufSize As Integer) As Long
'
'//
'// KOC_Connect
'//
'// Establishes an OPC Connection with the specified server
'// INVALID_HANDLE_VALUE (-1) id returned if the connection cannot be established.
'//
'// Param: BOOL bVer2 -- TRUE , using OPC 2.0 interface
'// -- FALSE, using OPC 1.0 interface
'//
'KOCRDK_API HANDLE WINAPI KOC_Connect(LPCSTR MachineName, LPCSTR ServerName, BOOL bVer2 = true);
Declare Function KOC_Connect Lib "kocrdk.dll" (ByRef MachineName As Byte, ByRef ServerName As Byte, ByVal Ver2 As Long) As Long
'//
'// KOC_Disconnect
'//
'// Used to shutdown an OPC Connection
'//
'KOCRDK_API void WINAPI KOC_Disconnect(HANDLE hConnect);
Declare Sub KOC_Disconnect Lib "kocrdk.dll" (ByVal hConnect As Long)
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// OPC Group functions
'//
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// KOC_AddGroup
'//
'// Creates a new OPC Group for the defined connection
'// Requested name, data rate and deadband etc. specified in parameter list.
'//
'KOCRDK_API HANDLE WINAPI KOC_AddGroup (
' HANDLE hConnect,
' LPCSTR Name,
' BOOL *bActive,
' DWORD *pRate,
' LONG *pTimeBias,
' float *pDeadBand,
' DWORD *dwLCID);
'
Declare Function KOC_AddGroup Lib "kocrdk.dll" (ByVal hConnect As Long, ByRef Name As Byte, ByRef bActive As Boolean, ByRef Rate As Long, ByRef TimeBias As Long, ByRef DeadBand As Single, ByRef LCID As Long) As Long
'//
'// KOC_RemoveGroup
'//
'// Removes and cleansup allocated resources for defined group
'//
'KOCRDK_API void WINAPI KOC_RemoveGroup(HANDLE hConnect, HANDLE hGroup);
Declare Sub KOC_RemoveGroup Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal hGroup As Long)
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// OPC Item functions
'//
'///////////////////////////////////////////////////////////////////////////////////////////
'
'//
'// KOC_GetItemCount
'//
'// Returns the number of OPC Items from the Browse Interface of the designated
'// Server connection. If the server does not support Browsing, a value of xero
'// is returned. This function fills an internal array of itemnames which may
'// then be accessed via KOC_GetItemName().
'//
'// This function is equivalent to calling BrowseItems using OPC_FLAT from
'// the Root position.
'//
'KOCRDK_API int WINAPI KOC_GetItemCount(HANDLE hConnect);
Declare Function KOC_GetItemCount Lib "kocrdk.dll" (ByVal hConnect As Long) As Integer
'
'//
'// KOC_GetItemName
'//
'// Allows user to iterate through the list of item names obtained from
'// KOC_GetItemCount().
'//
'KOCRDK_API BOOL WINAPI KOC_GetItemName(HANDLE hConnect, int index, char *pBuf, int BufSize);
Declare Function KOC_GetItemName Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal index As Integer, ByRef buf As Byte, ByVal BufSize As Integer) As Long
'
'//
'// KOC_AddItem
'//
'// Requests that the connected OPC Server add an item to the specified group.
'// The return value identifies the item for future access by the user application.
'// An INVALID_HANDLE_VALUE return, indicates that the requested item name does not
'// exist in the connected Server.
'//
'KOCRDK_API HANDLE WINAPI KOC_AddItem(HANDLE hConnect, HANDLE hGroup, LPCSTR ItemName);
Declare Function KOC_AddItem Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal hGroup As Long, ByRef ItemName As Byte) As Long
'
'//
'// KOC_RemoveItem
'//
'// Removes the specified OPC Item and cleans up resources
'//
'KOCRDK_API void WINAPI KOC_RemoveItem(HANDLE hConnect, HANDLE hGroup, HANDLE hItem);
Declare Sub KOC_RemoveItem Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal hGroup As Long, ByVal hItem As Long)
'
'
'//
'// KOC_WriteItem
'//
'// Allows the controlling application to write to a defined OPC item
'//
'//
'KOCRDK_API BOOL WINAPI KOC_WriteItem(HANDLE hConnect, HANDLE hGroup, HANDLE hItem, VARIANT *pVar, BOOL DoAsync);
Declare Function KOC_WriteItem Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal hGroup As Long, ByVal hItem As Long, ByRef Value As Variant, ByVal DoAsync As Boolean) As Long
'
'
'//
'// KOC_ReadItem
'//
'// Uses the SyncIO Interface to read an opc item directly from the Server.
'//
'KOCRDK_API BOOL WINAPI KOC_ReadItem(HANDLE hConnect, HANDLE hGroup, HANDLE hItem, VARIANT *pVar, FILETIME *pTimeStamp, DWORD *pQuality);
Declare Function KOC_ReadItem Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal hGroup As Long, ByVal hItem As Long, ByRef Value As Variant, ByRef TimeStamp As FILETIME, ByRef Quality As Long) As Long
'
'///////////////////////////////////////////////////////////////////////////////////////////
'//
'// CallBack Functions
'//
'//////////////////////////////////////////////////////////////////////////////////////////
'
'/////////////////////////////////////
'// KOC_SetDataChangeProc
'//
'// Establishes a callback function in the user application which will receive
'// control when the value of an item is updated from the connected server.
'//
'// prototype for the callback function is as follows:
'// void CALLBACK EXPORT DataChangeProc(HANDLE hGroup, HANDLE hItem, VARIANT *pVar, FILETIME timestamp, DWORD quality)
'//
'// Param: HANDLE hConnect -- HANDLE of connected opc server
'// DATACHANGEPROC lpCallback -- pointer of callback function in user application
'// Return: BOOL -- TRUE if succeded, FALSE if failed
'KOCRDK_API BOOL WINAPI KOC_SetDataChangeProc(HANDLE hConnect, DATACHANGEPROC lpCallback);
Declare Function KOC_SetDataChangeProc Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal callback As Long) As Long
'//
'// KOC_SetShutdownProc
'//
'// Establishes a callback to the application if the connected
'// server requests a disconnect;
'//
'// Param: HANDLE hConnect -- HANDLE of connected opc server
'// SHUTDOWNPROC lpCallback -- pointer of callback function in user application
'// Return: BOOL -- TRUE if succeded, FALSE if failed
'KOCRDK_API BOOL WINAPI KOC_SetShutdownProc(HANDLE hConnect, SHUTDOWNPROC lpCallback);
Declare Function KOC_SetShutdownProc Lib "kocrdk.dll" (ByVal hConnect As Long, ByVal callback As Long) As Long
'Time convert functions
Private Const rDayZeroBias As Double = 109205#
Private Const rMillisecondPerDay As Double = 10000000# * 60# * 60# * 24# / 10000#
Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As Any, lpFileTime As Any) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Public Function Win32FileTimeToVB(hFileTime As FILETIME) As Date
Dim ftl As Currency, Ft As FILETIME
FileTimeToLocalFileTime hFileTime, Ft
CopyMemory ftl, Ft, Len(Ft)
Win32FileTimeToVB = CDate((ftl / rMillisecondPerDay) - rDayZeroBias)
End Function
Public Function VBToWin32FileTime(hDate As Date) As FILETIME
Dim hft As Currency
hft = (CCur(hDate) + rDayZeroBias) * rMillisecondPerDay
LocalFileTimeToFileTime hft, VBToWin32FileTime
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -