USB serial communication with C#, Linux permission question - c#

I am having a serial communication problem on 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. But I suspect that this is an issue with Linux permissions, which is why I post this here.
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:
using System.IO.Ports;
sp = new SerialPort("/dev/ttyACM0", 9600);
sp.Open();
However, the same code in Unity3D at runtime tells me:
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.
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. The Unity3D version is 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. The Unity3D version is 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:
# file: dev/ttyACM0
# owner: root
# group: dialout
user::rw-
user:myname:rw-
group::rw-
mask::rw-
other::---
and the following on 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? 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.
In contrast, when I compile it outside of unity with mono, all the files in /dev are printed.
This confuses me, shouldn't Unity3D also be run with my user ID, and in turn have access to the dialout group permissions?
Unity3D is launched through a launcher application, could this cause it to not be run as my user?

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. 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.
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.
To get around this Unity Hub should be launched with the parameter:
--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

Related

Executing command using SSH.NET on digi 6030dx modem - what are this character sequences starting with left square bracket?

I have some large project that connect to many devices over SSH.NET
Now I have to add support for new modem witch is digi 6030dx.
I added it and I am able to connect with no issues
But when I send some/any command like show config the output is:
[16C[0K
[16C[0Ks
[17C[0Kh
[18C[0Ko
[19C[0Kw
[20C[0K
[20C[0Kc
[21C[0Ko
[22C[0Kn
[23C[0Kf
[24C[0Ki
[25C[0Kg
Commands
-------------------------------------------------------------------------------
config View and modify the configuration
exit Exit the CLI
analyzer Analyzer commands.
cli-legacy Enter the legacy Admin CLI.
cp Copy a file or directory.
help Show CLI editing and navigation commands.
ls List a directory.
mkdir Create a directory.
modem Modem commands.
more View a file.
mv Move a file or directory.
ping Ping a host.
reboot Reboot the system.
rm Remove a file or directory.
scp Copy a file or directory over SSH.
show Show instance statistics.
system System commands.
traceroute Print the route packets trace to network host.
update Update firmware.
dra.wk.0001> ashowconfigdra.wk.0001> ashowconfig
[16C[0K
Does anyone known what are this strange signs / why my command is splited by this and newlines?
It is first device that have this issue and the app support over 200 other with no issues.
I guess some coding issue or something ? putty does not show this signs so probably 'understand' them somehow?
Those are ANSI escape codes.
In general, with SSH, you get these only if your client (library) declares support for terminal emulation.
SSH.NET library does that always, when you use the "shell" channel (SshClient.CreateShell or SshClient.CreateShellStream).
In general (were you connecting to well behaving SSH server), to avoid getting the codes:
Use "exec" channel (use SshClient.RunCommand). SSH.NET does not use terminal emulation on "exec" channel. Though SSH servers on "devices" (contrary to full servers) usually do not implement the "exec" channel. See also What is the difference between exec_command and send with invoke_shell() on Paramiko?
Modify SSH.NET code not to request terminal emulation for the "shell" channel. – Remove SendPseudoTerminalRequest request from Shell.Start implementation.
Though as you are connecting to some "device" and telneting further to another device and the codes probably come from the far device, the question is whether this will in fact have any effect on that device at all. Possibly you won't be able to avoid getting the codes. Lack of terminal emulation on the first SSH connection possibly won't have any effect on the second telnet connection.
In the end, you may have to deal with the codes.
See also How to strip ANSI escape codes from AIX topas command result in C#.
i did for now
this.answer = Regex.Replace(this.answer, #"\r\[\d{0,3}C\[0K", "", RegexOptions.IgnoreCase);
will see if have any negative impact but looks perfect for now :D

SerialPort.GetPortNames() returns incorrect port names

While c# is not my primary programming language, I'm maintaining such a program for a couple of years now. This program connects to a device on a serial port and works from Windows XP up to 8.1. One specific "feature" is that it uses .NET Framework 2.0.
With some users upgrading to Windows 10 we've got complains that the program cannot detect/open the COM port of the device. We have confirmed this on our own test systems with clean Win10 installation.
It turns out that the function SerialPort.GetPortNames() returns incorrect port names and adds 'strange' characters after the port name.
For example:
COM3吀
COM3䡢
COM3゠
Etc. When I refresh the list, every time another character (or two) shows up after the number.
The test code is super straightforward:
string[] portNames = System.IO.Ports.SerialPort.GetPortNames();
log("Available ports:");
foreach (string PortAvailable in portNames)
{
log(PortAvailable);
}
Where the log function mearly adds a line to a standard TextBox on the form:
txtLog.Text += Msg + Environment.NewLine;
This works in every other Windows version.
I've checked the registry and all looks fine there also.
Does anybody has an idea about this?
I'm suspecting that .NET Framework 2.0 is not 100% compatible anymore, although you can enable it in the Windows features and it seems that the program itself runs fine (besides my serial port problem). I'm a bit scared to upgrade to a newer .NET, let alone that we've VisualStudio 2008 for c# (max=.NET 3.5). Note that the program still needs to run on Windows XP also (POS version is still maintained by Microsoft).
ADDED:
I've "upgraded" a test program to .NET 3.5, and still having exactly the same issue.
Next step is to install a new VisualStudio (it appears that it is free nowadays?? Should I check for privacy settings in Studio also? ;-).
ADDED 2:
Installed VisualStudio 2015 and made multiple builds with different .NET framework versions. v2.0 and 3.5 still adding the strange character. But in v4.0 and up this issue seems te be solved! Now to get the original program compiled and working for the newer Framework.
But I find this issue really strange and would expect that this would hit more .NET functions and more programs.
I've seen the strange characters too. My solution is using Regular Expressions to filter out the number portion of the comm port name.
Dim ports As New Devices.Ports
For Each s As String In ports.SerialPortNames
s = Regex.Replace(s, "\D*(\d+)\D*", "$1")
Debug.WriteLine(s)
Next
I've had this exact same problem with USB CDC serial devices, handled by the new rewritten Windows 10 usbser.sys driver.
The garbage characters are often digits, so removing non-digits isn't a reliable way to work around it. For my solution, look at my last post on this thread here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a78b4668-ebb6-46aa-9985-ec41667abdde/ioportsserialportgetportnames-registrykeygetvalue-corruption-with-usbsersys-driver-on-windows?forum=netfxbcl
..there is code there that'll go through the registry, find usbser ports, and return their unmangled names. Beware that it doesn't return all serial ports, just ones provided by that driver. The code works on Windows XP through to 10.
The underlying problem is that the usbser.sys driver creates a registry entry, and on .NET (at least up to 3.5) the GetPortNames() function tries to read those registry keys and gets corrupted data. I've reported this to Microsoft, both via that forum (assuming they read it) and using the built-in Windows 10 beta error reporting tool. Maybe one day there will be a fix.
As you say the program works after enabling the windows feature:
.NET 2.0,3.0,3.5 isn't enabled by default on Windows 8/8.1/10. The files aren't stored on the install media/wim.
It can be enabled with the DISM command from windows update or a local source.
DISM /Online /Enable-Feature /FeatureName:NetFx3 /All
check this out: http://forums.radioreference.com/uniden-tech-support/317887-windows-10-uniden-usb-driver-5.html
For a work around to fix the Win 10 serial port not working caused by strange characters after the port name.
Go in to the Registry with regedit.exe.
Navigate to "HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALC OMM"
Make note of the comm port name.
Append a "0" or any character to the comm port name.
Change the comm port name back to what it was in step 3.
It worked for me.
Baldur
string[] ports = SerialPort.GetPortNames();
for (int i = 0; i<ports.Length;i++)
{
string mystr = ports[i];
if (((mystr[mystr.Length-1]) >= 0x30) & ((mystr[mystr.Length-1]) <= 0x39))
{
}
else
{
mystr = mystr.Remove(mystr.Length-1);
}
ports[i] = mystr;
}

Selenium 2.0 Remote web driver fails to start IEDriver with C#

DesiredCapabilities capabilities = DesiredCapabilities.InternetExplorer();
System.Environment.SetEnvironmentVariable("webdriver.ie.driver", #"C:\\IEDriverServer.exe");
instance = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
I also have the a system variable set, and the IEDriverServer is in the system path. I cannot run the IEDriver from command line (which makes me thinking is there something wrong with path configuration, or some security restriction)
note that the hub and the node are one and the same machine.
when I execute the test I get:
"System.InvalidOperationException:
The path to the driver executable must be set by the webdriver.ie.driver system property; for m....."![enter image description here][1]
I'm running this one one machine both acting as a hub and a node just to make a proof of concept it'll work for me.
There are a number of things I'd question about your approach to what you're trying to accomplish. First, if you're running your C# code on the same machine as the remote Java server (node/hub), why bother? You can easily just use the InternetExplorerDriver class and eliminate the Java server altogether.
Second, setting an environment variable is not the same as setting a Java system property. You can set the system property by using a -D command line flag on the command line with which you launch the Java .jar.
Finally, if you're actually running the Java server on a different machine from the C# code, and are correctly using RemoteWebDriver, bear in mind that you need IEDriverServer.exe on the machine running the Java server, and not the one running your C# code. Furthermore, you need the hub/node to be aware of the path to the executable on that machine, not to the executable where your C# code is running.
I've got it solved, used the appropriate version IEDriver and fixed the PATH and it worked.

How to stream audio from Nao robot?

I would like to create a streaming audio from a Nao to my WPF form.
The only way that i have found is that we can download a recorded sound on Nao over SSH.
But its not a real streaming ...
If anyone has an idea !
Thanks
run this command on your NAO (login via ssh) gst-launch-0.10 pulsesrc ! audioconvert ! vorbisenc ! oggmux ! tcpserversink port=1234
list via VLC: vlc tcp://IP:1234/
You should look at gstreamer that is embedded in the robot. It would be a bit slow, but it'll be real streaming...
For windows:
Download the latest pulseaudio (not the official one) and creat a config.pa file with these two lines:
load-module module-native-protocol-tcp listen=0.0.0.0 auth-anonymous=1
load-module module-waveout
Run pulseaudio on your windows machine first with pulseaudio -F config.pa
On your NAO:
pacmd load-module module-tunnel-sink sink_name=nao server=192.168.1.152
(change this address to your windows one. Also make sure pulseaudio is running on windows otherwise this wont work)
qicli call ALAudioDevice._listOutputs
The previous command will show you the index number of the pulseaudio device you just created. Use this number in the next commands
pacmd set-default-sink 1
qicli call ALAudioDevice._setDefaultOutput 1
Thats it. Now everything comes through your pc speakers. index 0 is NAO's soundcard
For Linux:
Run this on terminal:
pactl load-module module-rtp-recv
On NAO:
pactl load-module module-null-sink sink_name=nao (creates the sink)
qicli call ALAudioDevice._listOutputs (checks the index on sink)
qicli call ALAudioDevice._setDefaultOutput 1
pactl load-module module-rtp-send source=nao.monitor
I find the Linux approach more stable. The TCP one on windows might break after some time.

.chm not working on specific machine

I've this situation: i've buld a .net application that use .chm file as Integrated Help, when user press F1, Help.ShowHelp is invoked like in this example:
Help.ShowHelp(ctrl, HelpNamespace, HelpNavigator.TopicId, GetTopicId(ctrl));
This work like a charm on my machine in Application debug and release mode. But when i try to do the same on other machine (Windows XP, equal my working machine), when user press f1, nothing append, Help File is not opened.
I've done some tests. I'm sure HelpNamespace (string containing chm file path) is correct, i've tried do something more simple:
Help.ShowHelp(ctrl, HelpNamespace);
This work but is not context sensitive. I'm not able tu understend what append on the remote machine and why the seconth example work and first not.
Does anyone have any idea where the problem can be?
I solved this problem. My Machine is Windows XP Service Pack 3, Remote machine is Windows XP with no SP. I ask system administrator to update machine, after i've made new install of my Application and all work fine.
Before ask Administrator to update machine (that in all cases is a good practice, i don't know why a lot sys admin don't do this!) i've do this tests:
on my machine from command line i have emulated the comand probably called from Help.ShowHelp:
hh.exe -mapid 2900 ms-its:C:\Programs\AppFolder\Help.chm
if all work fine this command show chm file with Selected TopicId, this command, on remote machine don't produce anything and this is the reason that convinced me to ask for an update of the system

Categories

Resources