您当前的位置: 首页 >  c#

鱼儿-1226

暂无认证

  • 0浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#与USB设备通信 kernel32.dll

鱼儿-1226 发布时间:2020-08-26 10:33:27 ,浏览量:0

  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. }

 

关注
打赏
1604459285
查看更多评论
立即登录/注册

微信扫码登录

0.0450s