opcmodule.cs
来自「OPCserver OPCserver.rar」· CS 代码 · 共 411 行
CS
411 行
#define Win32
using Microsoft.VisualBasic;
//using AxComctlLib;
using System.Data;
using Microsoft.VisualBasic.Compatibility;
using System;
using System.Collections;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
namespace CNETServer
{
sealed class OPCModule
{
//register
//用GUIDGEN工具生成GUID的字符串
//{EBE61984-73F5-4b80-814B-4B7421813C28}
public const string lpCLSID = "{EBE61984-73F5-4b80-814B-4B7421813C28}";
public const string lpOPCProgID = "TLSvrRDK.OPCTOOLKIT.2007.C# NET 2003";
public const string lpOPCDescr = "Tuo Lin RDK by C# 2003";
public static string debugstring;
public delegate void DelegateDisconnectProc(int dwNum);
public static DelegateDisconnectProc lpDisconnectProc;
public delegate void DelegateDeviceReadProc(int hTagHandle, ref object lpValue, ref short lpQuality, ref FILETIME lpTimestamp);
public static DelegateDeviceReadProc lpDelegateDeviceReadProc;
public delegate void DelegateWriteNotifyCallback(int hTagHandle, ref object pNewValue, ref int pDeviceError);
public static DelegateWriteNotifyCallback lpDelegateWriteNotifyCallback;
public struct Tag
{
public string m_sTagName; ////tagname
public string m_sDescr; ////Description
public int m_hHWND; ////Handle
public VariantType m_vType; ////type
public short m_lQuality; ////Quality
public opcdefine.FILETIME m_ft; // //timestamp
public object m_vValue; // //value
}
public const short MaxCounts = 1000;
public static Tag[] TagList = new Tag[MaxCounts + 1];
//UPGRADE_WARNING: 结构 FILETIME 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
[DllImport("kernel32", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern void GetSystemTimeAsFileTime( opcdefine.FILETIME lpSystemTimeAsFileTime);
//UPGRADE_WARNING: 结构 SYSTEMTIME 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
[DllImport("kernel32", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern void GetLocalTime(ref opcdefine.SYSTEMTIME lpSystemTime);
//UPGRADE_WARNING: 结构 SYSTEMTIME 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
//UPGRADE_WARNING: 结构 FILETIME 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
[DllImport("kernel32", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern int FileTimeToSystemTime(ref opcdefine.FILETIME lpFileTime, ref opcdefine.SYSTEMTIME lpSystemTime);
//UPGRADE_WARNING: 结构 FILETIME 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
//UPGRADE_WARNING: 结构 SYSTEMTIME 可能要求封送处理属性作为此声明语句中的参数传递。 单击以获得更多信息:“ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"”
[DllImport("kernel32", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern int SystemTimeToFileTime(ref opcdefine.SYSTEMTIME lpSystemTime, ref opcdefine.FILETIME lpFileTime);
////CALL BACK
////该函数注册客户端断开回调函数,当有客户端从设备断开时,调用
////EnableDisconnectNotification注册的函数
[DllImport("TLSvrRdk.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern int TL_EnableDisconnectNotification( DelegateDisconnectProc lpCallback);
////注册该函数后,当有客户端从服务器断开,通过注册的函数通知
////用户。当dwNum为零时,服务器程序可以退出
//void CALLBACK EXPORT DisconnectProc Lib "TLSvrRdk.dll" (DWORD dwNum)
////该函数用来注册读设备回调函数
[DllImport("TLSvrRdk.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern int TL_EnableDeviceRead( DelegateDeviceReadProc lpCallback);
////如果不注册该函数,服务器将把所有的读设备请求转换为读缓存
////请求。注册的DEVICEREADPROC函数应该支持多线程。
//void CALLBACK EXPORT DeviceReadProc Lib "TLSvrRdk.dll" (HANDLE hTagHandle, PVARIANT lpValue,
// PWORD lpQuality, PFILETIME lpTimeStamp)
////该函数用来注册写回调函数。如果添加了可写标签,则应该注册写回调函数。
[DllImport("TLSvrRdk.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public static extern int TL_EnableWriteNotification( DelegateWriteNotifyCallback lpCallback);
////如果不注册该函数,OPC服务器将对所有的客户端写请求返回
////错误。OPC 运行库保证向WRITENOTIFYPROC传递的新值能
////够转换为用户指定的标准数据类型,如果用户指定了一个模拟量
////标签的上下限(详见OPC DA 2.04规范4.4.6),OPC 运行库保
////证向WRITENOTIFYPROC传递的新值在上下限范围之内。注册
////的WRITENOTIFYPROC函数应该支持多线程。
//void CALLBACK EXPORT WriteNotifyCallback Lib "TLSvrRdk.dll" (HANDLE hTagHandle, PVARIANT lpNewValue,
//PDWORD lpDeviceError)
public static void DeviceReadProc(int hTagHandle,ref object lpValue,ref short lpQuality,ref FILETIME lpTimestamp)
{
//用于把客户端写入的数据通知Server
OPCModule.debugstring=Strings.Format (hTagHandle, "0000");
Console.Write (OPCModule.debugstring);
FrmMain.DefInstance.txtDebug.Text+="hTagHandle:";
FrmMain.DefInstance.txtDebug.Text+=OPCModule.debugstring;
OPCModule.debugstring=Strings.Format (lpValue, "0000000");
Console.Write (OPCModule.debugstring);
FrmMain.DefInstance.txtDebug.Text+="lpValue:";
FrmMain.DefInstance.txtDebug.Text+=OPCModule.debugstring;
}
public static void DisconnectProc(int dwNum)
{
if (dwNum == 1234)
{
OPCModule.debugstring=Strings.Format (dwNum, "0000");
Console.Write (OPCModule.debugstring);
//FrmMain.DefInstance.txtDebug.Text+=OPCModule.debugstring;
}
else
{
if (dwNum != 0)
{
OPCModule.debugstring=Strings.Format (dwNum, "0000");
Console.Write (OPCModule.debugstring);
FrmMain.DefInstance.txtDebug.Text="dwNum";
FrmMain.DefInstance.txtDebug.Text+=OPCModule.debugstring;
}
}
if (FrmMain.DefInstance.txtDebug.Text.Length >2048)
FrmMain.DefInstance.txtDebug.Text="";
}
public static void WriteNotifyCallback(int hTagHandle,ref object pNewValue,ref int pDeviceError)
{
//用于把客户端写入的数据通知Server
OPCModule.debugstring=Strings.Format (hTagHandle, "0000");
Console.Write (OPCModule.debugstring);
FrmMain.DefInstance.txtDebug.Text+="hTagHandle:";
FrmMain.DefInstance.txtDebug.Text+=OPCModule.debugstring;
OPCModule.debugstring=Strings.Format (pNewValue, "0000000");
Console.Write (OPCModule.debugstring);
FrmMain.DefInstance.txtDebug.Text+="pNewValue:";
FrmMain.DefInstance.txtDebug.Text+=OPCModule.debugstring;
}
static public string GetAppPath()
{
string returnValue;
string sAppPath;
sAppPath = Microsoft.VisualBasic.Compatibility.VB6.Support.GetPath();
if (sAppPath.Substring(sAppPath.Length - 1, 1) != "\\")
{
sAppPath = sAppPath + "\\";
}
returnValue = sAppPath;
return returnValue;
}
static public string GetAppExeName()
{
string returnValue;
string sAppExeName;
sAppExeName = Microsoft.VisualBasic.Compatibility.VB6.Support.GetEXEName();
sAppExeName = sAppExeName + ".exe";
returnValue = sAppExeName;
return returnValue;
}
static public bool OnRegister()
{
bool returnValue;
////注册OPC服务器
bool bResult;
if (opcdefine.TL_RegistryS(lpCLSID, lpOPCProgID, lpOPCDescr, GetAppPath() + GetAppExeName()) > 0)
{
bResult = true;
//System.Windows.Forms.MessageBox(
string message = "注册成功!";
string caption = "OPC Server Demo";
MessageBoxButtons buttons = MessageBoxButtons.OK ;
// Displays the MessageBox.
DialogResult result;
result = MessageBox.Show( message, caption, buttons);
}
else
{
bResult = false;
string message = "注册失败!";
string caption = "OPC Server Demo";
MessageBoxButtons buttons = MessageBoxButtons.OK ;
// Displays the MessageBox.
DialogResult result;
result = MessageBox.Show( message, caption, buttons);
}
returnValue = bResult;
return returnValue;
}
static public bool OnUnRegister()
{
bool returnValue;
////反注册OPC服务器
bool bResult;
if (opcdefine.TL_UnregisterS(lpCLSID, lpOPCProgID) > 0)
{
bResult = true;
string message = "反注册成功!";
string caption = "OPC Server Demo";
MessageBoxButtons buttons = MessageBoxButtons.OK ;
// Displays the MessageBox.
DialogResult result;
result = MessageBox.Show( message, caption, buttons);
}
else
{
bResult = false;
string message = "反注册失败!";
string caption = "OPC Server Demo";
MessageBoxButtons buttons = MessageBoxButtons.OK ;
// Displays the MessageBox.
DialogResult result;
result = MessageBox.Show( message, caption, buttons);
}
returnValue = bResult;
return returnValue;
}
static public bool OnInitOpcServer()
{
bool returnValue;
// //初始化OPC Server
bool bResult;
bResult = opcdefine.TL_SetupRegCode("TUOLIN20030713NOTAGCOUNTS");
bResult = opcdefine.TL_InitOpcServerS(lpCLSID, 500);
////设定服务器的分隔符
byte cQualifier;
cQualifier=(byte)Strings.Asc (".");
opcdefine.TL_SetQualifier(cQualifier);
returnValue = bResult;
return returnValue;
}
static public void OnUnInitOpcServer()
{
////结束OPC Server
opcdefine.TL_UnInitOpcServer();
}
static public void OnAddItem()
{
//建立点数组
short i;
for (i = 1; i <= MaxCounts; i++)
{
TagList[i].m_sTagName = "TAG" +Strings.Format (i, "0000");
TagList[i].m_sDescr = "m_sDescr tag" + Strings.Format(i, "0000");
TagList[i].m_hHWND = - 1;
TagList[i].m_lQuality = opcdefine.OPC_QUALITY_BAD;
//GetSystemTimeAsFileTime( TagList[i].m_ft);
switch (i % 4)
{
case 0:
TagList[i].m_vType = VariantType.Boolean;
TagList[i].m_vValue = false;
break;
case 1:
TagList[i].m_vType = VariantType.Short;
TagList[i].m_vValue = 0;
break;
case 2:
TagList[i].m_vType = VariantType.Single;
TagList[i].m_vValue = 0;
break;
case 3:
TagList[i].m_vType = VariantType.String;
TagList[i].m_vValue = "vbString";
break;
default:
break;
}
}
//加入opc server
for (i = 1; i <= MaxCounts; i++)
{
TagList[i].m_hHWND = opcdefine.TL_CreateTag(TagList[i].m_sTagName, TagList[i].m_vValue, TagList[i].m_lQuality, true);
////pTagName->m_sDescr
//// 101 详见DA 2.04 中的 41页 4.4.6 节
opcdefine.TL_SetTagProperties(TagList[i].m_hHWND, 101, "Item Description", TagList[i].m_sDescr);
}
string message = "additem end!";
string caption = "OPC Server Demo";
MessageBoxButtons buttons = MessageBoxButtons.OK ;
// Displays the MessageBox.
DialogResult result;
result = MessageBox.Show( message, caption, buttons);
}
static public void OnDeleteItem()
{
short i;
//删除所有点opc server
for (i = 1; i <= MaxCounts; i++)
{
opcdefine.TL_RemoveTag(TagList[i].m_hHWND);
}
string message = "delete all end!";
string caption = "OPC Server Demo";
MessageBoxButtons buttons = MessageBoxButtons.OK ;
// Displays the MessageBox.
DialogResult result;
result = MessageBox.Show( message, caption, buttons);
}
static public void OnUpdateData()
{
short i;
string sStr;
//修改所有点opc server
for (i = 1; i <= MaxCounts; i++)
{
sStr = Strings.Space(1024);
sStr = "Name :" + TagList[i].m_sTagName + " ,Quality:= " + Conversion.Str(TagList[i].m_lQuality) + ", Value =" + TagList[i].m_vValue;
if (i == 1)
{
FrmMain.DefInstance.Text1.Text = "名称 , 通讯质量, 值";
}
if (i < 100)
{
FrmMain.DefInstance.Text1.Text = FrmMain.DefInstance.Text1.Text + '\r' + '\n' + sStr;
}
opcdefine.TL_UpdateTag(TagList[i].m_hHWND, TagList[i].m_vValue, TagList[i].m_lQuality);
}
}
static public void OnRandomData()
{
//产生随机数据
short i;
float iRand;
for (i = 1; i <= MaxCounts; i++)
{
iRand = VBMath.Rnd(1) * 1000;
//If (iRand > 400) Then
// TagList(i).m_lQuality = OPC_QUALITY_UNCERTAIN
// If (iRand > 800) Then
// TagList(i).m_lQuality = OPC_QUALITY_GOOD
// End If
// Else
// TagList(i).m_lQuality = OPC_QUALITY_BAD
//End If
TagList[i].m_lQuality = opcdefine.OPC_QUALITY_GOOD;
switch (i % 4)
{
case 0:
if (iRand > 500)
{
TagList[i].m_vValue = false;
}
else
{
TagList[i].m_vValue = true;
}
break;
case 1:
TagList[i].m_vValue = System.Convert.ToInt16(iRand);
break;
case 2:
TagList[i].m_vValue = iRand;
break;
case 3:
TagList[i].m_vValue = Strings.Format(iRand, "###0.00");
break;
default:
break;
}
}
System.Windows.Forms.Application.DoEvents();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?