LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-07-2007, 12:01 PM   #1
bcg121
LQ Newbie
 
Registered: Oct 2007
Location: Pennsylvania
Posts: 19

Rep: Reputation: 0
Determining ttyUSB* based on location


I have two Sealevel USB-to-Serial adapters. They initially come up as /dev/ttyUSB1 and /dev/ttyUSB2 (another device briefly comes up as /dev/ttyUSB0 and then goes away). When these devices are disconnected and reconnected and my application keeps its handle to them open, they come back as /dev/ttyUSB0 and /dev/ttyUSB3.

What I need to do is check the return value of my read() and write() calls and close my port handle when they fail. Then I can call open again the next time I need to read/write. The problem, I think, is that if I don't close the handle quick enough, the device will still come back as a different ttyUSB* then it was originally.

So my question is, in my C user-mode application, can I programmatically determine the ttyUSB* based on a physical location. For example, based on any of the following information:

First Sealevel card:
hub 4-0:1.0: port 1
usb 4-1
ftdi_sio 4-1:1.0

2nd Sealevel card:
hub 4-0:1.0: port 2
usb 4-2
ftdi_sio 4-2:1.0
 
Old 11-08-2007, 09:29 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
As you probably know, if you want to see a hierarchy of hardware connected to your computer, you can use linux’s sysfs virtual filesystem. Sometimes it’s hard to see where things go or come from. An easy way to see this is to use the udevmonitor utility. First, unplug both devices. Then, at a root shell, type:
Code:
udevmonitor -kue
(this should show you kernel events, udev events, and a nice collection of environment variables for each event). Now, plug in one device. Wait a bit. Now, remove it. Wait a bit. Plug in the other device. Etc.

The DEVPATH or PHYSDEVPATH variables show you what physical connection your device has. Those are actual subdirectories of /sys.

In addition to just learning this information, you can use it. If you create a udev rule, you might assign each ttyUSBx based on something distinguishable about your device (perhaps a serial number). Using usb device locations will probably work for simple setups, but is not recommended for production setups (because of USB’s daisy-chain nature). That’s one way to assign tty based on physical devices.
 
Old 11-09-2007, 06:14 AM   #3
bcg121
LQ Newbie
 
Registered: Oct 2007
Location: Pennsylvania
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks for the reply! That is more or less what I ended up doing.

I use opendir() to open up the following directories for the two different ports:
/sys/devices/pci0000:00/0000:00:1d.2/usb4/4-1/4-1:1.0/
/sys/devices/pci0000:00/0000:00:1d.2/usb4/4-2/4-2:1.0/

Then, I use readdir() until I find a directory entry that starts with ttyUSB. That's how I figure out what "/dev/ttyUSB*" to use in my call to open().
 
Old 11-09-2007, 10:32 AM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by bcg121 View Post
That's how I figure out what "/dev/ttyUSB*" to use in my call to open().
I think it would be beneficial to move the logic of determining the tty out of your program and into udev rules.

Suppose you have two ftdi devices, a green one and an orange one. You can use udev rules to arrange the insertion and removal of the devices to trigger the creation of certain device nodes. For example, you can make it so the green one will always exist as “/dev/ttyUSB/green” and the orange one will always exist as “/dev/ttyUSB/orange” by matching distinguishable qualities of each device. Then, in your program, you don’t have to mess around with determining which is which, because you know that the “/dev/ttyUSB/green” will always be the character device for the green, and that “/dev/ttyUSB/orange” will always be the character device for the orange.

Of course in real life you might chose something besides their colors for the names (for example, the device on the other end). You might have it so “/dev/ttyUSB/bottom” and “/dev/ttyUSB/top” describe the two devices based on whether they are plugged into the bottom or top of your USB ports. This would remove some complexity from your program, and let it worry about what it needs to do.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux-based website beats Windows-based sites LXer Syndicated Linux News 0 06-21-2007 04:17 AM
ttyUSB iomari SUSE / openSUSE 3 11-04-2005 02:31 AM
Quarantine files based on their extension and location xbaez Programming 7 07-07-2005 12:36 PM
no /dev/ttyUSB* with FC3 please help derddera Fedora 3 12-30-2004 07:43 AM
/dev/ttyUSB? disappaers on SuSE 9.1 with pilot-link gevard Linux - Laptop and Netbook 0 08-29-2004 02:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration