USBInterFace.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "USBInterFace.h"
  2. #include "spdlog/spdlog.h"
  3. USBInterface::USBInterface()
  4. {
  5. }
  6. /**
  7. * @brief 加载动态库
  8. *
  9. * @param libPath 动态库所在的文件夹
  10. * @return true
  11. * @return false
  12. */
  13. bool USBInterface::loadLib(const QString& libName)
  14. {
  15. if(libName.isEmpty())
  16. {
  17. return false;
  18. }
  19. // QString libName = libPath + "/USBInterFace.dll";
  20. if(m_lib.isLoaded())
  21. {
  22. m_lib.unload();
  23. }
  24. m_lib.setFileName(libName);
  25. if(!m_lib.load())
  26. {
  27. SPDLOG_ERROR("加载 {} 失败,错误信息:{}",libName.toStdString(), m_lib.errorString().toStdString());
  28. return false;
  29. }
  30. /* 获取函数 */
  31. m_specifyDevID = reinterpret_cast<SpecifyDevIdx>(m_lib.resolve("SpecifyDevIdx"));
  32. m_devOpen = reinterpret_cast<DeviceOpen>(m_lib.resolve("DeviceOpen"));
  33. m_devOpenWithID = reinterpret_cast<DeviceOpenWithID>(m_lib.resolve("DeviceOpenWithID"));
  34. m_devClose = reinterpret_cast<DeviceClose>(m_lib.resolve("DeviceClose"));
  35. m_usbCtrlTransSimple = reinterpret_cast<USBCtrlTransSimple>(m_lib.resolve("USBCtrlTransSimple"));
  36. m_usbCtrlTrans = reinterpret_cast<USBCtrlTrans>(m_lib.resolve("USBCtrlTrans"));
  37. m_bufferWR = reinterpret_cast<BufferWR>(m_lib.resolve("GetBuffer4Wr"));
  38. m_setInfo = reinterpret_cast<SetInfo>(m_lib.resolve("SetInfo"));
  39. m_clearBuffer = reinterpret_cast<ClearBuffer>(m_lib.resolve("CLearBuffer"));
  40. m_resetPipe = reinterpret_cast<ResetPipe>(m_lib.resolve("ResetPipe"));
  41. m_aiReadBulkData = reinterpret_cast<AiReadBulkData>(m_lib.resolve("AiReadBulkData"));
  42. m_eventCheck = reinterpret_cast<EventCheck>(m_lib.resolve("EventCheck"));
  43. if(m_specifyDevID == nullptr)
  44. {
  45. SPDLOG_ERROR("加载USBInterFace库函数 SpecifyDevIdx 失败");
  46. return false;
  47. }
  48. if(m_devOpen == nullptr)
  49. {
  50. SPDLOG_ERROR("加载USBInterFace库函数 DeviceOpen 失败");
  51. return false;
  52. }
  53. if(m_devOpenWithID == nullptr)
  54. {
  55. SPDLOG_ERROR("加载USBInterFace库函数 DeviceOpenWithID 失败");
  56. return false;
  57. }
  58. if(m_devClose == nullptr)
  59. {
  60. SPDLOG_ERROR("加载USBInterFace库函数 DeviceClose 失败");
  61. return false;
  62. }
  63. if(m_usbCtrlTransSimple == nullptr)
  64. {
  65. SPDLOG_ERROR("加载USBInterFace库函数 USBCtrlTransSimple 失败");
  66. return false;
  67. }
  68. if(m_usbCtrlTrans == nullptr)
  69. {
  70. SPDLOG_ERROR("加载USBInterFace库函数 USBCtrlTrans 失败");
  71. return false;
  72. }
  73. if(m_bufferWR == nullptr)
  74. {
  75. SPDLOG_ERROR("加载USBInterFace库函数 bufferWR 失败");
  76. return false;
  77. }
  78. if(m_setInfo == nullptr)
  79. {
  80. SPDLOG_ERROR("加载USBInterFace库函数 SetInfo 失败");
  81. return false;
  82. }
  83. if(m_clearBuffer == nullptr)
  84. {
  85. SPDLOG_ERROR("加载USBInterFace库函数 ClearBuffer 失败");
  86. return false;
  87. }
  88. if(m_resetPipe == nullptr)
  89. {
  90. SPDLOG_ERROR("加载USBInterFace库函数 ResetPipe 失败");
  91. return false;
  92. }
  93. if(m_aiReadBulkData == nullptr)
  94. {
  95. SPDLOG_ERROR("加载USBInterFace库函数 AiReadBulkData 失败");
  96. return false;
  97. }
  98. if(m_eventCheck == nullptr)
  99. {
  100. SPDLOG_ERROR("加载USBInterFace库函数 EventCheck 失败");
  101. return false;
  102. }
  103. return true;
  104. }
  105. /**
  106. * @brief 指定设备编号
  107. OSCA02 6
  108. OSC482 6
  109. *
  110. * @param idx
  111. */
  112. void USBInterface::specifyDevId(int idx)
  113. {
  114. if(m_specifyDevID == nullptr)
  115. {
  116. return;
  117. }
  118. m_specifyDevID(idx);
  119. }
  120. /**
  121. * @brief 打开设备,调用该函数之前需要先设置设备编号
  122. *
  123. * @return unsigned long
  124. */
  125. unsigned long USBInterface::devOpen()
  126. {
  127. if(m_devOpen == nullptr)
  128. {
  129. return 0;
  130. }
  131. return m_devOpen();
  132. }
  133. /* 打开设备 */
  134. unsigned long USBInterface::devOpenWithID(int ID)
  135. {
  136. if(m_devOpenWithID == nullptr)
  137. {
  138. return 0;
  139. }
  140. return m_devOpenWithID(ID);
  141. }
  142. /* 关闭设备 */
  143. unsigned long USBInterface::devClose()
  144. {
  145. if(m_devClose == nullptr)
  146. {
  147. return 0;
  148. }
  149. return m_devClose();
  150. }
  151. /* 开始采集数据命令 */
  152. unsigned long USBInterface::usbCtrlTransSimple(int32_t Request)
  153. {
  154. if(m_usbCtrlTransSimple == nullptr)
  155. {
  156. return 0;
  157. }
  158. return m_usbCtrlTransSimple(Request);
  159. }
  160. /**
  161. * @brief 示波器控制函数,设置示波器相关的参数
  162. *
  163. * @param Request 命令码,不同的命令码代表不同的指令
  164. * @param Value 命令码的参数
  165. * @param outBufSize 未使用,固定传1
  166. * @return unsigned char 如果是需要从示波器获取数据的命令,这个就是示波器返回的数据
  167. */
  168. unsigned char USBInterface::usbCtrlTrans(unsigned char Request, unsigned short Value)
  169. {
  170. if(m_usbCtrlTrans == nullptr)
  171. {
  172. return 0;
  173. }
  174. return m_usbCtrlTrans(Request, Value, 1);
  175. }
  176. /**
  177. * @brief 获取缓冲区指针
  178. *
  179. * @param index 双通道的型号,固定传-1
  180. * @return unsigned char*
  181. */
  182. unsigned char* USBInterface::bufferWR(int index)
  183. {
  184. if(m_bufferWR == nullptr)
  185. {
  186. return nullptr;
  187. }
  188. return m_bufferWR(index);
  189. }
  190. /**
  191. * @brief 设置缓冲区的大小,固定的参数直接默认传入了
  192. *
  193. * @param dataNumPerPixar 固定参数传1
  194. * @param CurrentFreq 固定参数传0
  195. * @param ChannelMask 固定参数传0x11
  196. * @param ZrroUniInt 固定参数传0
  197. * @param bufferoffset 固定参数传0
  198. * @param HWbufferSize 实际缓冲区的大小,这个缓冲区是AB字交替存储,所以实际大小是偶数,且不能超过缓冲区总大小20MB字节
  199. */
  200. void USBInterface::setInfo(unsigned int HWbufferSize)
  201. {
  202. if(m_setInfo == nullptr)
  203. {
  204. return;
  205. }
  206. m_setInfo(1, 0, 0x11, 0, 0, HWbufferSize);
  207. }
  208. /* 目前不知道清空什么 */
  209. unsigned long USBInterface::clearBuffer(unsigned char objA , unsigned char objB, unsigned int Num)
  210. {
  211. if(m_clearBuffer == nullptr)
  212. {
  213. return 0;
  214. }
  215. return m_clearBuffer(objA, objB, Num);
  216. }
  217. /* 清空缓冲区遗留的数据 */
  218. void USBInterface::resetPipe()
  219. {
  220. if(m_resetPipe == nullptr)
  221. {
  222. return;
  223. }
  224. m_resetPipe();
  225. }
  226. /**
  227. * @brief 将数据从示波器的SRAM拷贝到内存中,这个函数调用完成后会立刻返回,可以等待足够长的时间(也没说多长时间)或者
  228. * 使用EventCheck函数阻塞住,直到传输完成
  229. *
  230. * @param SampleCount 拷贝多少字节的电压数据,不能超过SetInfo设置的缓冲区大小
  231. * @param EventNum 将SampleCount分多少次传输完成,一般设置成1即可,既1次传输完成
  232. * @param TimeOut 超时时间,单位ms
  233. * @param Buffer 数据缓冲区的指针,可以使用GetBuffer4Wr获取的指针
  234. * @param Flag 固定传0
  235. * @param First_PacketNum 固定传0
  236. */
  237. void USBInterface::readBulkData(unsigned int SampleCount, unsigned int EventNum, unsigned int TimeOut, unsigned char* Buffer)
  238. {
  239. if(m_aiReadBulkData == nullptr)
  240. {
  241. return;
  242. }
  243. m_aiReadBulkData(SampleCount, EventNum, TimeOut, Buffer, 0, 0);
  244. }
  245. /**
  246. * @brief 阻塞等待数据拷贝完成,这个函数在readBulkData函数调用后使用
  247. *
  248. * @param Timeout 超时时间,单位ms
  249. * @return unsigned long 返回0x555,表示超时了,但是事件没有完成
  250. * 返回大于等于0,表示事件完成了,如果设置的是1次事件,返回的是0
  251. * 如果设置的是2次事件,第一次返回0,第二次返回1
  252. */
  253. unsigned long USBInterface::eventCheck(long Timeout)
  254. {
  255. if(m_eventCheck == nullptr)
  256. {
  257. return 0;
  258. }
  259. return m_eventCheck(Timeout);
  260. }