How to remap /dev/ttyUSB* to a specific name to be called by my program.

How to map /dev/ttyUSB* to specific device.

ref: How can I match a ttyUSBX device to a usb serial device

ref: http://unix.stackexchange.com/questions/81754/how-can-i-match-a-ttyusbx-device-to-a-usb-serial-device

ref: http://ubuntuforums.org/showthread.php?t=168221and : http://unix.stackexchange.com/questions/64266/putty-can-access-serial-port-as-dev-ttyusb0-but-not-as-named-udev-device

Quickly Reference/Steps:

Brief Steps:

1. Command:
>> lsusb
This lists your usb devices and "067b:2303",for instance, as its ID
or:
>> ls -l /sys/bus/usb-serial/devices
An other locations worth exploring are /sys/class/tty/ 2. Write this to /etc/udev/rules.d/50-usb.rules
----
SUBSYSTEM=="tty", ATTRS{idVendor}=="19d2", ATTRS{idProduct}=="0031", SYMLINK+="ONDA"
SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="14ac", SYMLINK+="HUAWEI"
----
Reboot.
Now you can use /dev/ONDA to get to your ONDA device.
If you want non-root users to be able to use these, then add
----
, MODE="0666"
----
to the end of each line. 3. To get enough information to distinguish the devices try something like this for all devices:
Command:
>>
$ udevadm info --query all -name /dev/ttyUSB0 --attribute-walk 4. To start using these new rules, you need to run the command udevstart
Command:
>> sudo udevstart
You can also restart udev using
Command:
>> sudo /etc/init.d/udev restart #you may need to reboot if this doesn't work. 5. Command:
>>

Create your own udev rules to control removable devices

I'm sure many people who use removable devices have noticed that sometimes they don't appear where they were before.



You plug your USB drive in, and use fdisk to find the device node and then mount it. Sometimes the USB appears as /dev/sda1, sometimes /dev/sdb1. It can depend on what order you plug in your USB devices, and where you plug them in. This is a real pain if you
mount devices manually or if you are trying to customise your /etc/fstab.



udev allows the assignment of a persistant device node, /dev/..., based on a rule match defined by your specific hardware. In other words, if a device
is attached that matches certain criteria it is given it's own device node, rather than being assigned a dynamic one.



It's actually really easy to setup.

To start with you need to know the dynamic device node that is given to a device when attached for the first time. The way that I would do this is to use thetail command

Code:
tail -f /var/log/messages

When a device is plugged in, the screen will update with the newest messages, usually imparting some knowledge on the location of the new device. My USB flash disc for example, produced this output when plugged in.

Apr 30 16:37:01 localhost kernel: [4294885.683000] usb 1-3: new full speed USB device using ohci_hcd and address 6

Apr 30 16:37:01 localhost kernel: [4294885.853000] scsi5 : SCSI emulation for USB Mass Storage devices

Apr 30 16:37:02 localhost usb.agent[10421]: usb-storage: already loaded

Apr 30 16:37:06 localhost kernel: [4294890.859000] Vendor: Model: TS128MJFLASHA Rev: 1.00

Apr 30 16:37:06 localhost kernel: [4294890.859000] Type: Direct-Access ANSI SCSI revision: 02

Apr 30 16:37:06 localhost kernel: [4294890.883000] SCSI device sdd: 253400 512-byte hdwr sectors (130 MB)

Apr 30 16:37:06 localhost kernel: [4294890.896000] sdd: Write Protect is off

Apr 30 16:37:06 localhost kernel: [4294890.924000] SCSI device sdd: 253400 512-byte hdwr sectors (130 MB)

Apr 30 16:37:06 localhost kernel: [4294890.937000] sdd: Write Protect is off

Apr 30 16:37:07 localhost kernel: [4294890.937000] /dev/scsi/host5/bus0/target0/lun0: p1

Apr 30 16:37:07 localhost kernel: [4294891.046000] Attached scsi removable disk
sdd
at scsi5, channel 0, id 0, lun 0

Apr 30 16:37:07 localhost scsi.agent[10469]: sd_mod: loaded sucessfully (for disk)

The output shows that the USB device attached is assigned the device node
/dev/sdd
. You may also want to determine the partition number using

Code:
sudo fdisk -l

For my USB flash disc, the partition is /dev/sdd1.



So next thing is to find out some unique information from the device, information that will be used in defining the udev rule, remembering a match is required to assign the persistant node. The next command I have used is from theWriting
udev rules
link at the bottom of this HOWTO

Code:
udevinfo -a -p $(udevinfo -q path -n /dev/sdd)

This command outputs a lot information about the hardware associated with
/dev/sdd
. I've left out a lot of the information, leaving only the section I think is most relevant. This section contains the more specific information about my USB flash disc.



Note the bolded text in the output. It is important that information used in a udev rule is contained in the one section.

Code:
udevinfo starts with the device the node belongs to and then walks up the
device chain, to print for every device found, all possibly useful attributes
in the udev key format.
Only attributes within one device section may be used together in one rule,
to match the device for which the node will be created.
looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.0/usb1/1-3':
BUS=="usb"
ID=="1-3"
DRIVER=="usb"
SYSFS{bConfigurationValue}=="1"
SYSFS{bDeviceClass}=="00"
SYSFS{bDeviceProtocol}=="00"
SYSFS{bDeviceSubClass}=="00"
SYSFS{bMaxPower}=="100mA"
SYSFS{bNumConfigurations}=="1"
SYSFS{bNumInterfaces}==" 1"
SYSFS{bcdDevice}=="0100"
SYSFS{bmAttributes}=="80"
SYSFS{configuration}==""
SYSFS{devnum}=="6"
SYSFS{idProduct}=="0005"
SYSFS{idVendor}=="0c76"
SYSFS{maxchild}=="0"
SYSFS{product}=="TS128MJFLASHA"
SYSFS{speed}=="12"
SYSFS{version}==" 1.10"

The items that contain specific information regarding my USB disc have been coloured red. These are the two items of interest to me, the device is connected to theUSB bus, and the product is identified byTS128MJFLASHA

______



The next step is to create the udev rule concerning this device. I'll start by creating my own .rules file

Code:
sudo nano /etc/udev/rules.d/10-local.rules

By naming my set of rules 10-local.rules they should be looked at before the other rules set in this folder.



The rule I will use for the flash disc looks like this.

BUS=="usb",
SYSFS{product}=="TS128MJFLASHA", KERNEL=="sd?1",NAME="transcend128mb",SYMLINK="usbdevices/transcend128mb"

A quick explanation.



- The BUS==”usb” and SYSFS{product}==”TS128MJFLASHA” options are the same as those I picked out from theudevinfo output.



- The option KERNEL="sd?1" will only match locations like /dev/sda1, /dev/sdb1 and more importantly, it won't match nodes like /dev/sda, /dev/sdb, which can be fdisk'ed. The 'Writing udev rules' guide also mentions this.



- The options NAME="128FLASH" and
SYMLINK="usbdisc/128FLASH" will create the persistant node at /dev/transcend128mb and a symlink /dev/usbdisc/transcend128mb that points to the persistant node /dev/transcend128mb. The SYMLINK option is not required. The reason I have included it is so
that all my USB devices will have a symlink starting with /dev/usbdevices/...

I just think its neater.



There are other options that could be used to create udev rules, such as GROUP=”some_group”, if you want to assigned the group ownership of the device node to a specific group, and MODE=”0660”, which would give the owner/group read and write permissions, likechmod.



The Writing udev rules guide contains some more detailed information on these other options.



______



To start using these new rules, you need to run the command udevstart

Code:
sudo udevstart

You can also restart udev using

Code:
sudo /etc/init.d/udev restart    #you may need to reboot your linux system if this doesn't work.

This should work for later versions of udev, that don't appear to use the udevstart command



- thanks to ash211 for pointing this out



Now to quickly check that the new node for my example has been created.

Code:
user@ubuntu:~$ ls -l /dev/trans*
brw-r----- 1 root plugdev 8, 49 2006-04-30 16:37 /dev/transcend128mb
user@ubuntu:~$ ls -l /dev/usbdevices/
lrwxrwxrwx 1 root root 17 2006-04-30 16:37 transcend128mb -> ../transcend128mb

______



Finally the fstab can be edited to include the new persistant device node, but first we'll back it up

Code:
sudo cp /etc/fstab /etc/fstab_backup
sudo nano /etc/fstab

Then we can add an entry for the example USB device using either the device node
or
the symlink (if used), so in my example I could either use the new device node

Code:
/dev/transcend128mb  /media/usb128mb  vfat  iocharset=utf8,umask=000   0   0

or the symlink to the node, which I prefer as all my USB devices have symlinks in /dev/usbdevices. I think makes the fstab look neater.

Code:
/dev/usbdevices/transcend128mb  /media/usb128mb vfat iocharset=utf8,umask=000  0  0

When the entry has been correctly entered, you can save the file (Ctrl+O) and exit (Ctrl+X), and then mount the device, in this example my USB disc using

Code:
sudo mount /media/usb128mb

or

Code:
sudo mount -a

Once this is all completed, the example USB drive will always appear at /dev/transcend128mb so the entry in the fstab can remain unchanged and will always find the device.





And you're all done!





Hope that helps some people, like it did me.



Please let me know if this works for you, and of course if there are any typos, errors or things that need clarifying.







The useful links I needed to get this working

Kernel.org - udev



Writing udev rules

Last edited by Sutekh; May 27th, 2006 at02:02 PM.

