码迷,mamicode.com
首页 > 其他好文 > 详细

串口USB单一映射及重命名

时间:2017-06-16 18:29:57      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:from   parent   pat   mod   hicon   eal   机器   with   命名   

本文针对在开发过程中有时会出现用到多个串口设备,usb端口号会发生变化,如设备的灯指示信号和其他控制器都是ttyUSB* .其序号与控制接入的顺序有关,对于写好的launch每次修改串口连接名很麻烦。或者每次得先插入一个再插一个,才能保证ttyUSB0 ttyUSB1。重启机器后还会变,很不方便。

下面介绍相关的映射和修改

一  单一的串口映射

用lsusb命令查看所有usb的端口信息

wei@wei :~$ lsusb    
    Bus 002 Device 002: ID 0bda:0411 Realtek Semiconductor Corp.    
    Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub   
    Bus 001 Device 004: ID 04f2:b48c Chicony Electronics Co., Ltd    
    Bus 001 Device 009: ID 046d:c31c Logitech, Inc. Keyboard K120   
    Bus 001 Device 010: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light   
    Bus 001 Device 005: ID 0bda:5411 Realtek Semiconductor Corp.    
    Bus 001 Device 008: ID 24ae:1100     
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

 其中ID 10C4:ea60 表示usb的id,这个是芯片制造商设置,一般认为是唯一表示设备的

根据这个信息,就可以简单的写udev规则了:myusb.rulers

sudo gedit /etc/udev/rules.d/myusb.rulers 执行命令弹出此文件,修改相关内容

KERNEL=="ttyUSB*",ATTRS{idVendor}=="10c4",ATTRS{idProduct}=="ea60", MODE:=“0777”, SYMLINK+="myusblight"

这样你的连接串口名就可以写成唯一的名字myusblight了。

保存后 创建生效后重新插拔usb设备。

用 ls -l /dev |grep ttyUSB  命令就可以看到映射结果了

二 usb hub的多端口映射

同一设备连接多个usb串口时 ,两个设备硬件是一样的时 上述办法无法区分,需要做到一一映射 , 我们可以找usb端口的不同来区分,每一台电脑或设备usb口之间编号是不一样的

执行 udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB1 命令后:

Udevadm info starts with the device specified by the devpath and then   
    walks up the chain of parent devices. It prints for every device   
    found, all possible attributes in the udev rules key format.   
    A rule to match, can be composed by the attributes of the device   
    and the attributes from one single parent device.

looking at device ‘/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.2/1-2.2:1.0/ttyUSB1‘:   
        KERNEL=="ttyUSB1"   
        SUBSYSTEM=="usb-serial"   
        DRIVER=="cp210x"   
        ATTR{port_number}=="0"   
       
      looking at parent device ‘/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.2/1-2.2:1.0‘:   
        KERNELS=="1-2.2:1.0"   
        SUBSYSTEMS=="usb"   
        DRIVERS=="cp210x"   
        ATTRS{bInterfaceClass}=="ff"

 

执行命令查看另一个 udevadm info --attribute-walk --path=/sys/bus/usb-serial/devices/ttyUSB0

Udevadm info starts with the device specified by the devpath and then   
    walks up the chain of parent devices. It prints for every device   
    found, all possible attributes in the udev rules key format.   
    A rule to match, can be composed by the attributes of the device   
    and the attributes from one single parent device.   
       
      looking at device ‘/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.1/1-2.1:1.0/ttyUSB0‘:   
        KERNEL=="ttyUSB0"   
        SUBSYSTEM=="usb-serial"   
        DRIVER=="cp210x"   
        ATTR{port_number}=="0"   
       
      looking at parent device ‘/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2.1/1-2.1:1.0‘:   
        KERNELS=="1-2.1:1.0"   
        SUBSYSTEMS=="usb"   
        DRIVERS=="cp210x"   
        ATTRS{bInterfaceClass}=="ff"

 

我们发现KERNELS的号是不一样的 一个2.1一个2.2。

这样我们可以对两个设备进行一一映射了

修改上述myusb.rulers文件内容为

KERNEL=="ttyUSB*", KERNELS=="1-2.1", MODE:="0777",SYMLINK+="myusblight"   
KERNEL=="ttyUSB*", KERNELS=="1-2.2", MODE:="0777",SYMLINK+="myusbvoice"

这样我们可以usb口1-2.1 就是lignt设备  ,1-2.2 就是voice设备,只要固定讲设备查到对应固定的usb口上就可以做到每次重启设备等情况都不会混淆 ,也不用更新代码。便于安装部署。

执行 ls -l /dev  |grep ttyUSB 命令 后

myusblight ->ttyUSB0

myusbvoice ->ttyUSB1

映射成功,重新插拔后也没有问题

串口USB单一映射及重命名

标签:from   parent   pat   mod   hicon   eal   机器   with   命名   

原文地址:http://www.cnblogs.com/wenhuakulv1989/p/7028630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!