串口USB单一映射及重命名

本文针对在开发过程中有时会出现用到多个串口设备,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.rules

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

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.rules文件内容为

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

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

转载于:https://www.cnblogs.com/wenhuakulv1989/p/7028630.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值