Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find whether USB connected or not.I need this in if condition in C++

if ()
//USB Connected
else
//USB Not Connected

Can you please tell me the solution

What I have tried:

-----------------------------------------------------------------------------
Posted
Updated 9-Apr-18 3:23am

1 solution

It depends on what device you are checking for. You have to enumerate the USB devices and check for matching types or device names. Use something like "c++ linux enumerate usb devices" with your favorite search engine to know how to do that.

For a quick check of the existing devices use the lsusb command line tool (lsusb(8): USB devices - Linux man page[^]). You can also get the source code of that tool to see how the enumeration can be done.

[EDIT]
If you want to check for auto mounted USB storage devices list the entries of the directory where those devices are mounted (usually at /media) ignoring other devices like loaded CD/DVDs.

To get informed when USB media are inserted or removed you can use halevt or the old ivman.
[/EDIT]
 
Share this answer
 
v2
Comments
Member 13740197 10-Apr-18 2:53am    
I tried below,but if usb not connected also it is entering into if condition.Tell me why?

ret = system("lsusb -v");
if (ret == 0)
pop_up("Connected you USB drive...");
else
{
pop_up("USB Drive Not Connected..");
return;
}
Jochen Arndt 10-Apr-18 3:11am    
You should type lsusb on a shell first to see which devices are present so that you can identify for what you are looking.

If you want to use it with system(), you have to use the command line parameters -s or -d because only with those the return value specifies if matching devices has been found or not. In all other cases it returns zero (success) if no error occurs because it lists not only devices connected externally but also the internal hubs which are alwas present.
Member 13740197 10-Apr-18 6:28am    
It asking for arguments
lsusb -s [[bus]:][devnum]
lsusb -d vendor:[product]

what I have to give here.My requirement is only to check whether USB is connected or not.If connected it has to copy file otherwise it has to display a message to user to connect USB.

Jochen Arndt 10-Apr-18 6:46am    
See the first sentence of my answer:
"It depends on what device you are checking for".

USB is a bus where multiple devices can be connected. And there are allways already devices (the hubs on the mainboard) even if no external device is connected.

You have to specify first what you are looking for. I can't read your mind but I can guess that you are looking for storage devices. But there are many types of storage devices (USB sticks, external hard drives, memory card readers with inserted memory cards, smart phones in file access mode) and there may be multiple of them connected at once.

You might also have an USB keyboard and/or mouse connected.

I hope you understand now that "whether USB is connected or not" makes no sense for such a bus system when not defining a specific device or a device class

But I will update my answer reagrading USB storage devices beacuse they are usually automaticall mounted.
Member 13740197 10-Apr-18 7:06am    
Sorry for not mentioning,USB means Pendrive.I need to know whether Pendrive is connected or not using if condition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900