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

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

串口USB单一映射及重命名的更多相关文章

  1. 【Java EE 学习 22 上】【文件上传】【目录打散】【文件重命名】

    1.文件上传概述 (1)使用<input type="file">的方式来声明一个文件域. (2)表单提交方式一定要是post方式才行 (3)表单属性enctype 默 ...

  2. typedef 类型重命名 和 #define 宏定义(1)

    http://www.blogjava.net/jasmine214--love/archive/2010/11/29/339307.html 在现实生活中,信息的概念可能是长度,数量和面积等.在C语 ...

  3. bat如何实现图片与名字匹配重命名

    背景:有一批图片按顺序截取,需要按照规定的名称进行重名命. 问题:用批处理怎么实现呢?(公司电脑手动重名时,卡的不要不要的) No1:解决:将规定的名称放入criterion.txt中,将批处理Ren ...

  4. 怎样用bat批量重命名文件夹和文件

    很早以前本人写过重命名文件夹的文章,发现其中稍有不完善的地方,其主要功能在文件夹名前统一加上字符,或者在文件夹名后统一加上字符,有网友反应功能太单一.今天我又仔细研究了一下bat批处理代码,分别能完全 ...

  5. EF中外键重命名,打破原先的约束规则

    本人建议玩code frist的人多用用System.ComponentModel.DataAnnotations和System.ComponentModel.DataAnnotations.Sche ...

  6. centos7中的网卡一致性命名规则、网卡重命名方法

    一致性网络设备命名(Consistent Network Device Naming) 背景介绍: 在centos5的时候,我们习惯了eth0这样的网络设备命名,在centos6发现网络设备变成了em ...

  7. pandas 学习 第7篇:DataFrame - 数据处理(应用、操作索引、重命名、合并)

    DataFrame的这些操作和Series很相似,这里简单介绍一下. 一,应用和应用映射 apply()函数对每个轴应用一个函数,applymap()函数对每个元素应用一个函数: DataFrame. ...

  8. ubuntu 网卡名称重命名

    ubuntu 网卡名称重命名 参考:https://blog.csdn.net/hzj_001/article/details/81587824 biosdevname 和 net.ifnames 两 ...

  9. delphi 文件的操作:重命名、复制、移动、删除

    Delphi 文件的操作:重命名.复制.移动.删除第一种方法: RenameFile('Oldname', 'Newname'); CopyFile(PChar('Oldname'), PChar(' ...

随机推荐

  1. [SpringBoot]Web综合开发-笔记

    Web开发 Json接口开发 @RestController 给类添加 @RestController 即可,默认类中的方法都会以 json 的格式返回. 自定义filter filter作用: 用于 ...

  2. java通过年月得到该月每一天的日期

    public static List<String> getDayByMonth(int yearParam,int monthParam){ List<String> lis ...

  3. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  4. Mysql数据库 (JTree应用)

    package com.databases.jtree; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt ...

  5. mysql开启调试日志general_log开启跟踪日志

    general_log = 1 general_log_file = /tmp/umail_mysql.log 有时候,不清楚程序执行了什么sql语句,但是又要排除错误,找不到原因的情况下, 可以在m ...

  6. 可以用到的XSS跨站语句

    我们常用的测试XSS跨站的语句一般是alert比如: <script>alert(“sex”)</script> <script>alert(/sex/)</ ...

  7. 2019.02.15 codechef Favourite Numbers(二分+数位dp+ac自动机)

    传送门 题意: 给444个整数L,R,K,nL,R,K,nL,R,K,n,和nnn个数字串,L,R,K,数字串大小≤1e18,n≤65L,R,K,数字串大小\le1e18,n\le65L,R,K,数字 ...

  8. drf4 视图与路由组件

    APIView和View的区别 不管是View还是APIView最开始调用的都是as_view() APIView继承了View, 并且执行了View中的as_view()方法,最后把view返回了, ...

  9. JS图片验证码

    !(function(window, document) { var size = 5;//设置验证码长度 function GVerify(options) { //创建一个图形验证码对象,接收op ...

  10. python闭包的详细解析

    一.什么是闭包? 如果一个内嵌函数访问外部嵌套函数作用域的变量,并返回这个函数,则这个函数就是闭包 闭包必须满足三个条件: 1. 必须有一个内嵌函数    2. 内嵌函数必须引用外部嵌套函数中的变量  ...