Vbarapi.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.InteropServices;
  7. namespace tool
  8. {
  9. public class Vbarapi
  10. {
  11. IntPtr dev = IntPtr.Zero;
  12. //打开信道
  13. [DllImport("vbar.dll", EntryPoint = "vbar_channel_open", CallingConvention = CallingConvention.Cdecl)]
  14. public static extern IntPtr vbar_channel_open(int type, long parm);
  15. //发送数据
  16. [DllImport("vbar.dll", EntryPoint = "vbar_channel_send", CallingConvention = CallingConvention.Cdecl)]
  17. public static extern int vbar_channel_send(IntPtr dev, byte[] data, int length);
  18. //接收数据
  19. [DllImport("vbar.dll", EntryPoint = "vbar_channel_recv", CallingConvention = CallingConvention.Cdecl)]
  20. public static extern int vbar_channel_recv(IntPtr dev, byte[] buffer, int size, int milliseconds);
  21. //关闭信道
  22. [DllImport("vbar.dll", EntryPoint = "vbar_channel_close", CallingConvention = CallingConvention.Cdecl)]
  23. public static extern void vbar_channel_close(IntPtr dev);
  24. //连接设备
  25. public bool openDevice()
  26. {
  27. dev = vbar_channel_open(1, 1);
  28. if (dev == IntPtr.Zero)
  29. {
  30. return false;
  31. }
  32. else
  33. {
  34. return true;
  35. }
  36. }
  37. public void closeDevice()
  38. {
  39. if (dev != null)
  40. {
  41. vbar_channel_close(dev);
  42. dev = IntPtr.Zero;
  43. }
  44. }
  45. byte[] iSetByte_ctl = new byte[64];
  46. //扫码开关
  47. public void controlScan(bool cswitch)
  48. {
  49. if (dev != null)
  50. {
  51. if (cswitch)
  52. {
  53. iSetByte_ctl[0] = 0x55;
  54. iSetByte_ctl[1] = 0xAA;
  55. iSetByte_ctl[2] = 0x05;
  56. iSetByte_ctl[3] = 0x01;
  57. iSetByte_ctl[4] = 0x00;
  58. iSetByte_ctl[5] = 0x00;
  59. iSetByte_ctl[6] = 0xfb;
  60. }
  61. else
  62. {
  63. iSetByte_ctl[0] = 0x55;
  64. iSetByte_ctl[1] = 0xAA;
  65. iSetByte_ctl[2] = 0x05;
  66. iSetByte_ctl[3] = 0x01;
  67. iSetByte_ctl[4] = 0x00;
  68. iSetByte_ctl[5] = 0x01;
  69. iSetByte_ctl[6] = 0xfa;
  70. }
  71. vbar_channel_send(dev, iSetByte_ctl, 64);
  72. }
  73. }
  74. //背光控制
  75. byte[] iSetByte = new byte[64];
  76. public void backlight(bool bswitch)
  77. {
  78. if (dev != null)
  79. {
  80. if (bswitch)
  81. {
  82. iSetByte[0] = 0x55;
  83. iSetByte[1] = 0xAA;
  84. iSetByte[2] = 0x24;
  85. iSetByte[3] = 0x01;
  86. iSetByte[4] = 0x00;
  87. iSetByte[5] = 0x01;
  88. iSetByte[6] = 0xDB;
  89. }
  90. else
  91. {
  92. iSetByte[0] = 0x55;
  93. iSetByte[1] = 0xAA;
  94. iSetByte[2] = 0x24;
  95. iSetByte[3] = 0x01;
  96. iSetByte[4] = 0x00;
  97. iSetByte[5] = 0x00;
  98. iSetByte[6] = 0xDA;
  99. }
  100. vbar_channel_send(dev, iSetByte, 64);
  101. }
  102. }
  103. //解码设置
  104. public bool getResultStr(out byte[] result_buffer, out int result_size)
  105. {
  106. byte[] c_result = new byte[256];
  107. if (dev != null)
  108. {
  109. byte[] bufferrecv = new byte[1024];
  110. //当接收到数据之后,自动设置包头标志和数据长度????????
  111. vbar_channel_recv(dev, bufferrecv, 1024, 200);
  112. if (bufferrecv[0] == 85 && bufferrecv[1] == 170 && bufferrecv[3] == 0)
  113. {
  114. int datalen = (bufferrecv[4] & 0xff) + ((bufferrecv[5] << 8) & 0xff);
  115. byte[] readBuffers = new byte[datalen];
  116. for (int s1 = 0; s1 < datalen; s1++)
  117. {
  118. readBuffers[s1] = bufferrecv[6 + s1];
  119. }
  120. result_buffer = readBuffers;
  121. result_size = datalen;
  122. return true;
  123. }
  124. else
  125. {
  126. result_buffer = null;
  127. result_size = 0;
  128. return false;
  129. }
  130. }
  131. else
  132. {
  133. result_buffer = null;
  134. result_size = 0;
  135. return false;
  136. }
  137. }
  138. }
  139. }