Jump to content
Sign in to follow this  
Akshay07

How to detect when a USB modem is connected?

Recommended Posts

Akshay07

Hi all,

I searched the help and the forum and I found lots of info regarding the detection of USB drives or how to communicate with a modem, but nothing that really helped me (unless I missed or misunderstood some information).

I would like to detect automatically when a new modem is connected to the computer via USB, and what is the corresponding COM port. Could someone tell me where to start?

Thank you!

Share this post


Link to post
Share on other sites
Sobiech

Connection mean internet connection or just connected to computer?

Maybe you should look in "_WinNet_" or just use "ping" in cmd ^^


This world is crazy

Share this post


Link to post
Share on other sites
martin

Hi all,

I searched the help and the forum and I found lots of info regarding the detection of USB drives or how to communicate with a modem, but nothing that really helped me (unless I missed or misunderstood some information).

I would like to detect automatically when a new modem is connected to the computer via USB, and what is the corresponding COM port. Could someone tell me where to start?

Thank you!

Maybe the links Ptrex gave here could help, but it isn't simple.

You could also check the available COM ports evry 10 seconds say, and if a new one appears then test it with an AT command to see if it's a modem. This will fail if another application is using it though. My serial coms udf (link in my signature) will let you list the available COM ports.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
jchd

You could also check the available COM ports evry 10 seconds say, and if a new one appears then test it with an AT command to see if it's a modem. This will fail if another application is using it though. My serial coms udf (link in my signature) will let you list the available COM ports.

@Martin,

Isn't it possible, in the case where simple detection is the goal, to monitor the availability of the relevant key in HKEY_LOCAL_MACHINE\HARDWARE\... (forgot where this is enumerated)

@OP,

