Score:0

Systemctl cannot start service which uses ttyACM0 port after reboot

au flag

I need to start my own service (SignalR client) written in C# (.NET6) after ttyACM0 port is available. This service should be started after other service (SignalR server).

The server service starts after reboot normally (without any problem). The client service status gives an information:

● RfidHwSigRClient.service
Loaded: loaded (/etc/systemd/system/RfidHwSigRClient.service;
disabled; vendor preset: enabled)    Active: inactive (dead) since Mon
2023-03-06 22:20:58 CET; 2min 17s ago   Process: 1583
ExecStart=/usr/bin/dotnet
/home/predator/Projects/VSLinuxDbg/RfidHwSigRClient/RfidHwSigRClient.dll
(code=exited, status=0/SUCCESS)   Process: 767 ExecStartPre=/bin/sleep
10 (code=exited, status=0/SUCCESS)  Main PID: 1583 (code=exited,
status=0/SUCCESS)

Mar 06 22:20:46 PredatorClient systemd[1]: Starting
RfidHwSigRClient.service... Mar 06 22:20:57 PredatorClient
dotnet[1583]: Microsoft.Hosting.Lifetime[0] Application started.
Hosting environment: Production; Content root path: / Mar 06 22:20:57
PredatorClient systemd[1]: Started RfidHwSigRClient.service. Mar 06
22:20:58 PredatorClient dotnet[1583]:
Microsoft.Extensions.Hosting.Internal.Host[9] BackgroundService failed
System.UnauthorizedAccessException: Access to the port '/dev/usb_rfid'
is denied.  ---> System.IO.IOException: Device or resource busy    ---
End of inner exception stack trace ---    at
System.IO.Ports.SafeSerialDeviceHandle.Open(String portName)    at
System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate,
Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout,
Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean
rtsEnable, Boolean discardNull, Byte parityReplace)    at
System.IO.Ports.SerialPort.Open()    at
Predator.Hardware.RfIDCard.RfId.Connect() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\Hardware\RFIDCard\RfId.cs:line
159    at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ConnectAsync() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
95    at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ExecuteAsync(CancellationToken
stoppingToken) in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
77    at
Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService
backgroundService) Mar 06 22:20:58 PredatorClient dotnet[1583]:
Microsoft.Extensions.Hosting.Internal.Host[10] The
HostOptions.BackgroundServiceExceptionBehavior is configured to
StopHost. A BackgroundService has thrown an unhandled exception, and
the IHost instance is stopping. To avoid this behavior, configure this
to Ignore; however the BackgroundService will not be restarted.
System.UnauthorizedAccessException: Access to the port '/dev/usb_rfid'
is denied.  ---> System.IO.IOException: Device or resource busy    ---
End of inner exception stack trace ---    at
System.IO.Ports.SafeSerialDeviceHandle.Open(String portName)    at
System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate,
Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout,
Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean
rtsEnable, Boolean discardNull, Byte parityReplace)    at
System.IO.Ports.SerialPort.Open()    at
Predator.Hardware.RfIDCard.RfId.Connect() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\Hardware\RFIDCard\RfId.cs:line
159    at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ConnectAsync() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
95    at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ExecuteAsync(CancellationToken
stoppingToken) in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
77    at
Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService
backgroundService) Mar 06 22:20:58 PredatorClient dotnet[1583]:
Microsoft.Hosting.Lifetime[0] Application is shutting down...

The problem is that the port is not available.

As soon as the Ubuntu is started, I can restart client service (systemctl restart service_name) from command line, and service works properly (port is ready).

I've read several forums concerning how to start start service. Unfortunately, without any success.

My udev rules looks like:

KERNEL=="ttyACM*", ATTRS{manufacturer}=="MOD elektronik*", ATTRS{product}=="MOD RFID reader", MODE="0666", SYMLINK+="usb_rfid", ENV{SYSTEMD_WANTS}+="RfidHwSigRClient.service"

My RfidHwSigRClient.service looks like:

[unit]
Description=RFId Card Reader SignalR Client Service
BindsTo=dev-usb_rfid.device
Requires=MainSigRServer.service
After=dev-usb_rfid.device MainSigRserver.service
StartLimitIntervalSec=300
StartLimitBurst=30

[Service]
User=predator
Group=predator
Type=notify
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/dotnet /home/predator/Projects/VSLinuxDbg/RfidHwSigRClient/RfidHwSigRClient.dll
TimeoutStartSec=30s
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-use

I've also check to start service 10sec later (ExecStartPre=/bin/sleep 10) without any success. User predator belongs to group dialout. Ubuntu is 18.04 LTS.

Please, Does anybody known what I'm doing wrong? Thank you. R.

Chebona avatar
au flag
I've made a wrong copy of text in 99-usb-serial.rules. Also TAG+="systemd" is at the end.
Score:0
au flag

I've had to use sleep aprox. for 15s to start client service. If the sleep is 10s. the port is not ready (Access denied). However, I still don't know to start the client service as soon as both the port and server service are ready.

My temp. configuration of RfidHwSigRClient.service is as follows:

[Unit]
Description=RFId Card Reader SignalR Client Service
BindsTo=dev-usb_rfid.device
After=dev-usb_rfid.device MainSigRServer.service
StartLimitIntervalSec=300
StartLimitBurst=30

[Service]
Type=notify
ExecStartPre=/bin/sleep 15
ExecStart=/usr/bin/dotnet /home/predator/Projects/VSLinuxDbg/RfidHwSigRClient/RfidHwSigRClient.dll
Restart=on-failure
RestartSec=5s
User=predator
Group=predator

[Install]
WantedBy=dev-usb_rfid.device

Udev rule 99-usb-serial.rules is:

KERNEL=="ttyACM*", ATTRS{manufacturer}=="MOD elektronik*", ATTRS{product}=="MOD RFID reader", SYMLINK+="usb_rfid", MODE="0666", ACTION=="add", RUN+="/home/predator/Projects/Scripts/rfid_plugged.sh", TAG+="systemd" ENV{SYSTEMD_WANTS}+="MainSigRServer.service"

Thank you..

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.