Go Down

Topic: [TUTORIAL] Arduino Delphi 10.2 and Serial Port (Read 10810 times) previous topic - next topic


Jul 20, 2017, 06:11 am Last Edit: Jul 20, 2017, 06:18 am by Metaconta

Arduino Tutorial Delphi 10.2 and Serial Port. Interface created with Delphi that you can control the serial port to Arduino turning on and off a Led, handle the LCD and receive messages towards the PC.

The Delphi environment with the pascal language is still current and its use in Arduino is growing more and more.

Delphi source code:
Code: [Select]
unit Principal;


  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CPort;

  TForm1 = class(TForm)
    Label_Titulo: TLabel;
    Label_Mensajes: TLabel;
    Button_ON: TButton;
    Button_OFF: TButton;
    Button_COM: TButton;
    Button_Abrir: TButton;
    Memo_Mensajes: TMemo;
    Button_Limpiar: TButton;
    ComPort1: TComPort;
    procedure Button_ONClick(Sender: TObject);
    procedure Button_OFFClick(Sender: TObject);
    procedure Button_COMClick(Sender: TObject);
    procedure Button_AbrirClick(Sender: TObject);
    procedure Button_LimpiarClick(Sender: TObject);
    procedure ComPort1AfterClose(Sender: TObject);
    procedure ComPort1AfterOpen(Sender: TObject);
    procedure ComPort1RxChar(Sender: TObject; Count: Integer);
    { Private declarations }
    { Public declarations }

  Form1: TForm1;


{$R *.dfm}

procedure TForm1.Button_AbrirClick(Sender: TObject);
  // If the port is connected.
  if ComPort1.Connected then
    ComPort1.Close; // Close the port.

    Button_COM.Enabled := True;
    Button_ON.Enabled := False;
    Button_OFF.Enabled := False;

  else  // Otherwise.

    ComPort1.Open;  // Open the port.

    Button_COM.Enabled := False;
    Button_ON.Enabled := True;
    Button_OFF.Enabled := True;

procedure TForm1.Button_COMClick(Sender: TObject);
ComPort1.ShowSetupDialog; // Open the port settings.

procedure TForm1.Button_LimpiarClick(Sender: TObject);
Memo_Mensajes.Clear();  // Clears Memo messages.

procedure TForm1.Button_OFFClick(Sender: TObject);
ComPort1.WriteStr('Luz_OFF'); // Send the command "Light_OFF" to the port.

procedure TForm1.Button_ONClick(Sender: TObject);
ComPort1.WriteStr('Luz_ON');  // Send the command "Light_ON" to the port.

procedure TForm1.ComPort1AfterClose(Sender: TObject);
    if Button_Abrir <> nil then
    Button_Abrir.Caption := 'Abrir';

procedure TForm1.ComPort1AfterOpen(Sender: TObject);
   Button_Abrir.Caption := 'Cerrar';

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
  Str: String;
  // Receives messages from Arduino.
  ComPort1.ReadStr(Str, Count);

  // Displays messages on the screen.
  Memo_Mensajes.Lines.Add( Str );

  // Save the Arduino messages to a txt file.


Arduino Code:
Code: [Select]

// Initialize the library with its indicated pins.
// RS, RW, Enable, D4, D5, D6, D7.
LiquidCrystal lcd(8, NULL, 9, 4, 5, 6, 7);
// LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// Pin 10 para saber que es luz de fondo.
const byte LuzFondo = 10;

const byte Led = 13;   // Declare the variable pin of the Led.
char caracter;
String comando;

void setup()
  pinMode(Led, OUTPUT);  // Initialize the LED pin as output:
  Serial.begin(115200);     // Port serial 115200 baud.
  lcd.begin(16, 2);         // Screen Format.
  lcd.clear();      // Deletes the screen and its upper left position.
  lcd.print("    Arduino     ");

void loop()
     I'm reading character by character what is received by the serial channel
     (While some data arrives there), and I am linking them one after another
     In a chain. In practice, if we use the "Serial monitor" the while loop
     Will end when we press Enter. The delay is convenient not to saturate the
     Channel and that the concatenation is done in an orderly fashion.
  while (Serial.available() > 0)
    caracter = Serial.read();

     Once I already have the chain "finished", I check its value and make it
     The Arduino board reacts accordingly. Here we could do what
     That we would like: if the command is "such", lights a Led, if it is,
     Move a motor ... and so on.

  // If the characters are received and true.
  if (comando.equals("Luz_ON") == true)
    digitalWrite(Led, HIGH); // Turn on the Led 13.
    Serial.write("ON - Led encendido.");    // Send this message to the PC.
    lcd.setCursor(0, 1);
    lcd.print("Luz ON.         "); // Show on LCD.

  if (comando.equals("Luz_OFF") == true)
    digitalWrite(Led, LOW); // Turn off the Led 13.
    Serial.write("OFF - Led apagado. ");  // Send this message to the PC.
    lcd.setCursor(0, 1);
    lcd.print("Luz OFF.        "); // Show on LCD.

  // We cleaned the chain to receive the next command again.
  comando = "";

View video.

See display. (Spanish).

A cordial greeting.


Thank you so much.. It is one of the most helpful Tutorial on the forum..

Go Up