fasteddy516 / SimplySerial

A windows console application for serial communications.
MIT License
180 stars 22 forks source link

Allow selection of com port from a list of available ports when starting. #8

Open fasteddy516 opened 3 years ago

fasteddy516 commented 3 years ago

I almost always have multiple CircuitPython devices connected, which means I almost always have to run ss.exe with the -c command-line option to specify which device to connect to. If there is more than one com port available, SimplySerial should - by default - display a list of com ports and allow the user to choose which one to connect to.

fasteddy516 commented 3 years ago

Note that the auto-connect functionality will have to work in conjunction with this feature. The ONE option is straight-forward enough. The ANY option should probably display the connection list again if there is still more than one available device.

fasteddy516 commented 3 years ago

It would also be nice if this list would auto-update when new devices are connected. I'm not sure what options are available with regards to "listening" for new com ports. A polling-type operation is certainly possible, but far from ideal.

Pharap commented 3 years ago

I came across your repo because someone I watch recently took an interest in it, and since serial communication is something I have some experience with, I thought I'd drop by to provide some info that you might find useful...

I'm not sure what options are available with regards to "listening" for new com ports.

You may find this old project of mine interesting/useful: https://github.com/Pharap/SimpleSerialCommunication

In particular, this class provides a means of watching for being notified of serial devices being connected and disconnected: https://github.com/Pharap/SimpleSerialCommunication/blob/master/SerialCommunication/DeviceChangeWatcher.cs

And its usage is demonstrated in the main file: https://github.com/Pharap/SimpleSerialCommunication/blob/master/SerialCommunication/Program.cs

This is done using the System.Management namespace, which leverages the 'Windows Management Instrumentation' (WMI) system. via WMI queries (which are essentially a variant of SQL). Honestly, WMI isn't a particularly well designed system, but it does the job.

Another alternative would be to use Win32 calls, but it depends how you feel about using unsafe code and marshalling/interop, and enumerating serial ports using Win32 is a bit more complicated anyway.

fasteddy516 commented 3 years ago

That is excellent information, thank you for sharing! I will likely be looking into this in the very near future, and will see what I can manage. This application is literally the first .NET app I wrote after working through a "C# for beginners" book, and I've done depressingly little C#/.NET since then. The examples in your demo code - particularly with regards to utilizing events/handlers - are greatly appreciated.