Home > Software engineering >  Linux access to the port 'ttyUSB0' is denied via .Net5 even as root
Linux access to the port 'ttyUSB0' is denied via .Net5 even as root

Time:10-05

  1. On Debian, trying to access ttyUSB0 (Serial over USB using an FTDI cable).
  2. My user groups: myuser tty dialout cdrom floppy sudo audio dip video plugdev netdev bluetooth lpadmin scanner

the .Net5 code:

System.Diagnostics.Process.Start("whoami");
var serialPort = new SerialPort("ttyUSB0", 115200);
serialPort.Open();

the Serial port nugget: <PackageReference Include="System.IO.Ports" Version="5.0.1"/>

With Putty as either my user or root, I can access ttyUSB0.

Running dotnet progname.dll (as my user) and sudo dotnet progname.dll both give me the same result UnauthorizedAccessException:

**af**
Unhandled exception. System.UnauthorizedAccessException: Access to the port 'ttyUSB0' is denied.
 ---> System.IO.IOException: Unknown error 203
   --- 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()


**root**
Unhandled exception. System.UnauthorizedAccessException: Access to the port 'ttyUSB0' is denied.
 ---> System.IO.IOException: No such file or directory
   --- 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()

CodePudding user response:

Posted as an answer because it was resolved with the commented content:

Is it necessary to add /dev/ to the beginning of the device file path to make it /dev/ttyUSB0?

Then you will need to check the owner and permissions of the device file.

  • Related