Recent

Author Topic: Lazarus-Arduino communicatios  (Read 682 times)

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Lazarus-Arduino communicatios
« on: September 12, 2020, 05:55:49 pm »
I try to adapt a program from Internet to achieve a communication between Lazarus and Arduino Uno I miss probably something ,since it does not compile.Can anyone help me ? Thanks
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, vsComPort,Synaser ;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.  
  16.     Label_Titulo: TLabel;
  17.     Label_Mensajes: TLabel;
  18.     Button_ON: TButton;
  19.     Button_OFF: TButton;
  20.     Button_COM: TButton;
  21.     Button_Abrir: TButton;
  22.     Memo_Mensajes: TMemo;
  23.     Button_Limpiar: TButton;
  24.     Edit1: TEdit;
  25.     SetComPort: TButton;
  26.     Label1: TLabel;
  27.     ScrollBar1: TScrollBar;
  28.     vsComPort1: TvsComPort;
  29.     comPort1: TBlockSerial;  //class defined in Synaser http://synapse.ararat.cz/doc/help/
  30.     procedure Button_ONClick(Sender: TObject);
  31.     procedure Button_OFFClick(Sender: TObject);
  32.     procedure Button_COMClick(Sender: TObject);
  33.     procedure Button_AbrirClick(Sender: TObject);
  34.     procedure Button_LimpiarClick(Sender: TObject);
  35.     procedure ComPort1AfterClose(Sender: TObject);
  36.     procedure ComPort1AfterOpen(Sender: TObject);
  37.     procedure ComPort1RxChar(Sender: TObject; Count: Integer);
  38.     procedure ScrollBar1Change(Sender: TObject);
  39.     procedure SetComPortClick(Sender: TObject);
  40.  
  41.   private
  42.  
  43.   public
  44.  
  45.   end;
  46.  
  47. var
  48.   Form1: TForm1;
  49.  
  50. implementation
  51.  
  52. {$R *.lfm}
  53.  
  54. procedure TForm1.Button_AbrirClick(Sender: TObject);
  55. begin
  56.   // Si el puerto está conectado.
  57.   if ComPort1.Connected then
  58.   begin
  59.     ComPort1.Close; // Cierra el puerto.
  60.  
  61.     Button_COM.Enabled := True;
  62.     Button_ON.Enabled := False;
  63.     Button_OFF.Enabled := False;
  64.   end
  65.  
  66.   else  // En caso contrario.
  67.  
  68.   begin
  69.     ComPort1.Open;  // Abre el puerto.
  70.  
  71.     Button_COM.Enabled := False;
  72.     Button_ON.Enabled := True;
  73.     Button_OFF.Enabled := True;
  74.   end;
  75. end;
  76.  
  77.  
  78. procedure TForm1.Button_COMClick(Sender: TObject);
  79. begin
  80. ComPort1.ShowSetupDialog; // Abre la configuración del puerto.
  81. end;
  82.  
  83. procedure TForm1.Button_LimpiarClick(Sender: TObject);
  84. begin
  85. Memo_Mensajes.Clear();  // Limpia los mensajes del Memo.
  86. end;
  87.  
  88. procedure TForm1.Button_OFFClick(Sender: TObject);
  89. begin
  90. ComPort1.WriteStr('Luz_OFF'); // Envía el comando "Luz_OFF" al puerto.
  91. end;
  92.  
  93. procedure TForm1.Button_ONClick(Sender: TObject);
  94. begin
  95. ComPort1.WriteStr('Luz_ON');  // Envía el comando "Luz_ON" al puerto.
  96. end;
  97.  
  98. procedure TForm1.ComPort1AfterClose(Sender: TObject);
  99. begin
  100.     if Button_Abrir <> nil then
  101.     Button_Abrir.Caption := 'Abrir';
  102. end;
  103.  
  104. procedure TForm1.ComPort1AfterOpen(Sender: TObject);
  105. begin
  106.    Button_Abrir.Caption := 'Cerrar';
  107. end;
  108.  
  109. procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
  110. var
  111.   Str: String;
  112. begin
  113.   // Recibe mensajes desde Arduino.
  114.   ComPort1.ReadStr(Str, Count);
  115.  
  116.   // Muestra los mensajes en pantalla.
  117.   Memo_Mensajes.Lines.Add( Str );
  118.  
  119.   // Guarda los mensjes de Arduino en un archivo txt.
  120.   Memo_Mensajes.Lines.SaveToFile('archivo.txt');
  121. end;
  122.  
  123. end.                        
  124.  
Thanks

furious programming

  • Sr. Member
  • ****
  • Posts: 468
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Lazarus-Arduino communicatios
« Reply #1 on: September 12, 2020, 06:28:26 pm »
[…] since it does not compile.Can anyone help me ?

Yes, as long as you provide the error messages. Reading on coffee grounds does not work well with technology.
Lazarus 2.0.10 with FPC 3.2.0 (SVN Revision 63526), Windows XP (all 32-bit)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 621
Re: Lazarus-Arduino communicatios
« Reply #2 on: September 12, 2020, 07:29:50 pm »
Hi, with out any error we can't help you. Also you don't say which OS are you working.

But reading your code, I think you speak spanish as me. So you can visit this blog: https://teisrobotics.org/?p=91
Where you can read about connect a arduino with lazarus.
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Lazarus-Arduino communicatios
« Reply #3 on: September 12, 2020, 08:42:46 pm »
Thank you very much, doubleIcaro.
I have visited the bog you suggester and it is a good solution for my needs. However, I intend to pursue my first approach. I have discovered that the source code I got was lacking the OLD Cport library. I intend to incorporate this unit and adapt the old source code to the new requirements .See what happens

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Lazarus-Arduino communicatios
« Reply #4 on: September 12, 2020, 08:47:12 pm »
Thank you very much furious programming.
I could not provide the error messages since the unit CPortLibrery was missing. I have found a new version and will rework the old code to adapt it to the new unit.
And you are right, so no help is possible.
I work in Windows 10
Thanks to everyone involved

avra

  • Hero Member
  • *****
  • Posts: 2044
    • Additional info
Re: Lazarus-Arduino communicatios
« Reply #5 on: September 12, 2020, 09:45:34 pm »
communication between Lazarus and Arduino Uno
You can take a look at https://wiki.freepascal.org/Arduino
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 863
Re: Lazarus-Arduino communicatios
« Reply #6 on: September 13, 2020, 08:56:02 am »
hello,
if you want to use tcomport you can have a look here
but to adapt your program, you can use TLazserial component ( synaser + dialog form to setup serial port (replace tcomport)) and you can use it under windows and Linux. Tlazserial is in the Lazserial package that you can find in the online package manager.
in attachment a small project  like your project.  Serial status are shown in a statusbar.

Friendly, J.P
« Last Edit: September 13, 2020, 09:22:13 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Lazarus-Arduino communicatios
« Reply #7 on: September 13, 2020, 01:59:43 pm »
Thanks for your advice Jurasic Pork.
I will adapt the communication with Arduino using LazSerial and the example you send me. Unfortunately I am unable tu find the documentation for LazSerial. Perhaps you could give me a hint.
Thanks again

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 863
Re: Lazarus-Arduino communicatios
« Reply #8 on: September 13, 2020, 03:26:41 pm »
sorry  :-X   no documentation.  The example sertest  show how to use the component. for special function you can have a look to
 the TBlockSerial documentation of synaser.

If you have questions about this component you can ask them in this forum.
Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018