Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-10
3
Medium Priority
?
884 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

Technology Partners: 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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

596 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