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:
unit Principal;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CPort;
type
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
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button_AbrirClick(Sender: TObject);
begin
// If the port is connected.
if ComPort1.Connected then
begin
ComPort1.Close; // Close the port.
Button_COM.Enabled := True;
Button_ON.Enabled := False;
Button_OFF.Enabled := False;
end
else // Otherwise.
begin
ComPort1.Open; // Open the port.
Button_COM.Enabled := False;
Button_ON.Enabled := True;
Button_OFF.Enabled := True;
end;
end;
procedure TForm1.Button_COMClick(Sender: TObject);
begin
ComPort1.ShowSetupDialog; // Open the port settings.
end;
procedure TForm1.Button_LimpiarClick(Sender: TObject);
begin
Memo_Mensajes.Clear(); // Clears Memo messages.
end;
procedure TForm1.Button_OFFClick(Sender: TObject);
begin
ComPort1.WriteStr('Luz_OFF'); // Send the command "Light_OFF" to the port.
end;
procedure TForm1.Button_ONClick(Sender: TObject);
begin
ComPort1.WriteStr('Luz_ON'); // Send the command "Light_ON" to the port.
end;
procedure TForm1.ComPort1AfterClose(Sender: TObject);
begin
if Button_Abrir <> nil then
Button_Abrir.Caption := 'Abrir';
end;
procedure TForm1.ComPort1AfterOpen(Sender: TObject);
begin
Button_Abrir.Caption := 'Cerrar';
end;
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
begin
// 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.
Memo_Mensajes.Lines.SaveToFile('file.txt');
end;
end.
Arduino Code:
#include
// 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 ");
delay(1000);
}
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();
comando.concat(caracter);
delay(10);
}
/*
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 = "";
}
See display. (Spanish).
A cordial greeting.