C#与USB设备通信 kernel32.dll

本文介绍了一个使用C#实现的USB设备读写操作的简单示例。该示例通过调用kernel32.dll中的API函数实现了文件的创建、读取、写入及关闭等功能。文中定义了常量来指定读写权限、文件打开方式等,并提供了用于存储读写数据的缓冲区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
 
namespace WindowsApplication1
{
    class Class1
    {
        [DllImport("kernel32.dll")]
        private static extern IntPtr CreateFile(
            String lpFileName,
            UInt32 dwDesiredAccess,
            UInt32 dwShareMode,
            IntPtr lpSecurityAttributes,
            UInt32 dwCreationDisposition,
            UInt32 dwFlagsAndAttributes,
            IntPtr hTemplateFile
            );
 
        [DllImport("Kernel32.dll")]
        private static extern bool ReadFile(
            IntPtr hFile,
            byte[] lpBuffer,
            uint nNumberOfBytesToRead,
            ref uint lpNumberOfBytesRead,
            IntPtr lpOverlapped
            );
 
        [DllImport("Kernel32.dll")]
        private static extern bool WriteFile(
            IntPtr hFile,
            byte[] lpBuffer,
            uint nNumberOfBytesToWrite,
            ref uint lpNumberOfBytesWritten,
            IntPtr lpOverlapped
            );
 
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(
            IntPtr hObject
            );
 
        //--------------------------------------------------------------------------------
        IntPtr hFile;
 
        private const UInt32 GENERIC_READ = 0x80000000;
        private const UInt32 GENERIC_WRITE = 0x40000000;
        private const UInt32 OPEN_EXISTING = 3;
        private const Int32 INVALID_HANDLE_VALUE = -1;
        private const int USB_WRITENUM = 8;
        private const int USB_READNUM = 8;
 
        private byte[] m_rd_data = new byte[USB_READNUM];
        public byte[] rd_data
        {
            get { return m_rd_data; }
            set { m_rd_data = value; }
        }
        private byte[] m_wr_data = new byte[USB_WRITENUM];
        public byte[] wr_data
        {
            get { return m_wr_data; }
            set { m_wr_data = value; }
        }
 
        public bool OnInitUSB()
        {
            hFile = IntPtr.Zero;
            string deviceName = string.Empty;
            deviceName = "在这里写上你的设备的地址";
           
            hFile = CreateFile(
                deviceName,
                GENERIC_READ | GENERIC_WRITE,
                0,
                IntPtr.Zero,
                OPEN_EXISTING,
                0,
                IntPtr.Zero
                );
 
            return hFile.ToInt32() == INVALID_HANDLE_VALUE ? false : true;
        }
 
        public bool USBDataRead()
        {
            uint read = 0;
            return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero);
        }
 
        public bool USBDataWrite()
        {
            uint written = 0;
            return WriteFile(hFile, m_wr_data, (uint)USB_WRITENUM, ref written, IntPtr.Zero);
        }
 
        public void CloseConnection()
        {
            if (hFile.ToInt32() != INVALID_HANDLE_VALUE)
            {
                CloseHandle(hFile);
                hFile = IntPtr.Zero;
            }
        }
    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值