Solved

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

Posted on 2012-03-10
3
783 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSH commands for Nas4free 21 422
Issues with Ports in Linux (Hyper-V Guest) 2 102
Unix Script: Loop over all days of a month 17 96
Bash Script to Analyze Oracle Schemas 11 103
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 opening and writing to files in the C programming language.

776 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