ok6410 LED/GPIO 控制 C#程序
2015-07-24 23:54
447 查看
原来的版本是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);
}
}
}
}
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);
}
}
}
}
相关文章推荐
- [转]改善C#程序的建议4:C#中标准Dispose模式的实现
- C#的方法
- [转]详解C#组件开发的来龙去脉
- C#多播委托
- 十六、C# 常用集合类及构建自定义集合(使用迭代器)
- C#中的委托是什么?
- 十五、C# 使用查询表达式的LINQ
- 十四、C# 支持标准查询运算符的集合接口
- C# PPT Operator
- C# ?? 和 ?: 运算符
- 学习笔记---c#中的get与set学习
- C# 内存管理
- C# 内存管理
- C#:实现接口中定义的事件
- 使用SuspendLayout和ResumeLayout
- c# byte[] 与string转化
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- 在C#中读写INI配置文件
- C# Attribute应用:类签名
- C#简单读取、改变文件的创建、修改及访问时间的方法