Luos-io/Pyluos

[BUG] Pyluos is stucked on Linux when automatically searching a Gate

Closed this issue · 17 comments

Describe the bug

Bug on Linux systems (see on Ubuntu 20.04), when using "pyluos-discover" or "pyluos-shell" feature to automatically find a Gate :
Those commands stucks on first tty devices.

A filter should be added to scan only USB devices.

On Ubuntu and many other Linux distributions, a filter should be something like "/dev/ttyUSB*".
But I don't know if "/dev/ttyUSB*" is OK for all Linux distributions in the world...

How to reproduce the bug
Systematic on Ubuntu 20.04 when launching "pyluos-discover" or "pyluos-shell".

Additional context
This issue doesn't concern Windows.
I haven't tested on MacOS. Does this issue also concerns Mac ?

I second this.

But it's a tricky issue as people could develop their own gates that wouldn't necessarily connect to ttyUSB*. For instance, the L4 Gate (Nucleo L4) connects to ttyACM* on Ubuntu 20.04.

To solve this issue on Unix-based OS, I think it would make sense to provide some udev rules for Gates that are officially supported by Luos. These rules could create a symlink to the gate and we wouldn't have to bother with finding the right port everytime (e.g. /dev/luos-gate -> /dev/ttyUSB0).

I use this rule that works well with the L0 Gate (file /etc/udev/rules.d/90-luos-gate.rules):

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", GROUP="dialout", MODE="0666", RUN+="echo LUOS GATE CONNECTED!", SYMLINK+="luos-gate"

Good idea fleborne, your solution seems more generic.
We'll think about this.

Reminder for Luos Team : If needed, we'll have to add the solution in the documentation (FAQ / Troubleshooting chapter).

Hello.

I have a problem mentioned above.

I am using NUCLEO-L476RG Development board and its connected to the Linux Mint.

I have make some changes in NUCLEO-L432KC source code in order to make it possible to use it on the my development board.

I have make udev rules described above, but it doesn't solve the issue on my side.

Could you please lat me know if the issue was solved.

Best Regards,
Narek Manukyan

Hello Narek911,

Sorry, but we haven't solved this issue yet.
I've just made a quick and dirty patch which should solve your problem.

First find pyluos file "discover.py".
For example, mine is here "/usr/local/lib/python3.8/dist-packages/pyluos/tools/discover.py"

You should then edit discover.py and change function "def serial_discover()" like below.
Well, I also found a new bug with discover command. We must now put an "\r\n" to send a command. I have created a new issue here.

def serial_discover():
    serial_hosts = serial_ports()
    available_serial = []
    print("Searching for a gate available")
    for serial_host in serial_hosts:
        if("ttyU" in serial_host):
            print("Testing " + str(serial_host))
            try:
                port=1
                port = serial.Serial(serial_host, 1000000, timeout=0.05)
            except:
                continue
            port.write("{\"discover\": {}}\r\n".encode())
            port.flush()
            for x in range(10):
                state = port.readline()
                if ('gate'.encode() in state):
                    available_serial.append(serial_host)
                    continue

            port.close()
    return available_serial

A cleaner way is just to make this modification :

     port.write("{\"discover\": {}}\r\n".encode())

And to ignore this "if" statement using udev-rules instead :

TO IGNORE        if("ttyU" in serial_host):

I will try it and maybe we will quick deliver a fixed pyluos version.

Dear Jerome.

I have tried out with the code that has been provided.

The development board appears as ttyACM0 on my host machine. So I have changed ttyU to ttyA in the if statement.

Please be aware that the '\n' character was not helpful on my side. The pyluos-discover doesn't discovering the gate when there is '\n' character used in the code.

Best Regards,
Narek

@Narek911,

When you connect your NUCLEO, Linux binds 2 devices :

  • 1 for USB st-link (for example ttyACM0 on your machine, and mine too). This one is used to program your device, debug, etc.
  • 1 USB for data exchange (for example /dev/ttyUSB0). pyluos-shell and pyluos-discover use this one.

