0

I'm trying to run a service which uses the UART to control some hardware. The UART is using pins 8/10 (GPIO 14/15). raspi-config has been used to disable the login shell over serial and enable the serial port hardware (both under Interface Options -> P6 Serial Port). The hardware identifier for the port is "/dev/serial0".

The code works when I am logged in via SSH. I can start the code from the command line, or I can use systemd and I have no issues with the UART.

However, when the Pi first boots, systemd tries to start the code but the UART fails to communicate. Trying to start the code remotely (something like ssh pi@1.2.3.4 /home/pi/code_to_run) also fails.

When I SSH into the Pi and start the code everything is fine. I can now exit the session and the code will continue to work (either under systemd or starting it via SSH). If I reboot (sudo reboot) the code will continue to work after the restart. However, if I shutdown (sudo shutdown now and turn the power off and on) the code does not work until I SSH in and run it once from the command line.

The status of the service:

pi@MTM24:~ $ sudo systemctl status radar_interface.service
● radar_interface.service - Radar Interface
   Loaded: loaded (/etc/systemd/system/radar_interface.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-12-08 16:33:29 GMT; 2min 10s ago
 Main PID: 359 (radar_inter)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/radar_interface.service
           └─359 /home/pi/radar_interface /home/pi/radar_config.txt
Dec 08 16:33:31 MTM24 radar_interface[359]:          active: T
Dec 08 16:33:31 MTM24 radar_interface[359]:          running: FAILURE
Dec 08 16:33:31 MTM24 radar_interface[359]:       6:

(The last few lines are output from my code, but the "running: FAILURE" is caused by a failure to communicate over UART)

The service file:

pi@MTM24:~ $ sudo systemctl cat radar_interface.service
# /etc/systemd/system/radar_interface.service
[Unit]
Description=Radar Interface
[Service]
Type=simple
ExecStart=/home/pi/radar_interface /home/pi/radar_config.txt
Restart=always
RestartSec=10
KillMode=process
WatchdogSec=5
Nice=-20
[Install]
WantedBy=multi-user.target

Update. Having got some more testing time, the UART is sometimes working on startup, sometimes not. Update The sometimes working/sometimes not working persists across reboot. Shutdown and restart returns the code to a non-working state - I've updated the description above

2
  • When the service startup has failed, then please look at systemctl status your-uart.service and add its output to the question. You can edit it. And please also add the output of systemctl cat your-uart.service. – Ingo Dec 7 '20 at 13:01
  • Thank you @Ingo. I've added the unit file. When I get back to site I'll check the status, but from memory systemd starts the code correctly (no errors), the code just reports that 0 bytes are read from the serial port. – wtw Dec 7 '20 at 15:05
1

There is something strange with your Unit file. Try this Unit file:

[Unit]
Description=Radar Interface
After=multi-user.target

[Service]
User=pi
ExecStart=/home/pi/radar_interface /home/pi/radar_config.txt

[Install]
WantedBy=multi-user.target

Seems the main missing part is the After= statement. It ensures that the complete environment is initialized and running before starting your application.

3
  • Thank you, but this hasn't got it yet. It does feel like this is some hardware startup issue. I've tried just leaving the machine for a few minutes and then doing systemctl restart radar_interface.service but this doesn't get things working either. The state (working or not working) persists over reboots (so if I've logged in and started it once from the command line it will start under systemd after a reboot), but after shutdown and power cycling it goes back to not working. – wtw Dec 9 '20 at 18:19
  • @wtw What exit status do you get from /home/pi/radar_interface if you look with systemctl status radar_interface.service after failing? Does radar_interface emit some error messages to standard or error output (stdout, stderr)? That you will find in the journal (journalctl -b -e). – Ingo Dec 9 '20 at 19:49
  • In journalctl I can see that write(...) is returning 10 bytes written (this is correct) but that serialDataAvail(...) always returns 0 (i.e. no bytes ready to read) and the code eventually times out. There is no exit status - the code keeps on trying to get a good connection. – wtw Dec 10 '20 at 20:31
0

The question is very light on detail.

From what is written I would have to assume the UART is being used for boot messages. That would explain why it seems to fail during boot but seems to work afterwards.

See if UART boot messages can be disabled with raspi-config.

1
  • Thank you. I'm not trying to use the UART for boot messages - this doesn't mean that I've got everything correctly configured. I didn't find any obvious settings in raspi-config about UART boot messages. I checked that the serial shell was disabled, as this seemed close to your suggestion. I also found an option in raspi-config Boot Options -> Console Autologin. Selecting this fixed the problem on the first two machines I tried, but not on the third. – wtw Dec 7 '20 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.