原来的版本是c++的,我不太会,修改成了c#的,能用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace DeviceApplication2
{
public partial class Form1 : Form
{
//DeviceIoControl在C#中的引用和定义
[DllImport("Coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern int DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
uint[] lpInBuffer,
int nInBufferSize,
byte[] lpOutbuffer,
int nOutBufferSize,
ref int lpByteReturned,
IntPtr lpOverlapped
);
[DllImport("Coredll.dll", EntryPoint = "CreateFile", CharSet = CharSet.Unicode)]
private static extern IntPtr CreateFile(
string lpFileName,
uint dwDesireAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("Coredll.dll", EntryPoint = "CloseHandle",SetLastError = true)]
static extern int CloseHandle(IntPtr hObject);
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const int OPEN_EXISTING = 3;
private const int FILE_FLAG_RANDOM_ACCESS = 0x10000000;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr hLed = new IntPtr();
int bytesReturned = 0;
//HANDLE hLed;
hLed = CreateFile("LED1:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hLed == (IntPtr)(-1))
{
//暂时不处理
//Console.WriteLine("连接GPIO设备失败");
}
else
{
uint[] data = new uint[1];
data[0] = 0x04001060;
DeviceIoControl(hLed, 0x04001060, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
for (int j = 0; j < 60000; j++)
{
for (int k = 0; k < 200; k++)
{ }
}
data[0] = 0x04001061;
DeviceIoControl(hLed, 0x04001061, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
for (int j = 0; j < 60000; j++)
{
for (int k = 0; k < 200; k++)
{ }
}
data[0] = 0x04001070;
DeviceIoControl(hLed, 0x04001070, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
for (int j = 0; j < 60000; j++)
{
for (int k = 0; k < 200; k++)
{ }
}
data[0] = 0x04001071;
DeviceIoControl(hLed, 0x04001071, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
for (int j = 0; j < 60000; j++)
{
for (int k = 0; k < 200; k++)
{ }
}
data[0] = 0x04001080;
DeviceIoControl(hLed, 0x04001080, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
for (int j = 0; j < 60000; j++)
{
for (int k = 0; k < 200; k++)
{ }
}
data[0] = 0x04001081;
DeviceIoControl(hLed, 0x04001081, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
for (int j = 0; j < 60000; j++)
{
for (int k = 0; k < 200; k++)
{ }
}
data[0] = 0x04001090;
DeviceIoControl(hLed, 0x04001090, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
for (int j = 0; j < 60000; j++)
{
for (int k = 0; k < 200; k++)
{ }
}
data[0] = 0x04001091;
DeviceIoControl(hLed, 0x04001091, null, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);
}
}
}
}