Connect to android emulator with C# - c#

I have an .apk package that i want to push it into android emulator and run in emulator and then receive events and debug message to show to user with C# console program.
How should i connect to android emulator for receiving debug message from C# code console?
please help me. Thanks.

This depends on how much detail you want. Do you simply want to view the logcat or get full debug info(like breakpoints and device statistics)?
In the case of the former:
You can use the adb(Android Debug Bridge):
ProcessStartInfo adbStart = new ProcessStartInfo(#"your_path\android-sdk\platform-tools\adb.exe", "-e logcat *:D");
adbStart.UseShellExecute = false;
Process adb = new Process() { StartInfo = adbStart };
adb.Start();
Replace the first parameter in the constructor of adbStart with the path to your adb.exe, found in the platform_tools folder of your Android SDK. The second parameter is console flags. The above code forces the adb to only connect to an emulator using the -e flag and filters to debug messages only using the *:D flag. You should replace the * with a relevant filter for your app(like a package name). Just type adb help in a console for info on the different flags.
Since your program runs in the console, the standard output of the adb SHOULD be directed to your console. If it isn't, use the StreamReader at adb.StandardOutput to read the output of the adb. You can use adb.WaitForExit() to block the thread until adb dies.
In the case of the latter:
The official way to do this is via the Dalvik Debug Monitor(android_sdk\tools\ddms.bat). Since there is no official support for C# in android, the debug monitor is written in Java and so are the libs. I can't find any substitutes, sorry. Java is very similar to C# though, maybe you can jump languages for this particular project?

Related

USB serial communication with C#, Linux permission question

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

UnauthorizedAccessException when trying to open serial port in C#

I'm using .NET 4.5.2. In one of my ViewModels, I need to access serial port COM8, which is connected to my device, a sensor that provides a continuous stream of data. I can access and send commands to the serial port via Tera Term, Windows Command Prompt, and a LabView application, so the port is not occupied by another process - I know that is usually the case with these posts, but I'm very sure that is not the case here. I've tried sending a command to the port via C# using the Process() class, but that does not work, so it seems to be something with C#.
I saw this post: Force a COM port to close, but that does not seem to be the issue either.
Other things I've tried:
Uninstalling the COM8 device via Device Manager.
Running the application in "Release" mode via Visual Studio.
Running Visual Studio in Administrator mode.
Switching to .NET 4.5.2.
And a few other things that I'm not recalling right now. Any advice about this situation would be appreciated! I'm happy to provide any more information you may need.
Code to open the port:
if (port == null)
{
port = new SerialPort(location, baudRate);
port.ReadBufferSize = 16384;
}
if (!port.IsOpen)
{
port.Open();
}
Update:
If I copy the above code into the startup method, it works fine. It just doesn't seem to work where it is in my ViewModel. I thought it was a threading issue, but I was able to call the code in the main thread and it did not work. Hopefully this will get me closer to a solution!
Update 2:
The code above also works fine in the codebehind of my login method, but in no other codebehind. Super confused right now.

Minecraft C# Join server afther launching

Okay I succeeded in creating a custom Minecraft launcher for my server. Everything is working fine but there is one cool thing i would like to add. I think it would be cool if people can instantly join a server upon launching minecraft. Does anyone know if this is possible to do?
Have you seen:
http://minecraft.gamepedia.com/Minecraft_launcher
Specifically the section called
Command Line usage
MinecraftDev.jar (all systems): java -cp Minecraft.jar net.minecraft.bootstrap.Bootstrap <username> <server:port>.
Beginning with the launcher released with Minecraft 1.6.1, there are
other commands for the launcher. These can be seen by running java
-jar Minecraft.jar -help.
this shows you it is possible to launch minecraft telling it to connect to server. you should check out the -help to see the full list of command line arguments

Start Node.js server from a C# Application

A requirement has arisen that I need to start a Node.js server from a C# application, this is as simple as running a server.js script within the Node.js console. However, I'm not entirely certain how exactly to achieve that.
Here's what I've looked into so far:
In the Node.js installation, there's a file called C:\Program Files (x86)\nodejs\nodevars.bat, this is the command prompt window for Node.js. To start the server, I could possibly be using the following steps:
Execute the nodevars.bat file.
SendKeys to the new process console window to start the server.
This approach feels a bit fragile. There's no guarantee that the target user will have their Node.js installation in the same place, also sending keys to a process may not be an ideal solution.
Another method could be:
Write a batch file that executes nodevars.bat.
Execute the batch file from the C# application.
This seems like a better approach. However, the only problem here is that the nodevars.bat opens in a new console window.
So to the question(s), is there a way I can start a node.js server script using functionality built into the node.js installation? Perhaps sending arguments to the node.exe?
If it is to serve multiple users, i.e. as a server, then you can use the os-service package, and install a Windows service. You can then start and stop the service using the standard API.
If you are to start the server as a "single purpose" server, i.e. to serve only the current user, then os-service is the wrong approach. (Typically when using this approach you will specify a unique port for the service to use, which will only be used by your application).
To start a batch file or other Console application, from C#, without showing a console window, use the standard method, but be sure to specify:
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false; // This is important
psi.CreateNoWindow = true; // This is what hides the command window.
psi.FileName = #"c:\Path\to\your\batchfile.cmd";
psi.Arguments = #"-any -arguments -go Here"; // Probably you will pass the port number here
using(var process = Process.Start(psi)){
// Do something with process if you want.
}
There are a few different ones but I recommend the os-service package.

Custom Powershell Host Invoke external program without console window

I'm writing an embedded powershell host in C# (windows application), and if there is an external program called in the pipeline the console window flashes as it's executed. Ping and netstat are examples of this as shown in the following code snippet
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Get-Content test.txt; ls; ping www.google.com");
PowerShellInstance.Invoke();
# ...more code here to print output etc
}
Once the ping command is reached in the pipeline a console window will popup execute the command and return results. I would like to do this without the console window showing. I've searched and found examples of how to deal with this by starting a process for external commands and redirecting output.
I can do that of course, but then how would I test if a command in the pipeline is a call to an external program?
I would really like to be able to handle this generically if possible; meaning that I don't want to test for specific programs (e.g. ping, netstat, net) if I don't have to. The native powershell.exe does this so it's somehow possible.
It turns out that this is expected behavior of invoking console commands. In Windows 7 and above conhost.exe is responsible for handling calls to console programs. In this case, ping and netstat trigger an instance of conhost to be created, it handles the request, returns the results, and then remains open. conhost.exe starting up is what is causing the console flash I'm seeing. The fact that it remains open is why I was only seeing the console window flash once. Once conhost is connected to the calling process all subsequent console commands are handled by that same instance.
I'm marking this as an answer because a new question should be created to address conhost specifically.
Source. Windows Internals Part 1

Categories

Resources