USB serial ports by serial number

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

USB serial ports by serial number

O'Connor, Daniel-2
Hi,
I have several USB serial ports on a machine and I don't want to rely on attach order to get various programs to talk to the correct serial port.

I wrote the following devd script and shell script to create symlinks from cu.${sernum}/tty.${sernum} to the real device nodes.

I post it here in the hope other people find it useful.

[maarsytest 2:49] ~> cat /usr/local/etc/devd/usbserialsn.conf
attach 100 {
        device-name ".*";
        match "ttyname" ".+";
        match "ugen" ".+";
        match "sernum" ".+";
        action "/usr/local/libexec/usbserialsn attach $device-name $sernum $ttyname";
};

detach 100 {
        device-name ".*";
        action "/usr/local/libexec/usbserialsn detach $device-name";
};
[maarsytest 2:49] ~> cat /usr/local/libexec/usbserialsn
#!/bin/sh

if [ $# -lt 2 ]; then
  echo "Bad usage"
  exit 1
fi

mode=$1
devname=$2
sernum=$3
ttyname=$4

case "$mode" in
  attach)
    if [ $# -ne 4 ]; then
      echo "Bad usage"
      exit 1
    fi
    ln -sf cua${ttyname} /dev/cu.${sernum}
    ln -sf tty${ttyname} /dev/tty.${sernum}
    echo ${sernum} >/var/run/usbserialsn.${devname}
    ;;

  detach)
    if [ $# -ne 2 ]; then
      echo "Bad usage"
      exit 1
    fi
    if [ ! -e /var/run/usbserialsn.${devname} ]; then
      exit 0
    fi
    sernum=$(cat /var/run/usbserialsn.${devname})
    rm -f /dev/cu.${sernum} /dev/tty.${sernum} /var/run/usbserialsn.${devname}
    ;;

  *)
    echo "Unknown mode"
    exit 1
    ;;
esac

It would be a lot simpler if devd reported the same things on detach as it does on attach but I am not sure how difficult that would be to achieve (presumably it would require caching them).

--
Daniel O'Connor
"The nice thing about standards is that there
are so many of them to choose from."
 -- Andrew Tanenbaum


_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

Aleksandr Rybalko-3
Hi,

Looks like you are lucky guy :)
Most of USB-serial devices have "very stable" serial number :)
more than 50% have S/N "0123456789".

So, sometime you change will do it's job.

IMO, better to track hub/port.

Thanks.


чт, 3 жовт. 2019 о 05:56 O'Connor, Daniel <[hidden email]> пише:

> Hi,
> I have several USB serial ports on a machine and I don't want to rely on
> attach order to get various programs to talk to the correct serial port.
>
> I wrote the following devd script and shell script to create symlinks from
> cu.${sernum}/tty.${sernum} to the real device nodes.
>
> I post it here in the hope other people find it useful.
>
> [maarsytest 2:49] ~> cat /usr/local/etc/devd/usbserialsn.conf
> attach 100 {
>         device-name             ".*";
>         match "ttyname"         ".+";
>         match "ugen"            ".+";
>         match "sernum"          ".+";
>         action "/usr/local/libexec/usbserialsn attach $device-name $sernum
> $ttyname";
> };
>
> detach 100 {
>         device-name             ".*";
>         action "/usr/local/libexec/usbserialsn detach $device-name";
> };
> [maarsytest 2:49] ~> cat /usr/local/libexec/usbserialsn
> #!/bin/sh
>
> if [ $# -lt 2 ]; then
>   echo "Bad usage"
>   exit 1
> fi
>
> mode=$1
> devname=$2
> sernum=$3
> ttyname=$4
>
> case "$mode" in
>   attach)
>     if [ $# -ne 4 ]; then
>       echo "Bad usage"
>       exit 1
>     fi
>     ln -sf cua${ttyname} /dev/cu.${sernum}
>     ln -sf tty${ttyname} /dev/tty.${sernum}
>     echo ${sernum} >/var/run/usbserialsn.${devname}
>     ;;
>
>   detach)
>     if [ $# -ne 2 ]; then
>       echo "Bad usage"
>       exit 1
>     fi
>     if [ ! -e /var/run/usbserialsn.${devname} ]; then
>       exit 0
>     fi
>     sernum=$(cat /var/run/usbserialsn.${devname})
>     rm -f /dev/cu.${sernum} /dev/tty.${sernum}
> /var/run/usbserialsn.${devname}
>     ;;
>
>   *)
>     echo "Unknown mode"
>     exit 1
>     ;;
> esac
>
> It would be a lot simpler if devd reported the same things on detach as it
> does on attach but I am not sure how difficult that would be to achieve
> (presumably it would require caching them).
>
> --
> Daniel O'Connor
> "The nice thing about standards is that there
> are so many of them to choose from."
>  -- Andrew Tanenbaum
>
>
> _______________________________________________
> [hidden email] mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-usb
> To unsubscribe, send any mail to "[hidden email]"
>


--
WBW
-------
Rybalko Aleksandr <[hidden email]>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

