Solved

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

Posted on 2012-03-10
3
775 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
3 Comments
 
LVL 8

Assisted Solution

by:eager
eager earned 250 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 34

Accepted Solution

by:
Duncan Roe earned 250 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now