I don't know why you can see /dev/ttyUSBx... It seems there is a problem with your device. Have you tried to download a basic software to your NUCLEO-L476RG ? You can surely find one on ST website. If it's working with ST code, but not with your Luos based code, it's probably due to your modifications.

I suggest also you remove "ttyA" from your code.
Can't you see a ttyUSB when you type this command ? ls -ll /dev/ttyU*

Have you made fleborne udev suggestions with the correct idVendor and idProduct ?

Dear Jerome

There is only ttyACM0 in / dev. I tested it with the compiled demo binary available from the ST website.
Could you please share with me the instructions in order to modify the udev rules.

Best Regards,
Narek

Hi @Narek911 ,

It was the same for me, the boards only connected as ttyACM*, no USB. I was able to use both ST-Link and serial communication using ttyACM*, no problem about it.

If you want to make sure your device always connects with the right read/write permissions and potentially link it to a symbolic port (/dev/luos-gate in my case), you case use a udev rule:

To solve this issue on Unix-based OS, I think it would make sense to provide some udev rules for Gates that are officially supported by Luos. These rules could create a symlink to the gate and we wouldn't have to bother with finding the right port everytime (e.g. /dev/luos-gate -> /dev/ttyUSB0).

I use this rule that works well with the L0 Gate (file /etc/udev/rules.d/90-luos-gate.rules):

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", GROUP="dialout", MODE="0666", RUN+="echo LUOS GATE CONNECTED!", SYMLINK+="luos-gate"

