0

I've looked over several of these questions for Windows and C# in general. I see some answers, where ManagementObjectSearcher is used, but as far as I know Unity doesn't have System.Management.dll by default, which is okay, because I'd rather use another way if possible (I know you can manually add it, but I'd prefer to use a different method).

This question below is similar to mine, but the answer uses System.Management and as a back-up tests every COM on the SerialPort.GetPortNames() list. I don't like that the back-up test needs to create new SerialPorts every time to test whether a device is connected or not. It just seems inefficient to check every COM port. This is the question and answer:

How can I discover if a device is connected to a specific serial (COM) port?

On Mac/Linux, it's fairly simple to look up a device in /dev/ folder, and find the proper device for both USB and bluetooth and get the proper port name to use when creating a new SerialPort:

string[] ttys = System.IO.Directory.GetFiles("/dev/", "tty.*");
// iterate over list looking for device by name for usb and bluetooth

Is there a similar method in Windows to get a list of current devices connected to a Windows machine and their COM ports, without using System.Management or checking every single COM individually for feedback?

An ideal solution would be to lookup the devices on Windows without using System.Management.

karamazovbros
  • 950
  • 1
  • 11
  • 40
  • There should be a Windows C++ API to do this. You to write need a C++ plugin then use C# to communicate with it. – Programmer Aug 22 '18 at 21:57
  • I'm looking for a method in C# without using plugins, dlls. For instance, the manually com port loop that checks each one individually almost seems like a good alternative if there was a simpler way to check without creating a new SerialPort for every com for testing. – karamazovbros Aug 22 '18 at 22:02
  • You're making accessing hardware on Windows look like a magic. You don't want to use `SerialPort`, you don't want to use `System.Management.dll` and you don't want want make your own plugin. How else do expect to accomplish this? If you don't want to use standard .NET API then make your own with native API (C++). You can enumerate devices with C++ and return then in your Unity app and this is the appropriate way to go based on your comment. – Programmer Aug 22 '18 at 22:09
  • If you provide a C++ answer, I can try that out (I haven't used C++ plugins in Unity before). I guess I thought that maybe there would be a similar method as the Mac/Linux way, like maybe the devices are temporarily stored in some windows folder somewhere on use. – karamazovbros Aug 22 '18 at 22:14
  • See [this](https://mariusbancila.ro/blog/2007/08/02/how-to-detect-available-com-ports/). It shows how to do it in C++. Now, you have to convert that into a plugin. [Here](https://stackoverflow.com/a/50727287/3785314) one example of a C++ and C# plugin. For instruction on how to build a C++ plugin in Unity, see [this](https://stackoverflow.com/questions/49793560/build-c-plugin-for-unity/49795660#49795660) post. – Programmer Aug 23 '18 at 00:21
  • to add any .net assembly to your unity project, you have to copy it to your `Assets/Libraries` folder in your unity editor. then go to: `edit / project settings / player / other settings / optimization / api compatibility level` and set it to: .NET 2.0 – Jinjinov Aug 29 '18 at 15:00

0 Answers0