Categories: GSM | Symbian C++ | Code Examples | How To
This page was last modified 19:52, 23 May 2008.
How to send AT Commands
From Forum Nokia Wiki
#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.
#include <c32comm.h> //Link Against : c32.lib _LIT(KCsyName,"DATAPORT"); _LIT(KDataPort,"DATAPORT::1"); RCommServ commServer; RComm comm; // Connect the server User::LeaveIfError(commServer.Connect()); // Load the CSY module User::LeaveIfError(commServer.LoadCommModule(KCsyName()); // Open the port User::LeaveIfError(comm.Open (commServer, KDataPort, ECommShared)); // 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 User::After(KTimeDelay); const TInt KTimeDelay = 1000000; // 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; while( count < 2 ) { des.Zero(); aCommPort.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
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Bluetooth AT Commands | YYMM | Bluetooth Technology | 0 | 2006-11-22 16:07 |
| Nokia 30 with Linux | jt0203 | Nokia M2M | 2 | 2003-04-09 15:13 |
| SyncML OTA for 7650 tried but no luck | gvmadhav | OMA DM/DS/CP | 2 | 2005-05-25 14:15 |
| setCommandListener() | rooster13 | Mobile Java General | 7 | 2004-06-21 13:42 |
| PC SDK3.0 on a 6230 | ducados123 | PC Suite API and PC Connectivity SDK | 6 | 2006-01-04 15:55 |
