View Full Version : From Support: Detecting COMM port and attached device names (if any)
ErosOlmi
10-01-2011, 13:41
Issue request at http://www.thinbasic.com/community/project.php?issueid=246
Dear Paul,
this inquiry is more a help request so maybe if you post into forum and not Support area you will get more help. Than if we recognize it is a bug we will move to support.
COMM port can be indicated at runtime so you can always let your user indicate the port on which is needed to work before opening it.
To have more info regarding an installed hardware you can use thinBasic WMI module (Windows Management Instrumentation) that let you query Windows to get info about hardware or many other areas.
Here an example (also attached) from which you can have an idea:
uses "WMI"
uses "OS"
uses "CONSOLE"
dim vData() as string
dim nItems as long
dim Counter as long
dim ComputerName as string value OS_GetComputerName
dim sBuffer as string
'---Ask data to WMI system
sBuffer = WMI_GetData(ComputerName, "", "", "", "Win32_SerialPort", "", "DeviceID, MaxBaudRate, Name" )
'---Parse returned data into single lines
nItems = PARSE( sBuffer, vData(), $crlf)
'---Print lines
for Counter = 1 to nItems
console_writeline vData(Counter)
next
'---Finished
console_writeline "-----------------------------------------------------"
console_writeline "Number of lines: " & nItems
console_writeline "---------------------------Press a key to finish-----"
console_waitkey
More info on WMI and COMM port can be found at: http://msdn.microsoft.com/en-us/library/aa394413(v=vs.85).aspx
Hope this can help
Eros
Michael Clease
11-01-2011, 15:47
I do a slight cheat which seems to work at the moment but WMI is probably a better option.
Here is my whole routine but its only the first three lines that find the ports and organise them into an array. remember to add "\\.\" before the port name as ports over a certain number (which I cant remember) require it, so I add it to them all.
Function GetComPorts(CBHNDL As DWord)
Local N,M As DWord
Local nIdx As DWord
Local Buffer As String
Local Ports() As String
If Registry_PathExists("HKEYLM", "hardware\devicemap\serialcomm") Then
Buffer = Registry_GetAllKeys("HKEYLM", "hardware\devicemap\serialcomm")
Split(Buffer,$CRLF,Ports)
If UBound(Ports) <> UBound(ComPorts) Then
'---To minimize video redraw, better to stop control updates
ListView_BeginUpdate CBHNDL, %LvwDisp1
ListView_DeleteAllItems(CBHNDL, %LvwDisp1)
ReDim ComPorts(UBound(Ports))
For n = 1 To UBound(Ports)
ComPorts(n) = Remain$(Ports(n),"=")
ListView_InsertItem CBHNDL, %LvwDisp1, n, 1, ""
ListView_SetItem CBHNDL, %LvwDisp1, n, 2, ComPorts(n)
ListView_SetCheckState CBHNDL, %LvwDisp1, n, %TRUE
Next
'---To minimize video redraw, better to stop control updates
ListView_EndUpdate CBHNDL, %LvwDisp1
EndIf
EndIf
End Function
Eros;
I tried your code and it gets the three Bluetooth ports on my system, but not the USB-to-Serial bridge (COM9). System is 64-bit Windows 7 - could this be the reason? See attached regedit screen shot.
Rick
ErosOlmi
04-07-2011, 19:30
That is what WMI returns.
Try this great util: WMI Explorer 1.10
Download from http://www.ks-soft.net/hostmon.eng/downpage.htm#utils
Run "wmiexplorer.exe".
It will load all WMI class names
Click on "Win32_SerialPort" and check what this software tells you.
I'm quite sure it will tell you exactly the same as thinBasic script.
Let me know, I'm curious.
Ciao
Eros
Same thing, Eros. It does not show COM9. Both the Registry and Device Manager do show it, however. Strange. Consequence of a 64-bit machine?
Rick
ErosOlmi
04-07-2011, 20:28
Consequence of a 64-bit machine?
Well, not sure 100% but I do not think so.
I think that WMI is a very precise system and maybe that serial port falls into a different WMI class.
I will try to check. I have similar device at work.
ErosOlmi
04-07-2011, 21:44
Google (http://www.google.com/webhp?hl=en&tab=ww#sclient=psy&hl=en&site=webhp&source=hp&q=%2Bwmi+%2Busb+to+%2Bserial+%2Badapter+%2B%22windows+7%22&pbx=1&oq=%2Bwmi+%2Busb+to+%2Bserial+%2Badapter+%2B%22windows+7%22&aq=f&aqi=&aql=f&gs_sm=e&gs_upl=1301l1301l0l1l1l0l0l0l0l246l246l2-1l1&bav=on.2,or.r_gc.r_pw.&fp=c306e119a7d85f91&biw=1664&bih=1019)_ing and BING (http://www.bing.com/search?q=%2Bwmi+%2Busb+to+%2Bserial+%2Badapter+%2B%22windows+7%22&qs=n&sk=&form=QBRE)_ing around seems we are not alone on this problem.
The problem to be in WMI under Windows 7 that does not return virtual serial ports.
Michael Clease
05-07-2011, 10:46
Eros its not just Win7, I tested under XP Pro SP3 lastnight and WMI didnt report my prolific usb to rs232 adapter as a serial port but it did appear in the usb devices but that does directly show the com port number.
Mike C.
Most amusingly, Michael's script DOES show this port, but does not give the numbers for the other ports! See attached screen shot.
Rick
ErosOlmi
05-07-2011, 18:59
Eros its not just Win7, I tested under XP Pro SP3 lastnight and WMI didnt report my prolific usb to rs232 adapter as a serial port but it did appear in the usb devices but that does directly show the com port number.Mike C. For me at the moment is to understand if thinBasic WMI module is working fine or not.For what I can see it is working fine because it seems it is reporting the same data as other tools are doing.otherwise I woul have a bug to fixNow the problem it to see why WMI does not report that kind of device, maybe there are other ways like (for example) interrogating USB classes devices checking details
Michael Clease
06-07-2011, 00:32
@Rick can you export that registry branch so I can have a look.
@Eros I would suspect this is behaviour by design the usb RS232 adapter is after all a USB device so it appearing in the WMI_USBControllerDevice does make sense.
I will do some skiing of the web and see what information turns up.
Michael Clease
06-07-2011, 01:38
Try this
uses "WMI"
Uses "OS"
Uses "CONSOLE"
Dim vData() As String
Dim nItems As Long
Dim Counter As Long
Dim Position As Long
Dim ComputerName As String Value OS_GetComputerName
Dim sBuffer As String
Dim Ports() As String
sBuffer = WMI_GetData(ComputerName, "", "", "", "Win32_PnPEntity", "", "Name" )
'---Parse returned data into single lines
nItems = Parse( sBuffer, vData(), $CRLF)
'---Print lines
For Counter = 1 To nItems
Position = InStr(Ucase$(vData(Counter)),"(COM")
If Position Then
ReDim Preserve Ports(UBound(Ports)+1)
PortS(UBound(Ports)) = Extract$(Position+1,vData(Counter),")")
End If
Next
For Counter = LBound(Ports) To UBound(Ports)
Console_WriteLine Ports(Counter)
Next
2011-07-06 - Test02-01 : Update amended code to look for "(COM" instead of "COMM PORT" this is a better solution.
Michael;
That works!
Registry branch attached per your request.
Rick
Michael Clease
06-07-2011, 23:30
Thanks Rick
I was hoping you would export the serialcomm branch from the registry so I could look at the registry keys to see why the bluetooth ports didnt have numbers.
Mike C.
Michael;
I had attached a screen shot of Regedit to my previous post. Here is the export from Regedit of the branch. I had to zip it for it to be uploadable. The Bluetooth (virtual) ports seem to be the same as the usb-to-serial real one.
Rick
Michael Clease
07-07-2011, 21:53
Hello Rick,
Thanks for the keys, I tried adding the keys (made a fake branch) and the ports numbers seem to be reported fine....v strange.
Ive added the keys and the following script to the zip below can you give it a try and see what answers you get.
Mike C.
Michael;
Not adventurous enough to add records to my registry! The fact that I have a workaround is good enough for me. Doesn't satisfy Eros' desire to know why WMI doesn't work, tho... Many thanks.
Rick