重新指派usb转串口模块在linux系统中的设备调用名称的更多相关文章

  1. Linux系统中存储设备的两种表示方法

    转:https://blog.csdn.net/holybin/article/details/38637381 一.对于IDE接口的硬盘的两种表示方法: 1.IDE接口硬盘,对于整块硬盘的两种表示方 ...

  2. Linux系统中常见的目录名称以及相应内容

    目录名称 应放置文件的内容 /boot 开机所需文件——内核.开机菜单以及所需配置文件等等 /dev 以文件形式存放任何设备与接口 /etc 配置文件 /home 用户家目录 /bin 存放单用户模式 ...

  3. 龙尚3G、4G模块嵌入式Linux系统使用说明【转】

    本文转载自;http://blog.csdn.net/zqixiao_09/article/details/52506812 驱动部分: 1.kernle/drivers/usb/serial/opt ...

  4. Android OTG之USB转串口模块通讯

    微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521 1.背景简介 我们公司开发了一款室内机平板APP应用,要求平板能去控制智能门锁. ...

  5. Linux系统中的硬件问题如何排查?(3)

    Linux系统中的硬件问题如何排查?(3) 2013-03-27 10:32 核子可乐译 51CTO.com 字号:T | T 在Linux系统中,对于硬件故障问题的排查可能是计算机管理领域最棘手的工 ...

  6. Linux系统中“动态库”和“静态库”那点事儿 /etc/ld.so.conf 动态库的后缀为*.so 静态库的后缀为 libxxx.a ldconfig 目录名

    Linux系统中“动态库”和“静态库”那点事儿 /etc/ld.so.conf  动态库的后缀为*.so  静态库的后缀为 libxxx.a   ldconfig   目录名 转载自:http://b ...

  7. Linux系统中“动态库”和“静态库”那点事儿【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-3142046.html 今天我们主要来说说Linux系统下基于动态库(.so)和静态(.a)的程序那些猫腻. ...

  8. LINUX系统中动态链接库的创建与使用

    大家都知道,在WINDOWS系统中有很多的动态链接库(以.DLL为后缀的文件,DLL即Dynamic Link Library).这种动态链接库,和静态函数库不同,它里面的函数并不是执行程序本身的一部 ...

  9. LINUX系统中动态链接库的创建与使用{补充}

    大家都知道,在WINDOWS系统中有很多的动态链接库(以.DLL为后缀的文件,DLL即Dynamic Link Library).这种动态链接库,和静态函数库不同,它里面的函数并不是执行程序本身的一部 ...

随机推荐

  1. php 函数func_get_args()、func_get_arg()与func_num_args()之间的比较

    在PHP的官方文档上的个自定义如下: func_get_args():返回一个包含函数参数列表的数组. func_get_arg():返回指定的参数值. func_num_args():返回调用函数的 ...

  2. MySQL存储过程-->通过游标遍历和异常处理迁移数据到历史表

    -- 大表数据迁移,每天凌晨1点到5点执行,执行间隔时间10分钟,迁移旧数据到历史表. DELIMITER $$ USE `dbx`$$ DROP PROCEDURE IF EXISTS `pro_x ...

  3. Web API 令牌(秘钥是双方约定的,并不在网络连接上传输)

    http://blog.csdn.net/qq289523052/article/details/47750021 秘钥是双方约定的,并不在网络连接上传输 Web API数据传输加密 2015-08- ...

  4. WebApi安全性 使用TOKEN+签名验证 (秘钥是GUID的,私有的,不是雙方的,并不在网络连接上传输)

    转http://www.cnblogs.com/MR-YY/archive/2016/10/18/5972380.html WebApi安全性 使用TOKEN+签名验证   首先问大家一个问题,你在写 ...

  5. 黄聪:bootstrapValidator验证成功,按钮变灰却无法提交的问题

    对于这个坑真心无语! 主要问题是按钮的id和name不能为submit! 改成别的就好了!

  6. synchronized基础

    synchronized 例子 例1,没有同步的时候运行同一个对象的同一个方法的结果: public class TestSyn { public void showMsg() { try { for ...

  7. Bitmap BitmapData

    var sp:Sprite=new Sprite(); sp.graphics.beginFill(0xffccdd); sp.graphics.drawRect(0,0,100,100); sp.g ...

  8. 关于socket阻塞与非阻塞情况下的recv、send、read、write返回值---部分内容可能不确切,待讨论

    1.阻塞模式与非阻塞模式下recv的返回值各代表什么意思?有没有区别?(就我目前了解阻塞与非阻塞recv返回值没有区分,都是 <0:出错,=0:连接关闭,>0接收到数据大小,特别:返回值  ...

  9. [转]SQL数据库查询到的汉字字段是乱码

    使用英文版SQL数据库查询到的汉字字段是乱码的解决方案 2007-12-04 14:55:45 标签:函数 SQL 数据库 乱码 排序规则 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出 ...

  10. 廖雪峰Java3异常处理-1错误处理-4自定义异常

    JDK已有的异常: RuntimeException * NullPointerException * IndexOutOfBoundsException * SecurityException * ...