做过USB HID设备,信赖千问写代码,就想交给它来完成,结果代码如下
//#include "stdafx.h" // VS2010 默认预编译头
#include "CounterCardSDK.h"
#include <setupapi.h>
#include "hidsdi.h"
#include <winsock2.h>
#include <stdio.h> // 必须包含,用于 printf/sprintf
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "hidsdi.lib")
#pragma comment(lib, "ws2_32.lib")
// --- 全局变量 ---
static std::vector<DeviceInfo> g_Devices;
static int g_CurrentIndex = -1;
static HANDLE g_hCom = INVALID_HANDLE_VALUE;
static SOCKET g_Socket = INVALID_SOCKET;
// --- 扫描串口 ---
void ScanSerialPorts() {
HKEY hKey;
// 注意:VS2010 推荐使用 RegOpenKeyExA 明确指定 ANSI 版本
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
char valueName[64];
char valueData[64];
DWORD valueNameLen = sizeof(valueName);
DWORD valueDataLen = sizeof(valueData);
DWORD type = REG_SZ;
DWORD index = 0;
while (RegEnumValueA(hKey, index++, valueName, &valueNameLen, NULL, &type, (LPBYTE)valueData, &valueDataLen) == ERROR_SUCCESS) {
DeviceInfo info = {0}; // C 风格初始化
strcpy_s(info.name, valueData); // 使用安全函数
info.type = DEV_TYPE_RS232;
info.isConnected = false;
g_Devices.push_back(info);
// 重置长度
valueNameLen = sizeof(valueName);
valueDataLen = sizeof(valueData);
}
RegCloseKey(hKey);
}
}
// --- 扫描 HID (模拟) ---
void ScanUSBHID() {
// 实际开发需引入 hidsdi.h 并链接 hid.lib
// 这里为了代码在 VS2010 能直接跑通,做简化处理
}
// --- 扫描以太网 (模拟) ---
void ScanEthernet() {
DeviceInfo info = {0};
strcpy_s(info.name, "192.168.1.200");
info.type = DEV_TYPE_ETHERNET;
info.isConnected = false;
// g_Devices.push_back(info);
}
// --- 导出函数实现 ---
extern "C" {
SDK_API int ScanDevices() {
g_Devices.clear();
g_CurrentIndex = -1;
ScanSerialPorts();
ScanUSBHID();
ScanEthernet();
return (int)g_Devices.size();
}
SDK_API bool GetDeviceInfo(int index, DeviceInfo* info) {
if (index >= 0 && index < (int)g_Devices.size() && info) {
*info = g_Devices[index];
return true;
}
return false;
}
SDK_API bool ConnectDevice(int index) {
if (index < 0 || index >= (int)g_Devices.size()) return false;
DisconnectDevice(); // 先断开旧的
DeviceInfo& dev = g_Devices[index];
if (dev.type == DEV_TYPE_RS232) {
char portPath[128];
sprintf_s(portPath, "\\\\.\\%s", dev.name);
// 打开串口
g_hCom = CreateFileA(portPath, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (g_hCom != INVALID_HANDLE_VALUE) {
dev.isConnected = true;
g_CurrentIndex = index;
return true;
}
}
else if (dev.type == DEV_TYPE_ETHERNET) {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0) {
g_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (g_Socket != INVALID_SOCKET) {
dev.isConnected = true;
g_CurrentIndex = index;
return true;
}
}
}
return false;
}
SDK_API void DisconnectDevice() {
if (g_CurrentIndex != -1) {
g_Devices[g_CurrentIndex].isConnected = false;
}
if (g_hCom != INVALID_HANDLE_VALUE) {
CloseHandle(g_hCom);
g_hCom = INVALID_HANDLE_VALUE;
}
if (g_Socket != INVALID_SOCKET) {
closesocket(g_Socket);
g_Socket = INVALID_SOCKET;
WSACleanup();
}
g_CurrentIndex = -1;
}
SDK_API double ReadPosition() {
if (g_CurrentIndex == -1) return 0.0;
// 模拟数据
return 123.45;
}
SDK_API bool WriteParameter(int paramId, int value) {
if (g_CurrentIndex == -1) return false;
printf("配置发送: ID=%d, Val=%d\n", paramId, value);
return true;
}
}
#pragma comment(lib, "hidsdi.lib")
这个坑挖的你不知不觉,结果是费了一天的时间找这个库,问千问,告诉你又是少这个"SDK",又是少那个SDK,左找右找,都没办法解决,最后重装Visual Studio,还是没办法解决,才想起之前我也做过HID设备,怎么被它带进坑里了呢,在Windows系统中开发HID设备根本不需要这个库,有"hid.lib"和"setupapi.lib"2个库就够了,不管是32位的还是64位的