BBC BASIC for Windows
Programming >> Communication and Input/Output >> Identifying a particular COM port
http://bb4w.conforums.com/index.cgi?board=communication&action=display&num=1400182507

Identifying a particular COM port
Post by g3nrw on May 15th, 2014, 7:35pm

I want to communicate with a COM port that talks to a device using the Silicon Labs 210x driver.

For example, on one particular machine, Windows Device Manager says in the "Ports(COM & LPT)" section:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Silicon Labs CP210x USB to UART Bridge (COM5)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is there a reliable way that BB4W can obtain this information programmatically, to identify that this driver communicates through COM5?

--
Ian

Re: Identifying a particular COM port
Post by rtr on May 15th, 2014, 8:30pm

on May 15th, 2014, 7:35pm, g3nrw wrote:
Is there a reliable way that BB4W can obtain this information programmatically, to identify that this driver communicates through COM5?

So you want to know how a BASIC program can obtain the information that Device Manager displays, yes? The likelihood is that you will need to use WMI (Windows Management Instrumentation) for that:

http://msdn.microsoft.com/en-us/library/aa394582.aspx

Specifically, Win32_SerialPort would appear to be the relevant class:

http://msdn.microsoft.com/en-us/library/aa394413.aspx

I can't be sure which member contains the information you are interested in, but a bit of experimentation should provide the answer.

Richard.

Re: Identifying a particular COM port
Post by rtr on May 15th, 2014, 8:48pm

on May 15th, 2014, 8:30pm, Richard Russell wrote:
I can't be sure which member contains the information you are interested in, but a bit of experimentation should provide the answer.

It looks like it is probably the Caption member:

Code:
      INSTALL @lib$+"COMLIB"
      PROC_cominitlcid(1033)

      ON CLOSE PROCcleanup : QUIT
      ON ERROR PROCcleanup : SYS "MessageBox", @hwnd%, REPORT$, 0, 0 : END

      objSWbemLocator% = FN_createobject("WbemScripting.SWbemLocator")

      objWMIService% = FN_getobject(objSWbemLocator%, \
      \                            "ConnectServer(""."", ""root\CIMV2"")")

      objSWbemObjectSet% = FN_getobject(objWMIService%, \
      \                "execQuery(""select * from Win32_SerialPort"")")

      nObjs% = FN_getvalueint(objSWbemObjectSet%, "Count")
      IF nObjs%=0 ERROR 100, "No serial ports present"

      SYS !(!objSWbemObjectSet%+28), objSWbemObjectSet%, ^pEnum%
      IF pEnum%=0 ERROR 100, "SWbemObjectSet::NewEnum failed"

      DIM Variant{(nObjs%) type%, pad%, ldata%, hdata%}

      SYS !(!pEnum%+20), pEnum% : REM IEnumVARIANT::Reset
      FOR i% = 1 TO nObjs%
        SYS !(!pEnum%+12), pEnum%, 1, Variant{(i%)}, ^celtFetched%
        IF celtFetched%<>1 ERROR 100, "IEnumVARIANT::Next failed"
      NEXT i%
      SYS !(!pEnum%+8), pEnum% : REM IEnumVARIANT::Release

      FOR i% = 1 TO nObjs%
        PRINT "Port ";i%;" name is: ";
        dummy% = FN_getvariant(Variant{(i%)}.ldata%, "Caption", _ret{})
        IF _ret.vartype% = 8 THEN
          PRINT _ret.data$
        ELSE
          PRINT "Not available"
        ENDIF
      NEXT i%

      PROCcleanup
      END

      DEF PROCcleanup
      PROC_releaseobject(objSWbemLocator%)
      PROC_comexit
      ENDPROC 

Richard.
Re: Identifying a particular COM port
Post by movr0r0 on Dec 28th, 2014, 10:40am

Hi Richard,

This Win32_SerialPort WMI class support with your code example above works well to enumerate "COMx" ports and getting their actual number 'x', using the "DeviceID" Property.
However, although everything is fine with Win7 (and I guess 8), when using this method on WinXP, enumeration stops at the first available port, without error.
On-line MSDN doc appears to relate only to Vista onwards, and obviously there is no more hint about description or changes of the XP version of this class.

Any idea on how to make it fully work on XP too?

Thanks,
R.
Re: Identifying a particular COM port
Post by rtr2 on Dec 28th, 2014, 12:06pm

on Dec 28th, 2014, 10:40am, movr0r0 wrote:
Any idea on how to make it fully work on XP too?

I fear that if you can't make the WMI method work you will have to fall back to reading the Registry. Do you really need to support XP, given its age and status?

Richard.
Re: Identifying a particular COM port
Post by movr0r0 on Dec 28th, 2014, 3:32pm

Many thanks for your quick reply, Richard!

Supporting XP would be nice; I still have a couple of old laptops -- with real serial ports smiley -- that prefer this aging version...

What's the best way to get Windows version from within bb4w?

R.
Re: Identifying a particular COM port
Post by rtr2 on Dec 28th, 2014, 5:04pm

on Dec 28th, 2014, 3:32pm, movr0r0 wrote:
What's the best way to get Windows version from within bb4w?

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwine.html#winver

Richard.
Re: Identifying a particular COM port
Post by movr0r0 on Dec 28th, 2014, 5:45pm

Many thanks again for your great help, Richard.

R.
Re: Identifying a particular COM port
Post by movr0r0 on Jan 20th, 2015, 09:59am

Hi!

It looks like Windows Win32_SerialPort WMI class miserably fails to detect and list most of virtual serial ports (e.g. over an Ethernet link, such as Tibbo), and more annoyingly USB-to-serial adapters, such as ATEN or Prolific.

Any idea about which method could be used to let Windows (and BBCBasic) nicely list what is reported in the "COM ports and LPT" section of the Device Manager?

Thanks,
R.
Re: Identifying a particular COM port
Post by rtr2 on Jan 20th, 2015, 12:46pm

on Jan 20th, 2015, 09:59am, movr0r0 wrote:
It looks like Windows Win32_SerialPort WMI class miserably fails to detect and list most of virtual serial ports

I don't think you should blame Windows, it's more likely that some 'virtual' serial ports fail to follow the rules when they install themselves. After all, you can pretty much guarantee that all serial ports on a modern PC are 'virtual' so it would be daft if WMI never worked.

Quote:
Any idea about which method could be used to let Windows (and BBCBasic) nicely list what is reported in the "COM ports and LPT" section of the Device Manager?

I answered that before: "I fear that if you can't make the WMI method work you will have to fall back to reading the Registry". But you would be entirely justified in using WMI and simply saying that your software is incompatible with 'non-conforming' virtual port drivers.

Richard.