Page 1 of 1

Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 7:38 am
by jbonde
I have made a touch screen application for yachting where one of the tasks are reading serial input from a GPS via the USB port. Data is displayed in GUI via GUIzero and my python3 script is auto booted with ~/.config/lxsession/LXDE-pi/autostart. So far everything has been fine on a RPi3 but after porting the code to a new RPi4 with Buster the GUI is still fine but serial GPS data is no longer updated. The serial GPS input works if I exit the application and launch it from Thonny instead. It also works if I test with a simple application from command line but no luck if it is loaded via the autostart script.
As an alternative solution I've tested that I can autoboot and load the script in Thonny but it can't be executed automatically so I am looking for alternative ideas - either a command that can run (F5) a script when Thonny is started or another way of autostarting the application.

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 1:16 pm
by Andyroo
I'm assuming the program actually launches but either dies or ends here...

Thonny has a habit of using a virtual environment so it is possible that a Python module is available in Thonny but not in the main OS environment.

I would check your import requirements and see if they exist outside of Thonny as the first step.

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 1:24 pm
by jbonde
The program runs fine after autostart except for the serial reading so I can freely jump to other features I have written. The only difference using RPi4 instead of RPi3 is that it appears like the GPS is not connected. Exiting the application and running it from Thonny instead makes the port come alive. As for import requirements I only use 'import serial' for this specific GPS-feature.

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 1:32 pm
by scruss
Is the serial GPS connected via USB? If you have other USB devices plugged in, maybe the serial GPS is coming up under a different device name under certain circumstances on the Raspberry Pi 4.

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 1:43 pm
by jbonde
Yes, it's connected via USB but I have tried different combinations with keyboard/mouse attached and also tested with the GPS as the only USB-device. In any case I can't figure out why it should make a difference to the two ways of running the same program - either as a Python3 call during boot (disables GPS) or from the Thonny editor (enables GPS).

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 2:44 pm
by Andyroo
You could check using

Code: Select all

lsusb
from the GUI to see if the GPS is present on the USB ports.

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 2:50 pm
by jbonde
It is present and it works fine if activated from Thonny. This is what is registered with lsusb when moving the device from one USB port to another (no other USB devices attached)

pi@rpi4:~ $ lsusb
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@rpi4:~ $ lsusb
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 4:16 pm
by Andyroo
I have not got a 'real' GUI handy at the mo but a check on the Desktop emulation I do have shows that the module pyserial could be either a module or a plug-in...

Can you try opening a terminal session (with Thonny closed):

Code: Select all

sudo pip3 list | grep pyserial
That should show if the module is loaded outside of Thonny by returning the name and its version number. If nothing is shown then its not installed outside of Thonny

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 5:41 pm
by jbonde
Great idea, but closing Thonny and my apllication gives this result when connecting via a terminal session (Putty):
pi@rpi4:~ $ sudo pip3 list | grep pyserial
pyserial 3.4

So it seems like pyserial is installed? I made a small test script that can be executed same way via terminal and it works there. So the only combination not working is when booting and loading my application with autostart...

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 6:46 pm
by Andyroo
Right so we now know it’s your fault :lol: (Sorry)

Do you get anything out of your application at all when you start it automatically?

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 7:07 pm
by jbonde
Thanks! My application works fine GUI-wise when autostarting but without the serial connection. The other features I have programmed works fine so it seems like the USB/serial-port is not activated when running Python3 in a graphics environment.

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 7:17 pm
by Andyroo
Some thoughts:

Are you error trapping the open on the serial port? If so do you notify that it’s failed?

Are you using an event for the read of the serial data and is this impacted by the GUI event wait loop?

Are you sure that you are running the same program in auto start and Thonny?

Is both the autostart and Thonny running the same Python version?

I’m hopping it’s one of the above as I’m getting baffled and have no serial port hardware that I could try here :(

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 7:34 pm
by jbonde
Thanks for the good thoughts. Answers are no, no, yes and maybe. How can I compare Python versions?

Re: Autostart GUI and read serial data

Posted: Thu Sep 05, 2019 7:41 pm
by Andyroo
The Pi only has two versions of Python.

Depending on the OS version you may have slightly different dot versions.

Simplest way to check I can come up with is (no Thonny handy on the iPad):

Code: Select all

import platform
python_version=platform.python_version()
print (python_version)
How are you handling the GUI events and serial reads?

Re: Autostart GUI and read serial data

Posted: Fri Sep 06, 2019 9:03 am
by jbonde
Found a solution thanks to the debugging procedure (Method 2) described here https://learn.sparkfun.com/tutorials/ho ... tartup/all

If opening the application with autostart there is no shell window provided as with the IDE's. Running the app in Thonny and IDLE did show that everything was good to go according to my code, but by using the Method 2 procedure that provides a background shell, I could see that the script didn't like that there was no information from the GPS when starting so in some way it has stalled the USB port in a way that didn't pick up the feed when a signal came through. I did experiment with a .sh script and a delay of 10 seconds but apparently that wasn't enough. After changing the code and applying some 'if' exceptions it waits for a proper serial string from the GPS before updating the display.
So now I lean to a conclusion that running the application from Python3 is more error sensitive than using Thonny og IDLE.

In any case thanks so much for supporting me and for all the good and wise advice I have received so far. I hope this case can be helpful for other projects dependent of autostarting in the GUI environment where I can highly recommend 'Method 2' for testing before the application is deployed.