|
你好,这边按照同样的方式,使用最新的libBVUsb.dll针对设备做些操作,运行结果如下:
看到并没有出现连接失败的情况,BVUsb.exe进程不会被累积。另外,在进入U盘调用成功后并且设备进入了U盘模式,再断开设备连接,设备退出U盘需要一个过程,
在设备还没有退出U盘之前调用进入U盘的接口,会偶尔出现失败。我这边实现的代码如下:
#include <stdio.h>
#include <Windows.h>
#include <process.h>
#include "BVUsb.h"
#ifdef _DEBUG
#pragma comment(lib, "..\\..\\lib\\Debug\\libBVUsb.lib")
#else
#pragma comment(lib, "..\\..\\lib\\Release\\libBVUsb.lib")
#endif // _DEBUG
static HANDLE g_hThread = NULL;
static BOOL g_bRun = FALSE;
static unsigned __stdcall Thread_Proc(void* param);
int main(int argc, char** argv)
{
g_bRun = TRUE;
g_hThread = (HANDLE)_beginthreadex(nullptr, NULL, Thread_Proc, NULL, 0, NULL);
if (g_hThread == NULL) return 0;
while (g_bRun)
{
Sleep(20);
}
g_bRun = FALSE;
WaitForSingleObject(g_hThread, INFINITE);
if (g_hThread) {
CloseHandle(g_hThread);
g_hThread = NULL;
}
system("pause");
return 0;
}
unsigned __stdcall Thread_Proc(void* param)
{
if (RESULT_FLAG_S_OK != BV_ZFY_Init())
{
printf("BV_ZFY_Init 失败\b");
return 0;
}
int iDeviceCount = 0;
pDeviceArray deviceList;
char* szPassword = "123456";
while (g_bRun)
{
int iDeviceCount = MAX_DEVICE_COUNTS;
if (RESULT_FLAG_S_OK != BV_ZFY_devices(&iDeviceCount, &deviceList))
{
printf("BV_ZFY_devices 失败\n");
Sleep(3000);
continue;
}
// int iDeviceIndex = 0;
char szDeviceName[MAX_DEVICE_NAME_LENGTH] = { 0 };
for (int iDeviceIndex = 0; iDeviceIndex < iDeviceCount; iDeviceIndex++)
{
strncpy_s(szDeviceName, deviceList[iDeviceIndex], _TRUNCATE);
printf("================================================\n");
printf("设备 {%s} 被发现\n", szDeviceName);
unsigned short iRet = 0;
int iDeviceHandle = 0;
int iLevel = 0;
if (RESULT_FLAG_S_OK != BV_ZFY_Connect(szDeviceName, &iRet, &iDeviceHandle))
{
printf("设备 {%s} 连接失败\n", szDeviceName);
continue;
}
printf("设备 {%s} 连接成功\n", szDeviceName);
if (RESULT_FLAG_S_OK != BV_ZFY_Login_By_Index(iDeviceHandle, &iLevel, szPassword))
{
printf("设备 {%s} 登录接口调用失败\n", szDeviceName);
BV_ZFY_Disconnect(iDeviceHandle);
continue;
}
printf("设备: {%s} 登录成功\n", szDeviceName);
if (RESULT_FLAG_S_OK != BV_SyncDevTime(iDeviceHandle, szPassword, &iRet))
{
printf("BV_SyncDevTime fail, device: %s, ret: %d\n", szDeviceName, iRet);
BV_ZFY_Disconnect(iDeviceHandle);
continue;
}
printf("设备: {%s} 校时成功\n", szDeviceName);
#if 1
if (RESULT_FLAG_S_OK != BV_SetMSDC(iDeviceHandle, szPassword, &iRet))
{
printf("BV_SetMSDC fail, device: %s, ret: %d\n", szDeviceName, iRet);
BV_ZFY_Disconnect(iDeviceHandle);
continue;
}
printf("设备: {%s} 转磁盘模式成功\n", szDeviceName);
#endif
BV_ZFY_Disconnect(iDeviceHandle);
printf("================================================\n");
}
Sleep(2000);
}
return 0;
}
和您的代码基本类似。还望贵公司再次测试确认。
|
|