Nimit Shah
Nimit Shah

Reputation: 11

Program in c or Perl to find out which port of USB is being used?

Is there any way i can find out if a particular device is being plugged into a USB port and which USB port number is it plugged in, using C or Perl programming?

Upvotes: 1

Views: 963

Answers (2)

werewindle
werewindle

Reputation: 3029

Yes. There are portable means like using libusb to enumerate all devices.

Also, you can use OS specific features, like devfs or lsusb on Linux, or the registry or WMI on Windows.

The command lsusb shows you all connected devices. You can find the same information in the sysfs directory /sys/bus/usb/devices/.

On Windows you can dig through the registry at HKLM\System\CurrentControlSet\Enum\USB, or use WMI. WMI is accessible not only by Powershell, you can use Perl as well.

But, the preferred way is to use libusb because of its portability.

Upvotes: 4

xpapad
xpapad

Reputation: 4456

If you use linux you can just call lsusb from your script.

Upvotes: 0

Related Questions