C#与USB设备通信 kernel32.dll

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Text;

  4. using System.Runtime.InteropServices;

  5.  
  6. namespace WindowsApplication1

  7. {

  8. class Class1

  9. {

  10. [DllImport("kernel32.dll")]

  11. private static extern IntPtr CreateFile(

  12. String lpFileName,

  13. UInt32 dwDesiredAccess,

  14. UInt32 dwShareMode,

  15. IntPtr lpSecurityAttributes,

  16. UInt32 dwCreationDisposition,

  17. UInt32 dwFlagsAndAttributes,

  18. IntPtr hTemplateFile

  19. );

  20.  
  21. [DllImport("Kernel32.dll")]

  22. private static extern bool ReadFile(

  23. IntPtr hFile,

  24. byte[] lpBuffer,

  25. uint nNumberOfBytesToRead,

  26. ref uint lpNumberOfBytesRead,

  27. IntPtr lpOverlapped

  28. );

  29.  
  30. [DllImport("Kernel32.dll")]

  31. private static extern bool WriteFile(

  32. IntPtr hFile,

  33. byte[] lpBuffer,

  34. uint nNumberOfBytesToWrite,

  35. ref uint lpNumberOfBytesWritten,

  36. IntPtr lpOverlapped

  37. );

  38.  
  39. [DllImport("kernel32.dll")]

  40. private static extern bool CloseHandle(

  41. IntPtr hObject

  42. );

  43.  
  44. //--------------------------------------------------------------------------------

  45. IntPtr hFile;

  46.  
  47. private const UInt32 GENERIC_READ = 0x80000000;

  48. private const UInt32 GENERIC_WRITE = 0x40000000;

  49. private const UInt32 OPEN_EXISTING = 3;

  50. private const Int32 INVALID_HANDLE_VALUE = -1;

  51. private const int USB_WRITENUM = 8;

  52. private const int USB_READNUM = 8;

  53.  
  54. private byte[] m_rd_data = new byte[USB_READNUM];

  55. public byte[] rd_data

  56. {

  57. get { return m_rd_data; }

  58. set { m_rd_data = value; }

  59. }

  60. private byte[] m_wr_data = new byte[USB_WRITENUM];

  61. public byte[] wr_data

  62. {

  63. get { return m_wr_data; }

  64. set { m_wr_data = value; }

  65. }

  66.  
  67. public bool OnInitUSB()

  68. {

  69. hFile = IntPtr.Zero;

  70. string deviceName = string.Empty;

  71. deviceName = "在这里写上你的设备的地址";

  72.  
  73. hFile = CreateFile(

  74. deviceName,

  75. GENERIC_READ | GENERIC_WRITE,

  76. 0,

  77. IntPtr.Zero,

  78. OPEN_EXISTING,

  79. 0,

  80. IntPtr.Zero

  81. );

  82.  
  83. return hFile.ToInt32() == INVALID_HANDLE_VALUE ? false : true;

  84. }

  85.  
  86. public bool USBDataRead()

  87. {

  88. uint read = 0;

  89. return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero);

  90. }

  91.  
  92. public bool USBDataWrite()

  93. {

  94. uint written = 0;

  95. return WriteFile(hFile, m_wr_data, (uint)USB_WRITENUM, ref written, IntPtr.Zero);

  96. }

  97.  
  98. public void CloseConnection()

  99. {

  100. if (hFile.ToInt32() != INVALID_HANDLE_VALUE)

  101. {

  102. CloseHandle(hFile);

  103. hFile = IntPtr.Zero;

  104. }

  105. }

  106. }

  107. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值