SSPC-Tester/Driver/急停报警相关资料/PCI2312 Samples/C#/Simple_64/INT/Program.cs

71 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace Sys
{
class Program
{
[DllImport("msvcrt.dll")]
public static extern int _getch();
[DllImport("msvcrt.dll")]
public static extern int _kbhit();
[DllImport("Kernel32.dll")]
public static extern int WaitForSingleObject(IntPtr hHandle, int dwMillisenconds);
static void Main(string[] args)
{
IntPtr hDevice; // device object handle
IntPtr hEventInt; // Interruption handle
Int32 DeviceID; // device ID
int inIntCount = 0; // 内部中断计数
int outIntCount = 0; // 外部中断计数
DeviceID = 0;
hDevice = PCI2312.PCI2312_CreateDevice(DeviceID); // Create Device object
if (hDevice == (IntPtr)(-1))
{
Console.WriteLine("CreateDevice Error...");
_getch();
return;
}
hEventInt = PCI2312.PCI2312_CreateSystemEvent();
if (hEventInt == (IntPtr)(-1))
{
Console.WriteLine("CreateSystemEvent Error...");
_getch();
goto Exit;
}
PCI2312.PCI2312_InitDeviceInt(hDevice, hEventInt);
Console.WriteLine(" wait...\n ");
while (_kbhit() == 0)
{
while (true)
{
if (WaitForSingleObject(hEventInt, 100) == 0) // 等待中断发生
break;
if (_kbhit() != 0) break;
}
outIntCount++; // 外部中断计数
inIntCount = PCI2312.PCI2312_GetDeviceIntCount(hDevice); // 获得内部中断计数
Console.WriteLine("outIntCount={0}, inIntCount={1}", outIntCount, inIntCount);
}
Exit:
PCI2312.PCI2312_ReleaseDeviceInt(hDevice); // 释放中断资源
PCI2312.PCI2312_ReleaseDevice(hDevice); // 释放设备对象
return;
}
}
}