• 大小: 25KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-13
  • 语言: C#
  • 标签: C#  

资源简介

C#控制GPIO

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Security;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;


namespace GPIO
{
    public partial class Form1 : Form
    {
        [DllImport(“coredll.dll“ EntryPoint = “DeviceIoControl“ SetLastError = true)]
        internal static extern int DeviceIoControl(
            IntPtr hDevice
            int dwIoControlCode
            byte[] lpInBuffer
            int nInBufferSize
            byte[] lpOutBuffer
            int nOutBufferSize
            ref int lpBytesReturned
            IntPtr lpOverlapped);

        [DllImport(“coredll.dll“)]
        private static extern IntPtr CreateFile(
            string lpFileName
            // 要打开的串口名称           
            uint dwDesiredAccess
            // 指定串口的访问方式一般设置为可读可写方式   
            int dwShareMode
            // 指定串口的共享模式串口不能共享所以设置为0   
            int lpSecurityAttributes
            // 设置串口的安全属性WIN9X下不支持应设为NULL   
            int dwCreationDisposition
            // 对于串口通信创建方式只能为OPEN_EXISTING   
             int dwFlagsAndAttributes
            // 指定串口属性与标志设置为FILE_FLAG_OVERLAPPED( 重叠I/O操作 )指定串口以异步方式通信   
             int hTemplateFile
            // 对于串口通信必须设置为NULL   
        );

        private const uint GENERIC_READ = 0x80000000;
        private const uint GENERIC_WRITE = 0x40000000;
        private const int OPEN_EXISTING = 3;

        public Form1()
        {
            InitializeComponent();
        }
        IntPtr gpiodriver;
        private void button1_Click(object sender EventArgs e)
        {
            gpiodriver = CreateFile(“GIO1:“ GENERIC_READ | GENERIC_WRITE 0 0 OPEN_EXISTING 0 0);
            if (gpiodriver == (IntPtr)(-1))
            {
                MessageBox.Show(“打开GPIO设备失败!“);
            }
            else
            {
                int bytesReturned = 0;
                DeviceIoControl(gpiodriver 0x01 null 0 null 0 ref bytesReturned IntPtr.Zero);
                Thread.Sleep(5000);
                DeviceIoControl(gpiodriver 0x02 null 0 null 0 ref bytesReturned IntPtr.Zero);
                Thread.Sleep(5000);
                DeviceIoControl(gpiodriver 0x03 null 0 null 0 ref bytesReturned IntPtr.Zero);
                Thread.Sleep(5000);
                DeviceIoControl(gpiodriver 0x04 null 0 null 0 ref bytesReturned IntPtr.Zero);
                Thread.Sleep(5000);
                DeviceIoControl(gpiodriver 0x06 null 0 null 0 ref bytesReturned IntPtr.Zero);
                Thread.Sleep(5000);
                DeviceIoControl(gpiodriver 0x07 null 0 null 0 ref bytesReturned IntPtr.Zero);
                Thread.Sleep(5000);
              

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       7680  2012-09-04 15:43  GPIO\GPIO\bin\Debug\GPIO.exe

     文件      19968  2012-09-04 15:43  GPIO\GPIO\bin\Debug\GPIO.pdb

     文件       3525  2012-09-04 16:06  GPIO\GPIO\Form1.cs

     文件       2163  2012-09-04 14:56  GPIO\GPIO\Form1.Designer.cs

     文件       6298  2012-09-04 14:56  GPIO\GPIO\Form1.resx

     文件       4057  2012-09-04 15:14  GPIO\GPIO\GPIO.csproj

     文件        198  2012-09-04 15:22  GPIO\GPIO\GPIO.csproj.user

     文件        842  2012-09-04 15:14  GPIO\GPIO\obj\Debug\GPIO.csproj.GenerateResource.Cache

     文件       7680  2012-09-04 15:43  GPIO\GPIO\obj\Debug\GPIO.exe

     文件        180  2012-09-04 15:14  GPIO\GPIO\obj\Debug\GPIO.Form1.resources

     文件      19968  2012-09-04 15:43  GPIO\GPIO\obj\Debug\GPIO.pdb

     文件        180  2012-09-04 15:14  GPIO\GPIO\obj\Debug\GPIO.Properties.Resources.resources

     文件        527  2012-09-04 15:43  GPIO\GPIO\obj\GPIO.csproj.FileListAbsolute.txt

     文件        349  2012-09-04 14:26  GPIO\GPIO\Program.cs

     文件       1123  2012-09-04 14:26  GPIO\GPIO\Properties\AssemblyInfo.cs

     文件       2345  2012-09-04 14:26  GPIO\GPIO\Properties\Resources.Designer.cs

     文件       5618  2012-09-04 14:26  GPIO\GPIO\Properties\Resources.resx

     文件       1067  2012-09-04 14:26  GPIO\GPIO.sln

    ..A..H.     14336  2012-09-04 16:26  GPIO\GPIO.suo

     目录          0  2012-09-04 14:26  GPIO\GPIO\obj\Debug\TempPE

     目录          0  2012-09-04 15:19  GPIO\GPIO\bin\Debug

     目录          0  2012-09-04 15:43  GPIO\GPIO\obj\Debug

     目录          0  2012-09-04 14:26  GPIO\GPIO\bin

     目录          0  2012-09-04 15:14  GPIO\GPIO\obj

     目录          0  2012-09-04 14:26  GPIO\GPIO\Properties

     目录          0  2012-09-04 16:06  GPIO\GPIO

     目录          0  2012-09-04 14:26  GPIO

----------- ---------  ---------- -----  ----

                98104                    27



............此处省略0个文件信息

评论

共有 条评论