O'Connor, Daniel-2


> On 3 Oct 2019, at 16:12, Oleksandr Rybalko <[hidden email]> wrote:
> Looks like you are lucky guy :)
> Most of USB-serial devices have "very stable" serial number :)
> more than 50% have S/N "0123456789".

These are either FTDI (or possibly clones..) or a Micropython board so I guess avoiding CH341s is paying off :)

> So, sometime you change will do it's job.

Yes, it should be possible but of course not if the manufacturer has really cheaped out :(

> IMO, better to track hub/port.

No reason you couldn't do that (either instead or as well) with some very basic script modifications.

Perhaps I should have made /dev/usbserialbysn and put symlinks in that.

> Thanks.
>
>
> чт, 3 жовт. 2019 о 05:56 O'Connor, Daniel <[hidden email]> пише:
> Hi,
> I have several USB serial ports on a machine and I don't want to rely on attach order to get various programs to talk to the correct serial port.
>
> I wrote the following devd script and shell script to create symlinks from cu.${sernum}/tty.${sernum} to the real device nodes.
>
> I post it here in the hope other people find it useful.
>
> [maarsytest 2:49] ~> cat /usr/local/etc/devd/usbserialsn.conf
> attach 100 {
>         device-name             ".*";
>         match "ttyname"         ".+";
>         match "ugen"            ".+";
>         match "sernum"          ".+";
>         action "/usr/local/libexec/usbserialsn attach $device-name $sernum $ttyname";
> };
>
> detach 100 {
>         device-name             ".*";
>         action "/usr/local/libexec/usbserialsn detach $device-name";
> };
> [maarsytest 2:49] ~> cat /usr/local/libexec/usbserialsn
> #!/bin/sh
>
> if [ $# -lt 2 ]; then
>   echo "Bad usage"
>   exit 1
> fi
>
> mode=$1
> devname=$2
> sernum=$3
> ttyname=$4
>
> case "$mode" in
>   attach)
>     if [ $# -ne 4 ]; then
>       echo "Bad usage"
>       exit 1
>     fi
>     ln -sf cua${ttyname} /dev/cu.${sernum}
>     ln -sf tty${ttyname} /dev/tty.${sernum}
>     echo ${sernum} >/var/run/usbserialsn.${devname}
>     ;;
>
>   detach)
>     if [ $# -ne 2 ]; then
>       echo "Bad usage"
>       exit 1
>     fi
>     if [ ! -e /var/run/usbserialsn.${devname} ]; then
>       exit 0
>     fi
>     sernum=$(cat /var/run/usbserialsn.${devname})
>     rm -f /dev/cu.${sernum} /dev/tty.${sernum} /var/run/usbserialsn.${devname}
>     ;;
>
>   *)
>     echo "Unknown mode"
>     exit 1
>     ;;
> esac
>
> It would be a lot simpler if devd reported the same things on detach as it does on attach but I am not sure how difficult that would be to achieve (presumably it would require caching them).
>
> --
> Daniel O'Connor
> "The nice thing about standards is that there
> are so many of them to choose from."
>  -- Andrew Tanenbaum
>
>
> _______________________________________________
> [hidden email] mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-usb
> To unsubscribe, send any mail to "[hidden email]"
>
>
> --
> WBW
> -------
> Rybalko Aleksandr <[hidden email]>
>

--
Daniel O'Connor
"The nice thing about standards is that there
are so many of them to choose from."
 -- Andrew Tanenbaum


_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

Hans Petter Selasky-6
On 2019-10-03 08:56, O'Connor, Daniel wrote:
> Most of USB-serial devices have "very stable" serial number:)
> more than 50% have S/N "0123456789".

It is also allowed to have no serial number.

Maybe some kind of "lstty" would do.

-l  -  list all devices
-s  -  match by serial
-v  -  match by vendor
-p  -  match by product
-i  -  match by interface ID
-t  -  type [USB/PCI]

which simply output the tty number you need. Could be an API we could
add to libusb.

--HPS
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

O'Connor, Daniel-2


> On 3 Oct 2019, at 17:01, Hans Petter Selasky <[hidden email]> wrote:
> On 2019-10-03 08:56, O'Connor, Daniel wrote:
>> Most of USB-serial devices have "very stable" serial number:)
>> more than 50% have S/N "0123456789".
>
> It is also allowed to have no serial number.

Yes, that's why I match sernum to '.+' to skip those.

> Maybe some kind of "lstty" would do.
>
> -l  -  list all devices
> -s  -  match by serial
> -v  -  match by vendor
> -p  -  match by product
> -i  -  match by interface ID
> -t  -  type [USB/PCI]
>
> which simply output the tty number you need. Could be an API we could add to libusb.

The problem is you can't modify some program to call a new API a lot of the time but it is usually trivial to change which serial port it's configured to use.

--
Daniel O'Connor
"The nice thing about standards is that there
are so many of them to choose from."
 -- Andrew Tanenbaum


_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

Hans Petter Selasky-6
On 2019-10-03 09:37, O'Connor, Daniel wrote:

