LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem Using Udev Rule (https://www.linuxquestions.org/questions/programming-9/problem-using-udev-rule-924729/)

akash2happy 01-20-2012 12:08 AM

Problem Using Udev Rule
 
Hello,
I am using headless Ubuntu 11.04 running on BeagleBoard XM.
Everytime i reboot the system ttyUSB[x] port_number change i want to use that port number in my C program for particular device.
Is it possible in udev rule so that we assign (ttyUSB[x])
the value of x for particular device. Whenever I connect usb to any port it will assign the port number that i have given.
if it is possible please help me with the code......

barunparichha 01-20-2012 05:48 AM

I suppose your /dev/ttyUSB* comes from minicom serial port, which will very from time to time.

And I fear we can't handle this from udev rules.
But you can initialte your code from some script:
Code:

Your script :
================
if [ -f /dev/ttyUSB0] then
./urcode /dev/ttyUSB0
else if [ -f /dev/ttyUSB1] then
./urcode /dev/ttyUSB1

In ur C-program:
=================
accept device node as argument (int main(int argc, char *argv) ).

there might be other ways to do so.

firstfire 01-20-2012 08:31 AM

Hi.
I'm not very familiar with udev, so there may be inaccuracies.
First you need to find a pair of numbers idVendor:idProduct which correspond to your usb device (such as usb-tty converter). Say, I have a Logitech usb mouse and I want udev to create /dev/logitech_mouse each time I plug it in.
To identify the device, type
Code:

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 028: ID 064e:a102 Suyin Corp. Acer/Lenovo Webcam [CN0316]
Bus 003 Device 002: ID 138a:0001 Validity Sensors, Inc. VFS101 Fingeprint Reader
Bus 003 Device 007: ID 0a5c:2151 Broadcom Corp. Bluetooth
Bus 005 Device 004: ID 046d:c062 Logitech, Inc.

So, for my mouse, idVendor=046d and idProduct=c062.

Now, to create udev rule I open /etc/udev/rules.d/99-local.rules and put there something like this
Code:

# Logitech mouse, example
SUBSYSTEMS=="usb", ATTRS{idProduct}=="c062", ATTRS{idVendor}=="046d", NAME="%k", SYMLINK+="logitech_mouse", MODE="0660"

To restart udev type
Code:

sudo /etc/init.d/udev restart
Hope this helps.

EDIT: Note that udev rules cannot span multiple lines.

akash2happy 01-23-2012 01:44 AM

Quote:

Originally Posted by barunparichha (Post 4579675)
I suppose your /dev/ttyUSB* comes from minicom serial port, which will very from time to time.

And I fear we can't handle this from udev rules.
But you can initialte your code from some script:
Code:

Your script :
================
if [ -f /dev/ttyUSB0] then
./urcode /dev/ttyUSB0
else if [ -f /dev/ttyUSB1] then
./urcode /dev/ttyUSB1

In ur C-program:
=================
accept device node as argument (int main(int argc, char *argv) ).

there might be other ways to do so.

thanku barunparichha


All times are GMT -5. The time now is 02:25 PM.