简体   繁体   English

USB与C#、Linux的串行通信权限问题

[英]USB serial communication with C#, Linux permission question

I am having a serial communication problem on Linux.我在 Linux 上遇到串行通信问题。 I am trying to send and receive information between an arduino and the Unity3D engine which uses C# (mono) to open the serial communication.我正在尝试在 arduino 和使用 C# (单声道)打开串行通信的 Unity3D 引擎之间发送和接收信息。 But I suspect that this is an issue with Linux permissions, which is why I post this here.但我怀疑这是 Linux 权限的问题,这就是我在此处发布此内容的原因。 I already added the user to the dialout group and serial communication is actually working when I compile and run the following C# using mono:我已经将用户添加到拨出组,当我使用 mono 编译和运行以下 C# 时,串行通信实际上正在工作:

using System.IO.Ports;
sp = new SerialPort("/dev/ttyACM0", 9600);
sp.Open();

However, the same code in Unity3D at runtime tells me:但是,运行时 Unity3D 中的相同代码告诉我:

IOException: No such file or directory

The frustrating thing is that this is actually working on another Linux machine and I am having trouble understanding what the difference could be that is causing it not to work on the other.令人沮丧的是,这实际上正在另一台 Linux 机器上运行,我无法理解导致它无法在另一台机器上运行的差异可能是什么。

Here are some differences on both systems:以下是两个系统的一些区别:

The working Linux is Lubuntu 19 with the Unity3D installation inside of /home, which is on the same partition as root.工作的 Linux 是 Lubuntu 19,Unity3D 安装在 /home 内,与 root 在同一分区上。 The Unity3D version is 2019.2. Unity3D 版本为 2019.2。

The non-working system is Linux Mint 19.3 with the Unity3D installation also in /home, but this is a different partition from root.非工作系统是 Linux Mint 19.3,Unity3D 安装也在 /home 中,但这是与 root 不同的分区。 The Unity3D version is 2019.3. Unity3D 版本为 2019.3。

The permissions look slightly different too:权限看起来也略有不同:

crw-rw----+ 1 root dialout 166, 0 mei  1 05:08 /dev/ttyACM0    --> Lubuntu
crw-rw---- 1 root dialout 166, 0 May  1 15:03 /dev/ttyACM0     --> Mint

Also:还:

getfacl /dev/ttyACM0

gives me the following on Lubuntu:在 Lubuntu 上给我以下信息:

# file: dev/ttyACM0
# owner: root
# group: dialout
user::rw-
user:myname:rw-
group::rw-
mask::rw-
other::---

and the following on Linux Mint:以及 Linux Mint 上的以下内容:

# file: dev/ttyACM0
# owner: root
# group: dialout
user::rw-
group::rw-
other::---

Does anyone have any insight into why in one scenario Unity3D can't access /dev/ttyACM0, while in the other one it can?有谁知道为什么在一种情况下 Unity3D 无法访问 /dev/ttyACM0,而在另一种情况下却可以? Or any idea's how I might find out?或者有什么想法可以让我知道吗?

EDIT:编辑:

I played around a bit with the following to check what the program is allowed to read:我玩了一下以下内容来检查程序允许读取的内容:

string[] fileArray = Directory.GetFiles(@"/dev");
    foreach(string s in fileArray)
    {
        Console.WriteLine(s);
    }

And when run inside of Unity3D only a handful of files are recognized, only those with "other" read permissions.当在 Unity3D 内部运行时,只能识别少数文件,只有具有“其他”读取权限的文件。 In contrast, when I compile it outside of unity with mono, all the files in /dev are printed.相反,当我使用 mono 在统一之外编译它时,会打印 /dev 中的所有文件。

This confuses me, shouldn't Unity3D also be run with my user ID, and in turn have access to the dialout group permissions?这让我很困惑,不应该 Unity3D 也使用我的用户 ID 运行,进而访问拨出组权限吗? Unity3D is launched through a launcher application, could this cause it to not be run as my user? Unity3D 是通过启动器应用程序启动的,这会导致它无法以我的用户身份运行吗?

I would double check that the ACM device isnt reconnecting and being given ACM1/2/3 etc instead of 0. I've had the issue that it has timed out, reconnected and the number has incremented, because they are virtual this can happen very quickly and the system doesn't remove the previous devicefile before creating the new one.我会仔细检查 ACM 设备是否没有重新连接并被赋予 ACM1/2/3 等而不是 0。我遇到的问题是它已超时、重新连接并且数量已增加,因为它们是虚拟的,这很可能发生快速并且系统在创建新设备文件之前不会删除以前的设备文件。 If you type dmesg into terminal it will show any re connections and what port they got assigned, running sudo dmesg -c will clear the output as it can be quite verbose.如果您在终端中键入dmesg ,它将显示任何重新连接以及它们分配的端口,运行sudo dmesg -c将清除 output 因为它可能非常冗长。

Hope this helps.希望这可以帮助。 Hardware can be painful to work with sometimes.有时使用硬件可能会很痛苦。

I finally found the culprit.我终于找到了罪魁祸首。 Apparently Unity3D recently changed their launcher (Unity Hub) to a sandbox version using flatpak, and for some reason /dev is blacklisted by default.显然 Unity3D 最近使用 flatpak 将他们的启动器(Unity Hub)更改为沙盒版本,并且由于某种原因 /dev 默认被列入黑名单。 To get around this Unity Hub should be launched with the parameter:要绕过这个 Unity Hub,应该使用以下参数启动:

--device=all

The entire command to get this to work for me is:让它为我工作的整个命令是:

/usr/bin/flatpak run --branch=stable --arch=x86_64 --device=all --command=start-unityhub com.unity.UnityHub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM