Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Get comm num from handle?

  • 07-06-2011 01:55PM
    #1
    Registered Users, Registered Users 2 Posts: 311 ✭✭


    Hi again folks.

    Is there a way in C (win32, windows.h) of getting the comm port number if I have a handle of said comm port?

    Obviously I already know the comm number (thats how I got the handle in the first place) but I am using multiple ports and dont want to have to go passing the different numbers around along with the handles, especially when its essentially only going to lead to a more concise error messaging.

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    I had a quick look at all the comm related Win32 functions here and didn't see anything suitable.

    A solution would be to create a struct that acts as a context for all communications and store the comm number and other info in there along with the handle.

    You said you were using C, so something like this... But a C++ class or struct would be the same
    typedef struct _CommContext
    {
    	HANDLE commPort;
    	DWORD portNumber;
    	// Other connection related stuff
    } CommContext;
    

    This makes more sense than hitting up windows with an OS call each time you need to query info, and it makes for something a bit easier to maintain down the road.


  • Registered Users, Registered Users 2 Posts: 311 ✭✭wolf99


    Ha I was just stepping through them myself! (am still working on a SerialRx())
    Thanks thats a good idea, effectively creating an object to hold the data for each port.
    Cheers fasty


  • Registered Users, Registered Users 2 Posts: 1,346 ✭✭✭carveone


    It might be possible to use the GetFileInformationByHandle function to return something. It would return something like: "\\\\.\\COM1" if it worked at all!

    Worth a go?!


  • Registered Users, Registered Users 2 Posts: 311 ✭✭wolf99


    thanks carveone, I'll try that tomorrow, first glance looks promising though :)


Advertisement