ok6410 LED/GPIO 控制 C#程序

原来的版本是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);


            }
            
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值