Make sure you replace the idVendor and idProduct by what you see in the output of the lsusb command (e.g. https://stackoverflow.com/questions/3279800/how-to-get-usb-vendor-and-product-info-programmatically-on-linux).

Dear Narek911,

To find idVendor and idProduct for your device, enter :

lsusb -vvv

Then search for your device properties.

For example, on my computer I have a Nucleo L4 connected.
Command "lsusb -vvv" returns :

Bus 002 Device 034: ID 0483:374b STMicroelectronics ST-LINK/V2.1
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass          239 Miscellaneous Device
  bDeviceSubClass         2
  bDeviceProtocol         1 Interface Association
  bMaxPacketSize0        64
  idVendor           0x0483 STMicroelectronics   <------------- IdVendor is here
  idProduct          0x374b ST-LINK/V2.1         <------------- IdProduct is here
  bcdDevice            1.00
  iManufacturer           1 STMicroelectronics
  iProduct                2 STM32 STLink
  iSerial                 3 066AFF323535474B43114014
  etc...

Now, create a file in /etc/udev/rules.d/
In a former post, fleborne has created a file named "90-luos-gate.rules".
Edit this file and enter :

SUBSYSTEM=="tty", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", GROUP="dialout", MODE="0666", RUN+="echo LUOS GATE CONNECTED!", SYMLINK+="luos-gate"

I've put Ids 0483 and 374b from lsub output above (without 0x).

To activate the new rules, you have to :

  • Enter a command in a terminal, depending of your Linux ditribution. Maybe "udevadm control --reload-rules" will do the job.
  • Or, if this command doesn't work, reboot your computer ! You will be sure the dev rules have been updated :-)

Then a "ls -ll" shows you :

lrwxrwxrwx 1 root root 7 déc.  24 14:28 /dev/luos-gate -> ttyACM0

As suggested by fleborne, now your gate is mapped on "/dev/luos-gate" and is no more something "/dev/ttyUSB0".
So, if we now modify our "discover.py", it will surely do the job :
Replace :

if ("ttyU" in serial_host)

by :

if ("luos-gate" in serial_host)

It's a hack for the moment but if it works, we will maybe modify pyluos and update the doc.
I hope this will work. I'm sorry to give you this job, but maybe pyluos will be better thanx to you ;-)

Best regards,
Jerome

Fleborne has answered before me, I haven't seen it sorry ^^
I hope you will resolve your problem with our 2 messages

Dear Jerome.

Since this is the end of my working day (the test was being run in parallel with my main project), I will tell you in detail about the results.

As I have mentioned earlier I have using the NUCLEO-L476RG development board as device under test (DUT).
To be able to use DUT, I modified the NUCLEO-L432KC project, which I copied from the Get_started repository.
Here are the modified lines from the
#define PIPE_RX_PIN GPIO_PIN_3
#define PIPE_RX_AF GPIO_AF7_USART2

More details you can find here
https://www.st.com/en/evaluation-tools/nucleo-l476rg.html#cad-resources

After this I have crated the "/etc/udev/rules.d/90-luos-gate.rules" as mentioned above.
I have restarted my computer to update the udev rules.

Here is the content of the "discover.py" file

` import os
import time

from ..io.serial_io import Serial
import serial

def serial_ports():
return Serial.available_hosts()

def serial_discover():
serial_hosts = serial_ports()
available_serial = []
print("Searching for a gate available")
for serial_host in serial_hosts:
if ("luos-gate" in serial_host):
print("Testing " + str(serial_host))
try:
port = serial.Serial(serial_host, 1000000, timeout=0.05)
except:
continue
port.write("{"discover": {}}\r".encode())
port.flush()
for x in range(10):
state = port.readline()
if ('gate'.encode() in state):
available_serial.append(serial_host)
continue

          port.close()
  return available_serial

def main():
gate_list = serial_discover()

  if gate_list:
      print("Available Luos gate on port : " + str(gate_list))
  else:
      print("No gate detected")

if name == 'main':
main()`

Here is the output of the "pyluos-discover"

Searching for a gate available Testing /dev/luos-gate Available Luos gate on port : ['/dev/luos-gate']

I have also checked that the link was created and with the corresponding permissions.

Unfortunately the "pyluos-shell" still hangs on the terminal, and I am unable to use it.

Please be informed that if I use the '\n' character in the "discover.py" the "pyluos-shell" terminates, and "pyluos-discover" exits with "No gate detected" message.

Best Regards.
Narek
Files.tar.gz

Dear Narek911,

Thank you for all these details.

  1. According to ST documentation, your PIPE_RX_PIN seems to be correctly configured.
    I can't tell you about PIPE_RX_AF because my colleagues who deals with USART are on holidays :-) But I will check that if you want.

  2. I should have first ask you which Luos version have you downloaded ?
    Version 2.0.1 (with new delimiter \r\n) has been deployed a week ago. So, if you have an earlier version, you don't need "\n".

Be careful to use the correct version of LuosHal and Pyluos.
For example, in the last Luos release 2.01 here, you need to update your HAL and Pyluos with correct versions. Luos version 2.0.1 works with:

  • LuosHAL version 0.4.1
  • Pyluos version 2.0.2

If you are using platformIO, your current version is in platformio.ini under lib_deps :
lib_deps =
Luos@^2.0.1

Of course, if you want to use v2.0.0, all dependencies are here.

Best regards,
Jerome

Hi Jerome.

I like to inform you that the issue was solved :)

Its seems to be Pyluos version problem.

Today I checked and found out that my system is running with the following criteria.

  • LuosHAL version 0.4.1
  • Pyluos version 2.0.2

I have checkout to the Pyluos version of 2.0.0 and the terminal start to work correctly.

Thank you for the best support.

Best Regards,
Narek
NUCLEO-L476RG.tar.gz

Hi Narek911,

Great ! Santa Claus help us to find the answer :-)

If you have any other questions or problem, do not hesitate to ask us.
We are also curious about your project.

You can contact us on Discord, Slack or Reddit :
http://bit.ly/JoinLuosDiscord
http://bit.ly/JoinLuosSlack
https://www.reddit.com/r/Luos/

So, Luos community will benefit about our conversations and issues.

Best regards,
Jerome

Thanks a lot.
I was glad to participate.
Wish you a Merry Christmas and a Happy New Year.

Best Regards,
Narek

To be closed : this issue is related to Linux and not Pyluos.
Maybe we could add a link in documentation troubleshooting section :
1- Web link : how to configure "rules.d.
2- Example
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", GROUP="dialout", MODE="0666", RUN+="echo LUOS GATE CONNECTED!", SYMLINK+="luos-gate"