-
设备初始化
#include "../../include/HCUsbSDK.h" if (!USB_Init()) { printf("USB_Init failed\n"); return -1; } -
获取已连接的设备数量
int count = USB_GetDeviceCount(); printf("DeviceCount=%d\n", count); if (count <= 0) { USB_Cleanup(); return -1; } -
遍历设备
USB_DEVICE_INFO *devices = malloc(sizeof(USB_DEVICE_INFO) * count); if (!USB_EnumDevices(count, devices)) { free(devices); USB_Cleanup(); return -1; } printf("Device: type=%d name=%s\n",devices[0].byProtocolType,devices[0].szDeviceName); -
登录设备0
/* ========== 登录设备 ========== */ USB_USER_LOGIN_INFO li = {0}; USB_DEVICE_REG_RES lr = {0}; li.dwSize = sizeof(li); li.dwTimeout = 5000; li.dwVID = devices[0].dwVID; li.dwPID = devices[0].dwPID; memcpy(li.szSerialNumber, devices[0].szSerialNumber, MAX_SERIAL_NUMBER_LEN); memcpy(li.szUserName, "admin", 5); memcpy(li.szPassword, "12345", 5); li.byLoginMode = 1; lr.dwSize = sizeof(lr); LONG userId = USB_Login(&li, &lr); if (userId < 0) { printf("Login failed, err=%d\n", USB_GetLastError()); free(devices); USB_Cleanup(); return -1; }
整体代码
#include "../../include/HCUsbSDK.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
if (!USB_Init())
{
printf("USB_Init failed\n");
return -1;
}
int count = USB_GetDeviceCount();
printf("DeviceCount=%d\n", count);
if (count <= 0)
{
USB_Cleanup();
return -1;
}
USB_DEVICE_INFO *devices = malloc(sizeof(USB_DEVICE_INFO) * count);
if (!USB_EnumDevices(count, devices))
{
free(devices);
USB_Cleanup();
return -1;
}
printf("Device: type=%d name=%s\n", devices[0].byProtocolType, devices[0].szDeviceName);
/* ========== 登录设备 ========== */
USB_USER_LOGIN_INFO li = {0};
USB_DEVICE_REG_RES lr = {0};
li.dwSize = sizeof(li);
li.dwTimeout = 5000;
li.dwVID = devices[0].dwVID;
li.dwPID = devices[0].dwPID;
memcpy(li.szSerialNumber, devices[0].szSerialNumber, MAX_SERIAL_NUMBER_LEN);
memcpy(li.szUserName, "admin", 5);
memcpy(li.szPassword, "12345", 5);
li.byLoginMode = 1;
lr.dwSize = sizeof(lr);
LONG userId = USB_Login(&li, &lr);
if (userId < 0)
{
printf("Login failed, err=%d\n", USB_GetLastError());
free(devices);
USB_Cleanup();
return -1;
}
printf("Login OK, UserID=%ld\n", (long)userId);
printf(" DeviceName=%s\n", lr.szDeviceName);
printf(" SerialNumber=%s\n", lr.szSerialNumber);
printf(" SoftwareVersion=%d.%d\n",
lr.dwSoftwareVersion >> 16, lr.dwSoftwareVersion & 0xFFFF);
printf(" RetryLoginTimes=%d\n", lr.byRetryLoginTimes);
printf(" SurplusLockTime=%u\n", lr.dwSurplusLockTime);
/* ========== 登出并清理 ========== */
USB_Logout(userId);
USB_Cleanup();
free(devices);
printf("Done.\n");
return 0;
}