增加USB端口以定位设备,避免多台同VID/PID设备被识别为一台

This commit is contained in:
gb 2022-10-11 14:43:29 +08:00
parent 7ff6c33a4f
commit 62fe1bd9bc
2 changed files with 14 additions and 2 deletions

View File

@ -150,6 +150,10 @@ usb_device::usb_device(const char* name) : ref_(1), name_(name ? name : ""), is_
{ {
bzero(&guid_, sizeof(guid_)); bzero(&guid_, sizeof(guid_));
id_ = usb_device::vid_pid_from_name(name); id_ = usb_device::vid_pid_from_name(name);
GUID guid;
UuidFromStringA((RPC_CSTR)HG_SCANNER_GUID, &guid);
id_.addr = usb_device::get_device_address(name_.c_str(), &guid);
} }
usb_device::~usb_device() usb_device::~usb_device()
{ {
@ -200,7 +204,7 @@ int usb_device::set_timeout(HANDLE h)
DEVID usb_device::vid_pid_from_name(const char* name) DEVID usb_device::vid_pid_from_name(const char* name)
{ {
// name: \\?\usb#vid_3072&pid_0239#01234567aabbccddee#{a5dcbf10-6530-11d2-901f-00c04fb951ed} // name: \\?\usb#vid_3072&pid_0239#01234567aabbccddee#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
DEVID id = { 0 }; DEVID id;
std::string s(name ? name : ""); std::string s(name ? name : "");
size_t pos = 0; size_t pos = 0;
@ -639,6 +643,7 @@ bool usb_device::init(void)
free_usb_device_info(info); free_usb_device_info(info);
} }
id_.addr = port_;
return is_ok_; return is_ok_;
} }
@ -1010,6 +1015,8 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
int ev = arrive ? LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED : LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT; int ev = arrive ? LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED : LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT;
DEVID id = dev->id(); DEVID id = dev->id();
VLOG_MINI_4(LOG_LEVEL_DEBUG_INFO, "WINUSB %04X:%04X(%d) %s\r\n", dev->id().vid, dev->id().pid, dev->id().addr,
arrive ? "Connected" : "Left");
if (arrive) if (arrive)
{ {
bool found = false; bool found = false;

View File

@ -23,10 +23,15 @@ typedef struct _dev_id
{ {
int vid; int vid;
int pid; int pid;
int addr;
struct _dev_id()
{
memset(this, 0, sizeof(struct _dev_id));
}
bool operator==(const struct _dev_id& r) bool operator==(const struct _dev_id& r)
{ {
return vid == r.vid && pid == r.pid; return vid == r.vid && pid == r.pid && r.addr == addr;
} }
}DEVID; }DEVID;