An example: Using CPort Delphi Component to read data from your cellphone

Posted by Daniel - 9,261 Views

averagecoder_article.jpgCPort is a Delphi component created by Dejan Crnila. it provide interfaces for Delphi programmers to use the serial communication ports. If you need to utilize serial modem or any other serial communication devices in your Delphi application, you should use this component.

This short article will give you an example of using CPort Delphi Component to read the raw data returned from a cellular phone as responses to the GSM AT-Command given, such as ATE1 (echo) and AT+CMGL (read sms). We will use Siemens cellphone for this purpose.

In order to be able to read data from the cellphone (with the assumption that you had installed the CPort Delphi Component) you need to do three steps in general:

  1. Set the cellphone connection settings such as the serial port to connect and the baud rate.
  2. Send the AT-Command to the cellphone
  3. Parse the responses

Fortunately CPort has a setup dialog where we can easily set the connection parameters of the cellphone. You can do it by executing the ShowSetupDialog; procedure.

To send the command with CPort you can use WriteStr(const Str:string); method, where Str is the command string to send. Below is an example:

procedure TForm1.SendCommand(strCommand: string; Postfix : Char);
var s : string;
begin
s := strCommand + Postfix;
Comm1.WriteStr(s);
end;

There is a Postfix parameter. According to the GSM AT-Command specification this character is needed to indicate the end of the command. For example to ask the cellphone to return a string data about its model, the complete command string is,

Comm1.WriteStr(‘AT+CGMM’ + Chr(13));

To read the responses with CPort you can use ReadStr(var Str:string; Count:integer); method inside the CPort’s OnRxChar event procedure block, where Str is the string returned and Count is the buffer length. Following is the example:

procedure TForm1.Comm1RxChar(Sender: TObject; Count: Integer);
var Str: String;
begin
Comm1.ReadStr(Str, Count);
Memo1.Text := Memo1.Text + Str;
end;

When you send a command string to the cellphone, CPort will trigger the OnRxChar event procedure. Write your code between that procedure block to handle the data returned by the cellphone. In the above example, the data is stored in the Str variable and then displayed inside the Memo control as string.

To get a better understanding of this article, you can download the AT-CMD Tester project source code. It uses the same code as mentioned above. This code has been tested using the following Siemens cellphones: C35i, C45, ME45 and CX65 with the appropriate data cable for each phone via serial port.

Related Resoursces:

__________________________

Related posts brought to you by Yet Another Related Posts Plugin.

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - January 2008


Leave a Reply


Options for your comment:





Get my Full Feed Here or you can subscribe to one of my category based feeds below:
Coffee Break

Latest Blog Entries

Categories

Neighbours and Friends

Comments - Thanks Guys :)

  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • Marlena Albu: Super Blog, Dude! I am constantly on the watch for new and interesting sites and postings about audio equipment… which is what...
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.
  • Sean: This is a great piece of code and thanks for adding the updates. Sean’s last blog post: Not All Text Message Marketing Is Created...
  • rodhy: Thank for your code, it very usefull for me. best regard.