You may try it at no risk (Read the registry, don't write to it) to see if it works.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
martin

@Martin,

Isn't it possible, in the case where simple detection is the goal, to monitor the availability of the relevant key in HKEY_LOCAL_MACHINE\HARDWARE\... (forgot where this is enumerated)

Yes, the usb devices ever connected are all supposed to be saved in one place but they are not. The following code will give information on most devices which have been connected to the PC, but I don't know at the moment how you tell if a device is currently connected.

$inst = 1
While 1
    $key = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Enum\USBSTOR", $inst)
    If @error = -1 Then ExitLoop
    ConsoleWrite($key & @CRLF)
    $sub = 1
    While 1
    $subkey = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Enum\USBSTOR\" & $key, $sub)
    If @error = -1 Then ExitLoop
    ConsoleWrite(" " & $subkey & @CRLF)
    ConsoleWrite(" DeviceDesc------>" & RegRead("HKLM\SYSTEM\CurrentControlSet\Enum\USBSTOR\" & $key & "\" & $subkey, "DeviceDesc") & @CRLF)
    $sect = StringSplit(RegRead("HKLM\SYSTEM\CurrentControlSet\Enum\USBSTOR\" & $key & "\" & $subkey, "HardwareID"), @LF)
    For $n = 1 To $sect[0]
    ConsoleWrite(" H" & $n & " ------------->" & $sect[$n] & @CRLF)
    Next
    $sub += 1
    WEnd
    $inst += 1
WEnd

My USB to RS232 converter is not listed in the above, it appears in this key

"HKLM\SYSTEM\CurrentControlSet\Services\FTDIBUS"

And nothing seems to change in that key whether the device is plugged in or not.

A useful freeware program that lists all this and more including the connected state is USBDeview.exe by Nirsoft.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
jchd

Yes, the usb devices ever connected are all supposed to be saved in one place but they are not.

A useful freeware program that lists all this and more including the connected state is USBDeview.exe by Nirsoft.

You're right. I thought that USBed serial pseudo-ports were grouped somewhere and it was a bit late or too early for me to try this by the time I posted my answer. Now that my eyes are reopen (are they really?) I found a couple of these hanging around and they dispatch their registry information all around. That's a Validated Genuine Mess that MS allowed for these simple things.

Thanks for sharing the USBDeview.exe link, which could prove very useful some day. Have fun.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
martin

The serial ports are listed in the registry at

HKLM\HARDWARE\DEVICEMAP\SERIALCOMM

This list is udated so that plugging in a usb to serial device will add a COM port to the list and unplugging will remove it.

$inst = 1
While 1
    $key = RegEnumVal("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", $inst);
    If @error = -1 Then ExitLoop
    ConsoleWrite($inst & ' ' & $key & "--->" & RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM",$key ) & @CRLF)
    $inst += 1
WEnd

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
jchd

HKLM\HARDWARE\DEVICEMAP\SERIALCOMM

This list is udated so that plugging in a usb to serial device will add a COM port to the list and unplugging will remove it.

I had typed it in my first answer but decided to try it first. Unfortunately I had no pluggable hardware available, so I removed my suggestion after a search for \Device\Serial0 which lead to issues regarding the _mapping_ (local/remote) of serial ports over Terminal Service sessions. You now confirm this is indeed the place where serial devices have their birth and death.

I hope the initial poster keeps on reading the thread.

Thanks Martin and see you later!


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Akshay07

Thanks to all of you guys, I keep on reading this thread, but I tried mostly in the last days to fully understand how to use Martin's UDF.

Still following up this one :D

Share this post


Link to post
Share on other sites
jchd

Thanks to all of you guys, I keep on reading this thread, but I tried mostly in the last days to fully understand how to use Martin's UDF.

What do you have difficulty understanding? Are you talking about his COM UDF? We can help, his wrapper works fine.

Is the example registry monitoring Martin posted above working for you?

I mean the HKLM\HARDWARE\DEVICEMAP\SERIALCOMM thing. Is you modem's serial port identified under this key?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Akshay07

Thank you jchd and Martin for all your answers!

What do you have difficulty understanding? Are you talking about his COM UDF? We can help, his wrapper works fine.

I know it works very fine, there are enough comments/pages showing this, Martin has followed up on his UDF in an amazing way, and I can see he updated his UDF according to the various comments.

I also saw that he did re-upload his "examples.au3" file very recently (when I tried first to download it, it was not available anymore, but the day after, there was a version "from the same day" available).

The reasons are very simple:

1/ I am absolutely not familiar about the use of serial COM ports in general, so I am trying to understand first how it works before bothering you guys.

2/ I did not have time to concentrate on this (this = retrying it according to your previous comments)

Is the example registry monitoring Martin posted above working for you?

I mean the HKLM\HARDWARE\DEVICEMAP\SERIALCOMM thing. Is you modem's serial port identified under this key?

I wil need some time to look into it. Will come back to you asap, but it might take a few days :D Edited by Akshay07

Share this post


Link to post
Share on other sites
Akshay07

The serial ports are listed in the registry at

HKLM\HARDWARE\DEVICEMAP\SERIALCOMM

This list is udated so that plugging in a usb to serial device will add a COM port to the list and unplugging will remove it.

$inst = 1
While 1
    $key = RegEnumVal("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", $inst);
    If @error = -1 Then ExitLoop
    ConsoleWrite($inst & ' ' & $key & "--->" & RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM",$key ) & @CRLF)
    $inst += 1
WEnd

I tried this and it works, I see new items after connecting a USB Mode.

Actually, I am only interested in the COM value, so I am only interested in RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM",$key ).

Now that I have a way to detect when a new serial COM port is present, I need to figure out how to communicate with it to ensure that it is a modem.

Share this post


Link to post
Share on other sites
jchd

I tried this and it works, I see new items after connecting a USB Mode.

Actually, I am only interested in the COM value, so I am only interested in RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM",$key ).

Now that I have a way to detect when a new serial COM port is present, I need to figure out how to communicate with it to ensure that it is a modem.

Sounds good.

Send it AT commands and watch for valid answers. Example dialog:

at
OK
at&v
ACTIVE PROFILE:
B0 E1 L2 M1 Q0 T V1 W0 X4 &C1 &D2 &K3 &Q5 &R1 &S0 &X0 &Y0
S00:000 S01:000 S02:043 S03:013 S04:010 S05:008 S06:003 S07:090 S08:002 S09:006
S10:014 S11:085 S12:050 S18:000 S25:005 S26:001 S36:007 S38:020 S46:138 S48:007
S95:001

TELEPHONE NUMBERS:
0=  1=
2=  3=

OK
atz
OK

Lookup "AT commands" for generic modems, except if you have to deal with highly exotic modems.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Akshay07

The AT command part should hopefully not be an issue, but the setting up of the communication with the modem might be.

I just tried to run CommgExample.au3 and it fails immediately.

Edited by Akshay07

Share this post


Link to post
Share on other sites
Akshay07

I made a small change regarding the dll path. But after setting up the COM port data, the program ends:

C:\Program Files\AutoIt3\Include\CommMG.au3(149,33) : WARNING: $hDll: possibly used before declaration.

$vDllAns = DllCall($hDll,

C:\Program Files\AutoIt3\Include\CommMG.au3 (273) : ==> Variable used without being declared.:

$vDllAns = DllCall($hDll, 'int', 'SetPort', 'int', $iPort, 'int', $iBaud, 'int', $iBits, 'int', $iPar, 'int', $iStop, 'int', $iFlow, 'int', $RTSMode, 'int', $DTRMode)

$vDllAns = DllCall(^ ERROR

EDIT: My mistake, I incorrectly changed the dll path, both the 1st time and the 2nd time. No more errors.

Edited by Akshay07

Share this post


Link to post
Share on other sites
jchd

Don't hesitate to abuse the help file. The one which comes with AutoIt has been specially tailored to sustain an extremely frequent usage and won't wear up until the next release :D

Seriously, look up the Language topic where you can read that you really should declare any variable before its use. I guess you've corrected that since the error is gone, but keep this good habit.

Keep on learning and best of luck!


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Akshay07

The AutoIt help is always the first place I look when I need something :D I found most of my answers in the help.

I actually used to only look at it, and I was reminded in a friendly way that I should also search the forum.

I'll also look into the language topic.

And thanks for your tips, using AutoIt is a never ending learning process :huggles:

Edited by Akshay07

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×