Using Cport with the Serial Communication Ports

Sometimes you need to look back in archives to find the information you want especially when using tools which might not be as popular today.  Here’s an article saved from the archives on a component called Cport.

CPort 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.

It has all sorts of uses and often when you don’t even think you’d require the component.  For all sorts of tasks including remote ones on servers hosted in datacentres.  For example you could use it to re-reroute traffic on an eCommerce or ATC proxy server you could even do this remotely too.

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:

  • Set the cellphone connection settings such as the serial port to connect and the baud rate.
  • Send the AT-Command to the cellphone
  • 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;
s := strCommand + Postfix;

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;
Comm1.ReadStr(Str, Count);
Memo1.Text := Memo1.Text + Str;

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.   It might be a little out of date, last time I tried was when creating a sales video creator system using an old Risc box!

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.

Leave a Reply