【问题标题】:Linux access to the port 'ttyUSB0' is denied via .Net5 even as rootLinux 对端口“ttyUSB0”的访问被拒绝通过 .Net5,即使是 root
【发布时间】:2021-11-25 09:40:45
【问题描述】:
  1. 在 Debian 上,尝试访问 ttyUSB0(使用 FTDI 电缆通过 USB 串行)。
  2. 我的用户组:myuser tty dialout cdrom floppy sudo audio dip video plugdev netdev bluetooth lpadmin scanner

.Net5 代码:

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

串行端口块:<PackageReference Include="System.IO.Ports" Version="5.0.1"/>

使用 Putty 作为我的用户或 root,我可以访问 ttyUSB0。

运行dotnet progname.dll(作为我的用户)和sudo dotnet progname.dll都给了我相同的结果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()

【问题讨论】:

  • 是否需要在设备文件路径的开头加上/dev/才能变成/dev/ttyUSB0?然后您需要检查设备文件的所有者和权限。
  • 谢谢!就是这样。你能把它设为答案吗?

标签: c# linux serial-port debian


【解决方案1】:

作为答案发布,因为它已通过评论内容解决:

是否需要在设备文件路径的开头加上/dev/才能变成/dev/ttyUSB0

然后你需要检查设备文件的所有者和权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2013-11-25
    • 2019-04-30
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多