Download 3GModemConnect_src.zip - 222.27 KB
Download 3GModemConnect_demo.zip - 859.92 KB
IntroductionA wireless 3G modem is a type of modem which connects to a cellular network instead of to the land line telephone system, they allow you to get broadband internet access anywhere you can get a cell-phone signal. Many cellular phones these days have 3G modems built into them.
There has been a lot of great articles that guide you how to write an internet dialer for regular modem (and most of them work for 3G modems too) but some info is missing for ex. signal strength, connection mode (GSM, GPRS, EDEG, HSDPA, etc) and other info which are wireless modem related.
BackgroundAll my work and testing was on a Huawei modem, but things shouldn't change much with other modems. Before we get started you need to successfully install the modem, and you should notice after installing the modem it usually adds three ports: one for the modem and other two for application interface. In my case with a Huawei modem, you should see the following in the device manger
HUAWEI Mobile Connect - 3G Modem
HUAWEI Mobile Connect - 3G Application Interface (COM #)
HUAWEI Mobile Connect - 3G PC UI Interface (COM #)
We are only interested in two ports: the "Huawei Connect - 3G Modem" port which is the modem port where normal connecting and disconnecting commands should be invoked (RAS should do this for us), and the "HUAWEI Mobile Connect - 3G PC UI Interface" port that the modem uses for sending events like signal quality, signal mode etc.
Code DesignI have tried to keep every thing as object oriented as possible so the code can be reusable and easy to maintain, because not all manufactures use the same set of commands. I used factory design pattern to select the correct class to load dynamically depending on the installed modem. An instance of
CDummyModemis first created, then the modem model is identified and the appropriate class is constructed for that modem.
DialingFor dialing, there are two ways: either opening the "Huawei Connect - 3G Modem" port and issue a dial command, or letting RAS do the job for you. I took the easy way and used RAS to handle the dialing.
There are a lot of atricles that talk about RAS functions in details, so I will not go deep in this. Also there are a lot of ready made classes that make using RAS functions easier but I thought of making my own.
To use CMyRas class you will need to create an instance of CMyRas
CMyRAS m_RAS;Then you have to call the
Initializefunction passing a pointer to
CWndthat will be recieving the events from the RAS callback function.
//Error return FALSE;
Initializefunction also retrieves a list of address book entries and their count. You can use
GetEntriesCountto know the number of Address book entries and you can get entries by index by using
Now you are ready to Dial using the function
Dial, by passing the entrie name that you want to dial, the user name, and the password.
To hang up a connection just call
Note: you need the modem to have the correct APN set or else the server will disconnect you.
Communicating with the ModemSending and reserving messages from the modem is straight forward, all you have to do is open the port for "HUAWEI Mobile Connect - 3G PC UI" and send your commands. If you open the modem port, you won't be able to use RAS for dialing.
I have added my own serial port class
CSerial.All I needed is simply read/writing to the serial port. The
CSerialClass has two threads one for reading and one for writing. To use the class make an instance of the class and call
Initialize, which takes a pointer of
CModemclass to forward the received data from the port. Now you will need to open the port by calling
Openfunction will also start reading and writing threads.
When you're done, call
ShutDownto close the port and to end the threads.
Modem EventsModem traffic status
Usually HUAWEI 3G modems send there status to the "HUAWEI Mobile Connect - 3G PC UI". The
DSFLOWRPTmessage informs us about connection status every two seconds. The recieved text on the serial port looks something like this.
^DSFLOWRPT:0000240E,00000000,00000000,00000000000AD023,00000000002FA192,0003E800,0003E800This is an explanation for what the numbers represent
^DSFLOWRPT: N1, N2, N3, N4, N5, N6, N7Using the info that is supplied from the
N1: Connection duration in seconds
N2: measured upload speed
N3: measured download speed
N4: number of sent data
N5: number of received data
N6: connection, supported by the maximum upload speed
N7: connection, supported by a maximum download speed
DSFLOWRPTevent, you can draw a graph showing your connection status (upload/download over time).
Note: this event is only sent when you are connected.
Once there has been a signal level change, your modem will send a
RSSIevent. The RSSI event shows the current signal quality level. Usually it is between 0 to 31.
This table maps the RSSI value with the signal Quality:
List of common 3G commandsSome Commands can be used for querying or setting. To Query you will need to append a “?”. To set you will need to append a “=”. For example to query for a modem for current APN, use the following command:
To set the APN:
|AT||Get the modem's attention|
|ATI||Get manufacturer information|
|AT+CGMI||Get manufacturer information|
|AT+CIMI||Get SIM IMSI number|
|AT+CGSN||Get modem IMEI|
|AT^HWVER||Get hardware version|
|AT^SYSINFO||Get System information|
|AT+CSQ||Get signal strength|
|AT+CGMR||Print firmware version of the modem|
|ATZ||Reset the modem back to default factory settings|
|AT+CFUN||Get/Set operating mode|
|AT^SYSCFG||Get/Set System configuration|
|AT+CUSD||Sending USSD Commands|
Points of InterestWhat really made me do this application, is that I noticed that the application that comes with the modem doesn't give accurate signal quality. There has also been options that the modem supports and there is no way to set them from the application (e.g. force connecting to 3G mode).
Also I didn't like the modem's application GUI. All I needed is a simple clean dialer.
ReferencesA Good reference for AT Commands and general information about 3G modems
3G Modem Wiki
HistoryV0.1 Initial Release
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)