📄 module1.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", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern void CopyMemory(IntPtr Destination, IntPtr Source, int Length);
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 string Name;
public object Value;
public short Quality;
public FILETIME Ft;
}
public static OPCItem[] ItemArr = new OPCItem[1025];
public static string ReadInIFiles( string Mainkey, string Subkey, string DefaultKey, string FileName)
{
string functionReturnValue = null;
int Success;
byte[] ReadBack=new byte[128];
string rdbk = new string(' ',150);
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();
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;
}
public static System.DateTime FileTimeToDate( FILETIME hFileTime)
{
double ftl;
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;
return System.DateTime.FromOADate(ftl / rMillisecondPerDay - rDayZeroBias);
}
public static int AddItemM(ref string ItemName)
{
int functionReturnValue = 0;
int h;
h = YFOPCSDK.OPC_AddItem(ServerHandle, GroupHandle, ItemName);
if (h > 0)
{
ItemArr[h].Name = ItemName;
functionReturnValue = h;
Program.mainWindow.lstProcess.Items.Add("ItemArr(" + h + ").Handle=" + h + ",Name=" + ItemName);
}
return h;
}
public static bool RemoveItem( int Index)
{
return (YFOPCSDK.OPC_RemoveItem(ServerHandle, GroupHandle, Index) != 0);
}
public static void ServerDataChangeProc(int ServerHandle, int GroupHandle, int ItemHandle, object Value, double Ft, short Quality)
{
Program.mainWindow.sbStatusBar.Items[1].Text = " ItemHandle=" + ItemHandle.ToString();
if (ItemHandle > 0)
{
ItemArr[ItemHandle].Ft = DoubleToFileTime(Ft);
ItemArr[ItemHandle].Value = Value;
ItemArr[ItemHandle].Quality = Quality;
Program.mainWindow.RefreshItem(ref ItemHandle);
}
else
{
Program.mainWindow.lstProcess.Items.Add(ItemHandle + " " + Value);
}
}
public static void ServerDataChangeProcEx(int ServerHandle, int GroupHandle,ref object ItemHandle, ref object Value, ref object Ft, ref object Quality)
{
int Index;
// Program.mainWindow.sbStatusBar.Items[1].Text = " ItemHandle=" + ItemHandle.ToString();
object[] tmp1 = (object[])ItemHandle;
object[] tmp2 = (object[])Value;
object[] tmp3 = (object[])Ft;
object[] tmp4 = (object[])Quality;
for (int i = 0; i < tmp1.Length; i++)
{
Index =(int)tmp1[i];
if (Index > 0)
{
ItemArr[Index].Ft = DoubleToFileTime((double )tmp3[i]);
ItemArr[Index].Value = tmp2[i];
ItemArr[Index].Quality = (short )tmp4[i];
Program.mainWindow.RefreshItem(ref Index);
}
else
{
// Program.mainWindow.lstProcess.Items.Add(ItemHandle + " " + Value);
}
}
}
public static void ServerShutdownProc(int ServerHandle)
{
Program.mainWindow.Disconnect();
}
public static void AddLandHostIP(string Host, string 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 + -