⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module1.cs

📁 OPC-Client sdk for vb/vb.net/c#
💻 CS
字号:
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
static class Program
{
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    /// 
    public static frmMain mainWindow;
    public static frmServerBrowser sbWindow;
    [STAThread]
    static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        mainWindow = new frmMain();
        Application.Run(mainWindow);
    }
}
class Module1
{

	private const double rDayZeroBias = 109205.0;
	private const double rMillisecondPerDay = 10000000.0 * 60.0 * 60.0 * 24.0 / 10000.0;
	[StructLayout(LayoutKind.Sequential)]
	public struct FILETIME
	{
		[MarshalAs(UnmanagedType.I4, SizeConst = 4)]
		public int dwLowDateTime;
		[MarshalAs(UnmanagedType.I4, SizeConst = 4)]
		public int dwHighDateTime;
	}
	[DllImport("kernel32", EntryPoint = "WritePrivateProfileStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
	[DllImport("kernel32", EntryPoint = "GetPrivateProfileStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, byte[] lpReturnedString, int nSize, string lpFileName);
	[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int FileTimeToLocalFileTime(ref FILETIME lpFileTime, ref FILETIME lpLocalFileTime);
	[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern int LocalFileTimeToFileTime(ref string lpLocalFileTime, ref string lpFileTime);
	[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern void CopyMemory(IntPtr Destination, IntPtr Source, int Length);
	//  Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef hpvDest As String, ByRef hpvSource As String, ByVal cbCopy As Integer)
	public static int ServerHandle;
    public static int GroupHandle;
    public static string GroupName;

	public struct ServerItem
	{
		public string ServerName;
		public string ServerClassID;
	}

    public static ServerItem[] ServerItems = new ServerItem[100];

	public struct OPCItem
	{
		public int Handle;
		public string Name;
		public object Value;
		public short Quality;
		public FILETIME Ft;
		public int Index;
	}

    public static OPCItem[] ItemArr = new OPCItem[1025];
    public static short ItemIndex;
	public static  string ReadInIFiles( string Mainkey,  string Subkey,  string DefaultKey,  string FileName)
	{
		string functionReturnValue = null;
		int Success;
		byte[] ReadBack=new byte[128];
		Success = GetPrivateProfileString(Mainkey, Subkey, DefaultKey, ReadBack, 150, FileName);
        ReadBack[Success] = 0;

        if (Success == 0)
        {
            functionReturnValue = DefaultKey;
        }
        else
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            functionReturnValue = encoding.GetString(ReadBack);
        }
		return functionReturnValue;
	}

    public static FILETIME DoubleToFileTime(double Value)
	{
		FILETIME ftdt=new FILETIME();
		//UPGRADE_WARNING: 脦麓脛脺陆芒脦枚露脭脧贸 ftdt 碌脛脛卢脠脧脢么脨脭隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"隆卤
		IntPtr source;
		IntPtr dest;
		source = Marshal.AllocHGlobal(Marshal.SizeOf(Value));
		Marshal.StructureToPtr(Value, source, true);
		dest = Marshal.AllocHGlobal(Marshal.SizeOf(ftdt));
		//禄貌脮脽卤冒碌脛路陆脢陆禄帽碌脙碌脛路脟脥脨鹿脺脛脷麓忙碌脴脰路
		CopyMemory(dest, source, Marshal.SizeOf(Value));
        ftdt = (FILETIME)Marshal.PtrToStructure(dest, typeof(FILETIME));
		return ftdt;
		//UPGRADE_WARNING: 脦麓脛脺陆芒脦枚露脭脧贸 DoubleToFileTime 碌脛脛卢脠脧脢么脨脭隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"隆卤
	}

	public static  System.DateTime FileTimeToDate(FILETIME hFileTime)
	{
		double  ftl;
		//脭脷VB6脧脗脫脙currency
		long tm=0;
		FILETIME Ft=new FILETIME();
		FileTimeToLocalFileTime(ref hFileTime, ref Ft);
		IntPtr source;
		IntPtr dest;
		source = Marshal.AllocHGlobal(Marshal.SizeOf(Ft));
		Marshal.StructureToPtr(Ft, source, true);
		dest = Marshal.AllocHGlobal(Marshal.SizeOf(tm));
		//禄貌脮脽卤冒碌脛路陆脢陆禄帽碌脙碌脛路脟脥脨鹿脺脛脷麓忙碌脴脰路
		CopyMemory(dest, source, Marshal.SizeOf(Ft));
		tm =(long) Marshal.PtrToStructure(dest, tm.GetType());
		ftl = tm;
		ftl = ftl / 10000;
		//赂茫虏禄脟氓脦陋脢虏脙麓脪陋鲁媒10000,潞娄脦脪碌梅录赂赂枚脨隆脢卤
		return System.DateTime.FromOADate(ftl / rMillisecondPerDay - rDayZeroBias);
	}

	public static short Finditem( int ItemHandle)
	{
		short functionReturnValue = 0;
		short I;
		for (I = 1; I <= ItemIndex; I++) {
			if (ItemArr[I].Handle == ItemHandle)
			{
				functionReturnValue = (short)ItemArr[I].Index;
				break; // TODO: might not be correct. Was : Exit For
			}
		}
		return functionReturnValue;
	}

	public static short AddItemM(ref string ItemName)
	{
		short functionReturnValue = 0;
		int h;
		if (ItemIndex > 1023) return 0; // TODO: might not be correct. Was : Exit Function
 
		//UPGRADE_WARNING: 脦麓脛脺陆芒脦枚露脭脧贸 ServerHandle 碌脛脛卢脠脧脢么脨脭隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"隆卤
		h = YFOPCSDK.OPC_AddItem(Module1.ServerHandle, GroupHandle, ItemName);
		if (h > 0)
		{
			//        Debug.Print h
			ItemIndex++;// = ItemIndex + 1;
			ItemArr[ItemIndex].Handle = h;
			ItemArr[ItemIndex].Name = ItemName;
			ItemArr[ItemIndex].Index = ItemIndex;
			functionReturnValue = ItemIndex;
			Program.mainWindow.lstProcess.Items.Add("ItemArr(" + ItemIndex + ").Handle=" + h + ",Name=" + ItemName);
		}
		return functionReturnValue;
	}

	public static  bool RemoveItem( int Index)
	{
		bool functionReturnValue = false;
		if (Index > 0 & Index < 1025)
		{
			//UPGRADE_WARNING: 脦麓脛脺陆芒脦枚露脭脧贸 ServerHandle 碌脛脛卢脠脧脢么脨脭隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"隆卤
			if (YFOPCSDK.OPC_RemoveItem(ServerHandle, GroupHandle, ItemArr[Index].Handle)!=0)
			{
				ItemArr[Index].Handle = 0;
				functionReturnValue = true;
			}
		}
		return functionReturnValue;
	}

    public static void ServerDataChangeProc(int ServerHandle, int GroupHandle, int ItemHandle, object Value, double Ft, short Quality)
	{
		 // ERROR: Not supported in C#: OnErrorStatement

		short Index;
		//UPGRADE_WARNING: 录炉潞脧 frmMain.sbStatusBar.Panels 碌脛脧脗脧脼脪脩脫脡 1 赂眉赂脛脦陋 0隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"隆卤
		Program.mainWindow.sbStatusBar.Items[1].Text =  " ItemHandle=" + ItemHandle.ToString();
		Index = Finditem(ItemHandle);

		if (Index > 0)
		{
			//UPGRADE_WARNING: 脦麓脛脺陆芒脦枚露脭脧贸 ItemArr().Ft 碌脛脛卢脠脧脢么脨脭隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"隆卤
			ItemArr[Index].Ft = DoubleToFileTime(Ft);
			//UPGRADE_WARNING: 脦麓脛脺陆芒脦枚露脭脧贸 Value 碌脛脛卢脠脧脢么脨脭隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"隆卤
			ItemArr[Index].Value = Value;
			ItemArr[Index].Quality = Quality;
            //Debug.Print(Index);
			Program.mainWindow.RefreshItem(ref Index);
		}
		else
		{
			//UPGRADE_WARNING: 脦麓脛脺陆芒脦枚露脭脧贸 Value 碌脛脛卢脠脧脢么脨脭隆拢 碌楼禄梅脪脭禄帽碌脙赂眉露脿脨脜脧垄:隆掳ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"隆卤
			Program.mainWindow.lstProcess.Items.Add(ItemHandle + " " + Value);
		}
	}

    public static void ServerShutdownProc(int ServerHandle)
	{
		Program.mainWindow.Disconnect();

	}
	//  dim Host(20) As Byte,ip(20) As Byte<VBFixedString(301)>
    public static void AddLandHostIP(StringBuilder Host, StringBuilder ip)
	{
        Program.sbWindow.cbHost.Items.Add(Host.ToString());
	}
    public static void AddOPCname(string Name, string clsid)
	{
		{
            Program.sbWindow.listServer.Items.Add(Name);
            ServerItems[Program.sbWindow.gServerID].ServerName = Name;
            ServerItems[Program.sbWindow.gServerID].ServerClassID = clsid;
            Program.sbWindow.gServerID = Program.sbWindow.gServerID + 1;
		}
	}

    public static void AddProcess(StringBuilder ev)
	{
		Program.mainWindow.lstProcess.Items.Add(ev);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -