?
Solved

How to virtually unplug a USB device in C/C++ and Ubuntu

Posted on 2012-03-10
3
Medium Priority
?
832 Views
Last Modified: 2012-03-17
I'm using a C program with termios to exchange information and commands between my pc with Ubuntu and a USB motor controller.

It works great and i'm able to successfully send messages over the serial port; however, sometimes it happens that if i close the program in the wrong way or something else happens, i'm not able any more to reconnect to my usb device.

To be able to connect again to the device, i have to reboot my pc and start again ubuntu.

I'd like to avoid this problem and to find a solution to enable again the usb port without have to reboot my system each time.

At the first time, i tried to kill all the processes (sudo killall -9 program_name) that were using the usb port, but the port still remains blocked and i have to reboot it.

Can you suggest me some solutions to avoid the reboot, please?
And why it happens?

I connect to the usb port (/dev/ttyACM0) in this way:
handle = open(port.c_str(), O_RDWR |O_NOCTTY | O_NDELAY);

	if(handle == RQ_INVALID_HANDLE)

	{

		cout<<"failed."<<endl;

		return RQ_ERR_OPEN_PORT;

	}

Open in new window

0
Comment
Question by:ichigokurosaki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Assisted Solution

by:eager
eager earned 1000 total points
ID: 37707606
Try "echo 1>/sys/bus/usb/devices/usbXXX/remove" or the equivalent in your program.

You will need to identify the correct usbXXX device.
0
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 1000 total points
ID: 37708084
When you have the problem, check to see whether there is a new tty device in /dev/tty. This might happen after a USB disconnect for example. I expect you would find the new device works fine.
This is the approach we took at work with a USB modem which would reset itself sometimes. Using a shell script and some symlink trickery, we were able to ensure that /dev/ttyUSBx always pointed to a working port (3 ports altogether).
Alternatively try unplugging and replugging the USB device. That should definitely create a new entry in /dev/tty.
0
 

Author Closing Comment

by:ichigokurosaki
ID: 37732987
Thanks, guys!

When the usb fails, i try to search next mounted device with a while() loop, and it works!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question