123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- #include "USBInterFace.h"
- #include "spdlog/spdlog.h"
- USBInterface::USBInterface()
- {
- }
- /**
- * @brief 加载动态库
- *
- * @param libPath 动态库所在的文件夹
- * @return true
- * @return false
- */
- bool USBInterface::loadLib(const QString& libName)
- {
- if(libName.isEmpty())
- {
- return false;
- }
- // QString libName = libPath + "/USBInterFace.dll";
-
- if(m_lib.isLoaded())
- {
- m_lib.unload();
- }
- m_lib.setFileName(libName);
- if(!m_lib.load())
- {
- SPDLOG_ERROR("加载 {} 失败,错误信息:{}",libName.toStdString(), m_lib.errorString().toStdString());
- return false;
- }
- /* 获取函数 */
- m_specifyDevID = reinterpret_cast<SpecifyDevIdx>(m_lib.resolve("SpecifyDevIdx"));
- m_devOpen = reinterpret_cast<DeviceOpen>(m_lib.resolve("DeviceOpen"));
- m_devOpenWithID = reinterpret_cast<DeviceOpenWithID>(m_lib.resolve("DeviceOpenWithID"));
- m_devClose = reinterpret_cast<DeviceClose>(m_lib.resolve("DeviceClose"));
- m_usbCtrlTransSimple = reinterpret_cast<USBCtrlTransSimple>(m_lib.resolve("USBCtrlTransSimple"));
- m_usbCtrlTrans = reinterpret_cast<USBCtrlTrans>(m_lib.resolve("USBCtrlTrans"));
- m_bufferWR = reinterpret_cast<BufferWR>(m_lib.resolve("GetBuffer4Wr"));
- m_setInfo = reinterpret_cast<SetInfo>(m_lib.resolve("SetInfo"));
- m_clearBuffer = reinterpret_cast<ClearBuffer>(m_lib.resolve("CLearBuffer"));
- m_resetPipe = reinterpret_cast<ResetPipe>(m_lib.resolve("ResetPipe"));
- m_aiReadBulkData = reinterpret_cast<AiReadBulkData>(m_lib.resolve("AiReadBulkData"));
- m_eventCheck = reinterpret_cast<EventCheck>(m_lib.resolve("EventCheck"));
- if(m_specifyDevID == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 SpecifyDevIdx 失败");
- return false;
- }
- if(m_devOpen == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 DeviceOpen 失败");
- return false;
- }
- if(m_devOpenWithID == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 DeviceOpenWithID 失败");
- return false;
- }
- if(m_devClose == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 DeviceClose 失败");
- return false;
- }
- if(m_usbCtrlTransSimple == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 USBCtrlTransSimple 失败");
- return false;
- }
- if(m_usbCtrlTrans == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 USBCtrlTrans 失败");
- return false;
- }
- if(m_bufferWR == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 bufferWR 失败");
- return false;
- }
- if(m_setInfo == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 SetInfo 失败");
- return false;
- }
- if(m_clearBuffer == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 ClearBuffer 失败");
- return false;
- }
- if(m_resetPipe == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 ResetPipe 失败");
- return false;
- }
- if(m_aiReadBulkData == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 AiReadBulkData 失败");
- return false;
- }
- if(m_eventCheck == nullptr)
- {
- SPDLOG_ERROR("加载USBInterFace库函数 EventCheck 失败");
- return false;
- }
- return true;
- }
- /**
- * @brief 指定设备编号
- OSCA02 6
- OSC482 6
- *
- * @param idx
- */
- void USBInterface::specifyDevId(int idx)
- {
- if(m_specifyDevID == nullptr)
- {
- return;
- }
- m_specifyDevID(idx);
- }
- /**
- * @brief 打开设备,调用该函数之前需要先设置设备编号
- *
- * @return unsigned long
- */
- unsigned long USBInterface::devOpen()
- {
- if(m_devOpen == nullptr)
- {
- return 0;
- }
- return m_devOpen();
- }
- /* 打开设备 */
- unsigned long USBInterface::devOpenWithID(int ID)
- {
- if(m_devOpenWithID == nullptr)
- {
- return 0;
- }
- return m_devOpenWithID(ID);
- }
- /* 关闭设备 */
- unsigned long USBInterface::devClose()
- {
- if(m_devClose == nullptr)
- {
- return 0;
- }
- return m_devClose();
- }
- /* 开始采集数据命令 */
- unsigned long USBInterface::usbCtrlTransSimple(int32_t Request)
- {
- if(m_usbCtrlTransSimple == nullptr)
- {
- return 0;
- }
- return m_usbCtrlTransSimple(Request);
- }
- /**
- * @brief 示波器控制函数,设置示波器相关的参数
- *
- * @param Request 命令码,不同的命令码代表不同的指令
- * @param Value 命令码的参数
- * @param outBufSize 未使用,固定传1
- * @return unsigned char 如果是需要从示波器获取数据的命令,这个就是示波器返回的数据
- */
- unsigned char USBInterface::usbCtrlTrans(unsigned char Request, unsigned short Value)
- {
- if(m_usbCtrlTrans == nullptr)
- {
- return 0;
- }
- return m_usbCtrlTrans(Request, Value, 1);
- }
- /**
- * @brief 获取缓冲区指针
- *
- * @param index 双通道的型号,固定传-1
- * @return unsigned char*
- */
- unsigned char* USBInterface::bufferWR(int index)
- {
- if(m_bufferWR == nullptr)
- {
- return nullptr;
- }
- return m_bufferWR(index);
- }
- /**
- * @brief 设置缓冲区的大小,固定的参数直接默认传入了
- *
- * @param dataNumPerPixar 固定参数传1
- * @param CurrentFreq 固定参数传0
- * @param ChannelMask 固定参数传0x11
- * @param ZrroUniInt 固定参数传0
- * @param bufferoffset 固定参数传0
- * @param HWbufferSize 实际缓冲区的大小,这个缓冲区是AB字交替存储,所以实际大小是偶数,且不能超过缓冲区总大小20MB字节
- */
- void USBInterface::setInfo(unsigned int HWbufferSize)
- {
- if(m_setInfo == nullptr)
- {
- return;
- }
- m_setInfo(1, 0, 0x11, 0, 0, HWbufferSize);
- }
- /* 目前不知道清空什么 */
- unsigned long USBInterface::clearBuffer(unsigned char objA , unsigned char objB, unsigned int Num)
- {
- if(m_clearBuffer == nullptr)
- {
- return 0;
- }
- return m_clearBuffer(objA, objB, Num);
- }
- /* 清空缓冲区遗留的数据 */
- void USBInterface::resetPipe()
- {
- if(m_resetPipe == nullptr)
- {
- return;
- }
- m_resetPipe();
- }
- /**
- * @brief 将数据从示波器的SRAM拷贝到内存中,这个函数调用完成后会立刻返回,可以等待足够长的时间(也没说多长时间)或者
- * 使用EventCheck函数阻塞住,直到传输完成
- *
- * @param SampleCount 拷贝多少字节的电压数据,不能超过SetInfo设置的缓冲区大小
- * @param EventNum 将SampleCount分多少次传输完成,一般设置成1即可,既1次传输完成
- * @param TimeOut 超时时间,单位ms
- * @param Buffer 数据缓冲区的指针,可以使用GetBuffer4Wr获取的指针
- * @param Flag 固定传0
- * @param First_PacketNum 固定传0
- */
- void USBInterface::readBulkData(unsigned int SampleCount, unsigned int EventNum, unsigned int TimeOut, unsigned char* Buffer)
- {
- if(m_aiReadBulkData == nullptr)
- {
- return;
- }
- m_aiReadBulkData(SampleCount, EventNum, TimeOut, Buffer, 0, 0);
- }
- /**
- * @brief 阻塞等待数据拷贝完成,这个函数在readBulkData函数调用后使用
- *
- * @param Timeout 超时时间,单位ms
- * @return unsigned long 返回0x555,表示超时了,但是事件没有完成
- * 返回大于等于0,表示事件完成了,如果设置的是1次事件,返回的是0
- * 如果设置的是2次事件,第一次返回0,第二次返回1
- */
- unsigned long USBInterface::eventCheck(long Timeout)
- {
- if(m_eventCheck == nullptr)
- {
- return 0;
- }
- return m_eventCheck(Timeout);
- }
|