Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to send AT Commands

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Platform(s): S60 3rd Edition, S60 5th Edition.
Platform Security
Signing Required: DevCert
Capabilities: NetworkControl, CommDD
Article
Created: User:Kanu123 (29 Jun 2007)
Last edited: morkki (28 May 2012)
#include <c32comm.h> 
Link Against : c32.lib


TRequestStatus status;
RCommServ commServer;
User::LeaveIfError(commServer.Connect());
 
_LIT(KCsyName, "dataport");
User::LeaveIfError(commServer.LoadCommModule(KCsyName));
 
_LIT(KDataPort, "DATAPORT::0");
 
RComm comm;
User::LeaveIfError(comm.Open(commServer1, port, ECommShared));
 
//Send AT Commands to MODEM.
 
_LIT8(cmd,"AT\r\n");
comm.Write(status, cmd);
User::WaitForRequest(status);
User::After(5000000);
comm.Close();
commServer.Close();




How to Write and Read AT Commands

To pass AT commands to the GSM modem of the mobile device, we need to use the Dataport CSY module and read/write commands on Port 1 of this module.

This port is not available on the emulator so this code will work only on a mobile device.

This code is well tested on Nokia 6600, Nokia 6670 and Nokia 3230.

Header needed:

#include <c32comm.h>

Library needed:

LIBRARY  c32.lib

Capabilities needed:

Capability NetworkControl CommDD
    _LIT(KCsyName,"DATAPORT");
_LIT(KDataPort,"DATAPORT::1");
RCommServ commServer;
RComm comm;
 
// Connect the server
User::LeaveIfError(commServer.Connect());
 
// Load the CSY module
TInt err = commServer.LoadCommModule(KCsyName);
if ( err )
{
//add here needed logging etc.
User::Leave( err );
}
 
// Open the port
err = comm.Open (commServer, KDataPort, ECommShared);
if ( err )
{
//add here needed logging etc.
User::Leave( err );
}
// Write any AT command
_LIT8(KWriteCommand, "AT+CGSN\r\n");
TRequestStatus writeStatus;
comm.Write(writeStatus, KWriteCommand());
User::WaitForRequest(writeStatus);
// Put some delay before reading the response
const TInt KTimeDelay = 1000000;
User::After(KTimeDelay);
 
// Here I am reading the port twice since the first read will be an echo
// i.e the same command will be received in response
 
// Second read will give the actual response, in this case, the IMEI number of
// the device
// Putting in a while loop hangs the application since there is nothing to read
// on the third read, and so the port keeps on waiting for a response
 
TBuf8<1024> des; // Response will be received in this buffer.
// 1024 is the default buffer size of the receive buffer
TInt count = 0;
TRequestStatus readStatus;
while( count < 2 )
{
des.Zero();
comm.ReadOneOrMore(readStatus, des);
User::WaitForRequest(readStatus);
User::LeaveIfError(readStatus.Int());
count++;
User::After(500000);
}
// Close the port and the server
comm.Close();
commServer.Close();

Note

Logic for reading of AT commands can be implemented in a much better manner according to the requirement. Here I have used a makeshift method of counter just to demonstrate the type and number of responses received.


Related Links

This page was last modified on 28 May 2012, at 11:28.
121 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×