Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

697 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