Page 1 of 1

Delphi Developers Guide...

Posted: Fri Dec 20, 2002 2:15 pm
by Henry
A Delphi Developers guide brought to you by relix... :D

Posted: Fri Dec 20, 2002 3:12 pm
by relix
Introduction:
This source is developped in Delphi 5, but should work for Delphi 3 and above.

The component used for communicating with the com-port is TComPort from Dejan Crnila. You can download it here.

The code assumes that you have a form "TForm1" with a TComPort on it named "ComPort1".

Example 1: Sending simple text to the screen
This example will send the traditional "Hello World!" to the display at com-port 1, at baudrate 19200.

Code: Select all

procedure TForm1.HelloWorld;
begin
 //Initialize settings
 ComPort1.Port := 'COM1';
 ComPort1.BaudRate := br19200;

 //Open the comport
 try
  ComPort1.Open;
 except
  //If there was an error while opening the comport, inform the user 
  //and exit
  ShowMessage('Com-port 1 could not be opened.');
  exit;
 end;

 //Send the string
 ComPort1.WriteStr('Hello World!');

 //Close the comport again
 ComPort1.Close;

end;

Posted: Fri Dec 20, 2002 3:17 pm
by relix
Example 2: clear screen
This example sends the command to clear the screen of the display to the display, when the user clicks Button1. So this code assumes that you have a TButton named "Button1" on the form, and the code should be in the OnClick event-handler. The caption for the button could be "Clear screen".

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
 //This function will clear the screen by sending 'X'
 //to the display after a command-byte (255)

 //Initialize settings
 ComPort1.Port := 'COM1';
 ComPort1.BaudRate := br19200;

 //Open the comport
 try
  ComPort1.Open;
 except
  //If there was an error while opening the comport, inform the user and exit
  ShowMessage('Com-port 1 could not be opened.');
  exit;
 end;


 //Send the command (commands sent to the display are always preceeded
 //by byte 255, the command-byte):
 ComPort1.WriteStr(#255'X');

 //Close the port
 ComPort1.Close;

end;

Posted: Fri Dec 20, 2002 3:46 pm
by relix
Example 3: set backlight on
This example will set the backlight for the display on, infinite. This means that the backlight will not go out after some minutes, but only if they display gets disconnected, powered off or the command is sent to set the backlight off

Code: Select all

procedure TForm1.Button2Click(Sender: TObject);
begin
 //This function lights the backlight

 //Initialize settings
 ComPort1.Port := 'COM1';
 ComPort1.BaudRate := br19200;

 //Open the comport
 try
  ComPort1.Open;
 except
  //If there was an error while opening the comport, inform the user
  //and exit
  ShowMessage('Com-port 1 could not be opened.');
  exit;
 end;


 //Send the command ('B' for backlight on), followed by 0 (zero) for infinite on
 ComPort1.WriteStr(#255'B'#0);

 //To set the backlight off, send 'F' instead of 'B', like this:
 //ComPort1.WriteStr(#255'F');

 //Close the port
 ComPort1.Close;

end;

Posted: Fri Dec 20, 2002 4:46 pm
by relix
Example 4: clear screen, set backlight off and write text on startup
As a fourth example I've created a little application called "Small Foot", which can be used to set the backlight off, clear the screen and/or send some text to the display on startup, without showing any forms. When started with "settings" as a parameter in the commandline, the settings-form is shown in which some settings can be altered. The settings are saved in a inifile called "SmallFootSettings.ini" in the default ini-directory.

Here's a link to the ZIP-file with source code and binary, a total size of 205KB

Here's a screenshot:
Image

Here is the display-related code:

Code: Select all

...

 //Open the comport
 ComPort.Open;

 //If opening the comport didn't work, exit
 if not ComPort.Connected then
 begin
  ShowMessage('Unable to connect to comport ' + ComPort.Port + '.');
  Close;
 end;

 //Now go through all the settings
 if CB_BacklightOff.Checked then
  ComPort.WriteStr(#255'F');

 if CB_ClearScreen.Checked then
  ComPort.WriteStr(#255'X');

 if CB_WriteText.Checked then
  ComPort.WriteStr(Edit_Text.Text);

 //Now close the port again
 ComPort.Close;

...