>
>
>> On 3 Oct 2019, at 17:01, Hans Petter Selasky <[hidden email]> wrote:
>> On 2019-10-03 08:56, O'Connor, Daniel wrote:
>>> Most of USB-serial devices have "very stable" serial number:)
>>> more than 50% have S/N "0123456789".
>>
>> It is also allowed to have no serial number.
>
> Yes, that's why I match sernum to '.+' to skip those.
>
>> Maybe some kind of "lstty" would do.
>>
>> -l  -  list all devices
>> -s  -  match by serial
>> -v  -  match by vendor
>> -p  -  match by product
>> -i  -  match by interface ID
>> -t  -  type [USB/PCI]
>>
>> which simply output the tty number you need. Could be an API we could add to libusb.
>
> The problem is you can't modify some program to call a new API a lot of the time but it is usually trivial to change which serial port it's configured to use.
>

You don't need to change any program:

cu /dev/cuaU`lstty -s SERIAL`

--HPS

_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

O'Connor, Daniel-2


> On 3 Oct 2019, at 17:41, Hans Petter Selasky <[hidden email]> wrote:
>
> On 2019-10-03 09:37, O'Connor, Daniel wrote:
>>> On 3 Oct 2019, at 17:01, Hans Petter Selasky <[hidden email]> wrote:
>>> On 2019-10-03 08:56, O'Connor, Daniel wrote:
>>>> Most of USB-serial devices have "very stable" serial number:)
>>>> more than 50% have S/N "0123456789".
>>>
>>> It is also allowed to have no serial number.
>> Yes, that's why I match sernum to '.+' to skip those.
>>> Maybe some kind of "lstty" would do.
>>>
>>> -l  -  list all devices
>>> -s  -  match by serial
>>> -v  -  match by vendor
>>> -p  -  match by product
>>> -i  -  match by interface ID
>>> -t  -  type [USB/PCI]
>>>
>>> which simply output the tty number you need. Could be an API we could add to libusb.
>> The problem is you can't modify some program to call a new API a lot of the time but it is usually trivial to change which serial port it's configured to use.
>
> You don't need to change any program:
>
> cu /dev/cuaU`lstty -s SERIAL`

That doesn't work in a lot of cases, eg serial port is in some other configuration file.

Even in rc.conf it would be tricky since that file is read so often.

--
Daniel O'Connor
"The nice thing about standards is that there
are so many of them to choose from."
 -- Andrew Tanenbaum


_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

Hans Petter Selasky-6
On 2019-10-03 10:13, O'Connor, Daniel wrote:

>
>
>> On 3 Oct 2019, at 17:41, Hans Petter Selasky <[hidden email]> wrote:
>>
>> On 2019-10-03 09:37, O'Connor, Daniel wrote:
>>>> On 3 Oct 2019, at 17:01, Hans Petter Selasky <[hidden email]> wrote:
>>>> On 2019-10-03 08:56, O'Connor, Daniel wrote:
>>>>> Most of USB-serial devices have "very stable" serial number:)
>>>>> more than 50% have S/N "0123456789".
>>>>
>>>> It is also allowed to have no serial number.
>>> Yes, that's why I match sernum to '.+' to skip those.
>>>> Maybe some kind of "lstty" would do.
>>>>
>>>> -l  -  list all devices
>>>> -s  -  match by serial
>>>> -v  -  match by vendor
>>>> -p  -  match by product
>>>> -i  -  match by interface ID
>>>> -t  -  type [USB/PCI]
>>>>
>>>> which simply output the tty number you need. Could be an API we could add to libusb.
>>> The problem is you can't modify some program to call a new API a lot of the time but it is usually trivial to change which serial port it's configured to use.
>>
>> You don't need to change any program:
>>
>> cu /dev/cuaU`lstty -s SERIAL`
>
> That doesn't work in a lot of cases, eg serial port is in some other configuration file.
>
> Even in rc.conf it would be tricky since that file is read so often.
>

How about that we build the cuaX unit number by the location, and have a
sysctl for that?

unit = 0;
while (hubdepth--) {
   unit *= numports;
   unit += portno - 1;
}
unit *= maxifaces;
unit += device_get_unit(x);

Typically this should fit into a 32-bit integer, but we could also use a
64-bit integer.

--HPS
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

Hans Petter Selasky-6
Hi,

Can you try this patch:

https://reviews.freebsd.org/D21886

--HPS
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: USB serial ports by serial number

Ed Maste-2
In reply to this post by Aleksandr Rybalko-3
On Thu, 3 Oct 2019 at 02:43, Oleksandr Rybalko <[hidden email]> wrote:
>
> Hi,
>
> Looks like you are lucky guy :)
> Most of USB-serial devices have "very stable" serial number :)
> more than 50% have S/N "0123456789".

cp210x USB-serial devices are ubiquitous and cheap and it's easy to
set the serial number.

I have a trivial port of the 'cp210x-cfg' tool at
https://github.com/emaste/cp210x-cfg.
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-usb
To unsubscribe, send any mail to "[hidden email]"