Multiple Serial devices over USB on Raspberry Pi

pdoubleu
2019-04-05
2019-04-09
  • pdoubleu

    pdoubleu - 2019-04-05

    Hi everyone,

    Unfortunately I havent found anything regarding my question using the search function.

    Here is my question:

    I want to connect 4 serial devices to the 4 USB ports on Raspi. All 4 devices are exactely the same (Roboclaw motor controller).
    Using udev rules I am able to give all of them reproducable names, even if I disconnect and reconnect them, so thats all fine.

    To use the serial USB in codesys I know that I need to add the following lines to CODESYSControl_User.cfg:

    [SysCom]
    Linux.Devicefile=/dev/ttyUSB
    portnum:=COM.SysCom.SYS_COMPORT1

    After this I am able to control the devices vial the CAA serial library.

    My question is now if there is a way to assign each Motorcontroller to a specific comport, so that I can decide which motorcontroller to "speak" without sending every command to each motor?

    Thank for any help!

     
  • Anonymous - 2019-04-09

    Originally created by: KevinR

    Hi pdoubleu,

    you can bound the COM-Port Number to the USB-Port, by specifying the USB-port path.
    And then you can use the COM-Port numbers for a single motor, for example COM-Port number 1 for motor 1 and COM-Port number 2 for motor 2 and so on.

    I've got a little instruction for this:

    First let's identify the name of the interface, by using the attribute ATTRS(interface}.
    So we can make sure that only a device, with the correct interface name, is "linked".

    To check the attributes of a plugged-in device, use following code:

     udevadm info --name /dev/ttyUSB0 --attribute-walk
    

    It will show you the information, from parent device to parent device. So the first info should be something like this:

     
     looking at device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/ttyUSB0/tty/ttyUSB0':
        KERNEL=="ttyUSB0"
        SUBSYSTEM=="tty"
        DRIVER==""
    

    So go ahead and look for the attribute "ATTRS{interface}"
    This attribute gives you the name of the plugged-in USB extension.

      looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0':
        KERNELS=="1-1.4:1.0"
        SUBSYSTEMS=="usb"
        DRIVERS=="ftdi_sio"
        ATTRS{authorized}=="1"
        ATTRS{bAlternateSetting}==" 0"
        ATTRS{bInterfaceClass}=="ff"
        ATTRS{bInterfaceNumber}=="00"
        ATTRS{bInterfaceProtocol}=="ff"
        ATTRS{bInterfaceSubClass}=="ff"
        ATTRS{bNumEndpoints}=="02"
        ATTRS{interface}=="<Name of the device>"
        ATTRS{supports_autosuspend}=="1"
    

    Now you can create a udev rule, by editing your udev rule file and add:

    SUBSYSTEM=="tty", KERNEL=="ttyUSB*", KERNELS=="1-1.4:1.0", ATTRS{interface}=="<Name of the device>", SYMLINK+="ttyRS0"
    

    The Parameter is the fixed path of the usb-port.

    So this device will now always have the COM-Port number 1. If you want to add another device and you want to skip Com-Port number 2, you can use following rule:

    1.) Find out the name of the interface again
    2.) Check the USB-Port path.
    3.) add your rule as you want

    SUBSYSTEM=="tty", KERNEL=="ttyUSB*", KERNELS=="1-1.3:1.0", ATTRS{interface}=="<Name of the other device>", SYMLINK+="ttyRS2"
    

    What we also did, is to create a SYMLINK. This will avoid further problems, in case of that you're using different devices, like "ttyAMA" or something else. Because as far as I know, you can specify only one Device file name in the codesyscontrol config file, so you can avoid further troubles.

    In case of that, you have a totally different device, you can also use other devices.
    Example:

    SUBSYSTEM=="tty", KERNEL=="ttyAMA*", KERNELS=="1-1.2:1.0", ATTRS{interface}=="<Name of the ttyAMA device>", SYMLINK+="ttyRS3"
    

    The CODESYSControl_User.cfg should look like this:

    [SysCom]
    Linux.Devicefile=/dev/ttyRS
    

    I think you don't need the last parameter "_:=...". But I'm not sure.

    I hope this little instruction helps you.

    Let me know if I missed something.

    PS: I think this post should be moved to the raspberry pi forum category.

     
  • Anonymous - 2019-04-09

    Originally created by: KevinR

    After writing the instruction, I've checked the forum and have seen, that there is already an instruction available:

    Check this link:
    l viewtopic.php?f=23&t=6247&start=30 l

     

Log in to post a comment.