* * *

Author Topic: program start normally, then hang and close, Z connection  (Read 1351 times)

Kevinn33

  • New member
  • *
  • Posts: 26
program start normally, then hang and close, Z connection
« on: May 12, 2018, 06:14:06 pm »
Hello !, in my project i use Zeos access to access MySQL. I 've comfirmed that it use only 1 Zconnection component and 1 Zquery.
the problem is after i compile and run, it was successfully run at a time. Then it Hang and close by it self. later the error appear.

it says about too many connection..
i'll post it here

the code is too long to put at here. but i can later upload it to here if someone want to help abit  :)
 thanks before

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: program start normally, then hang and close, Z connection
« Reply #1 on: May 12, 2018, 06:38:29 pm »
It looks that you actually make too many connections. Everything else can be just random. So check your connection(s) and keep it open if it mean just one.
That said: Zeos is in my opinion notoriously unreliable and unstable, contrary to its fans on this forum. It may be a bug in Zeos and not in your own code, but to decide that we really need some example code to reproduce it. I would not use Zeos in any production code as it stands. But it is nice in principle. It should have been mature.
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Kevinn33

  • New member
  • *
  • Posts: 26
Re: program start normally, then hang and close, Z connection
« Reply #2 on: May 12, 2018, 06:45:54 pm »
hmm i've tried to use as least as ican in using zconnection. everytime i open the connection, i close it right after get the data,

Code: Pascal  [Select]
  1. unit Sewa;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, LR_Class, LR_BarC, Forms, Controls, Graphics,
  9.   Dialogs, StdCtrls, ExtCtrls, Buttons, Process, db, Printers, Menus, ComCtrls,
  10.   ZConnection, ZDataset;
  11.  
  12. type
  13.  
  14.   { TFSewaan }
  15.  
  16.   TFSewaan = class(TForm)
  17.     Bbayar: TButton;
  18.     Bhome: TButton;
  19.     Biaya: TLabel;
  20.     Button1: TButton;
  21.     Button2: TButton;
  22.     Button3: TButton;
  23.     Button4: TButton;
  24.     Button5: TButton;
  25.     Button6: TButton;
  26.     cekkode: TLabel;
  27.     DateTimeSewa: TLabel;
  28.     frBarCodeObject1: TfrBarCodeObject;
  29.     frReport1: TfrReport;
  30.     Info: TLabel;
  31.     Komfirmasi: TButton;
  32.     Label1: TLabel;
  33.     Panel1: TPanel;
  34.     Panel2: TPanel;
  35.     Panel3: TPanel;
  36.     Panel4: TPanel;
  37.     Panel5: TPanel;
  38.     Shape1: TShape;
  39.     Shape2: TShape;
  40.     Shape3: TShape;
  41.     Shape4: TShape;
  42.     Shape5: TShape;
  43.     Timer1: TTimer;
  44.     Timer2: TTimer;
  45.     Timer3: TTimer;
  46.     ZConnection1: TZConnection;
  47.     ZQuery1: TZQuery;
  48.  
  49.     procedure BbayarClick(Sender: TObject);
  50.     procedure BhomeClick(Sender: TObject);
  51.     procedure Button1Click(Sender: TObject);
  52.     procedure Button2Click(Sender: TObject);
  53.     procedure Button3Click(Sender: TObject);
  54.     procedure Button4Click(Sender: TObject);
  55.     procedure Button5Click(Sender: TObject);
  56.     procedure Button6Click(Sender: TObject);
  57.     procedure FormCreate(Sender: TObject);
  58.     procedure KomfirmasiClick(Sender: TObject);
  59.     procedure Printsaja;
  60.     procedure BacaRFID;
  61.     procedure CLoseDialog;
  62.     procedure Timer2Timer(Sender: TObject);
  63.     procedure Timer3Timer(Sender: TObject);
  64.     procedure insertdata(nomorloker:string);
  65.   private
  66.     sc: Integer;
  67.     flagbayar: Boolean;
  68.     terpilihloker :Char;
  69.   public
  70.     data1,data2,data3,data4,data5 :Integer;
  71.     caridata:integer;
  72.     barkode: string;
  73.     flagterbayar: boolean;
  74.     procedure findbarkode(bar:string);
  75.     procedure tutupstatusloker(kolombarkode:string);
  76.     procedure Tombolkomfirmasi(uploadbarkode:string);
  77.     procedure cekloker;
  78.   end;
  79.  
  80. const
  81.   BUF_SIZE = 2048;
  82. var
  83.   FSewaan: TFSewaan;
  84.   AProcess: TProcess;
  85.   OutputStream: Tstream;
  86.   BytesRead : longint;
  87.   Buffer : array[1..BUF_SIZE] of byte;
  88.  
  89. implementation
  90. uses
  91.   MainGUI,message,ambil;
  92. {$R *.lfm}
  93.  
  94. { TFSewaan }
  95.  
  96. //pencetakan struk pembayaran
  97. procedure TFSewaan.Timer2Timer(Sender: TObject);
  98. begin
  99.     if flagbayar=true then
  100.     begin
  101.        BacaRFID;
  102.        if sc<0 then
  103.        begin
  104.           ShowMessage('Pembayaran dibatalkan');
  105.           flagbayar:=false;
  106.           flagterbayar:=false;
  107.           sc:=15;
  108.        end
  109.        else if cekkode.Caption <>''  then
  110.        begin
  111.           flagbayar:=false;
  112.           flagterbayar:=true;
  113.           Printsaja; //Cetak Struk pembayaran
  114.           insertdata(terpilihloker);
  115.           Info.Caption:='Cetak Struk pembayaran';
  116.           sleep(1000);
  117.           Info.Caption:='Pembayaran selesai';
  118.           sleep(1000);
  119.           Info.Caption:= 'Silahkan tekan tombol komfirmasi'+LineEnding+'untuk membuka loker anda'+LineEnding+barkode;
  120.           komfirmasi.Visible:=True;
  121.           sc:=15;
  122.        end
  123.        else
  124.        dec(sc,1);
  125.     end;
  126. end;
  127. //atur cek status loker
  128. procedure TFSewaan.Timer3Timer(Sender: TObject);
  129. begin
  130.    cekloker;
  131.    //perubahan isiloker ke warna GUI
  132.    //loker1
  133.    if data1=1 then
  134.     Shape1.Brush.Color := clRed
  135.    else
  136.     Shape1.Brush.Color := clGreen;
  137.    //loker2
  138.    if data2=1 then
  139.     Shape2.Brush.Color := clRed
  140.    else
  141.     Shape2.Brush.Color := clGreen;
  142.    //loker3
  143.    if data3=1 then
  144.     Shape3.Brush.Color := clRed
  145.    else
  146.     Shape3.Brush.Color := clGreen;
  147.    //loker4
  148.    if data4=1 then
  149.     Shape4.Brush.Color := clRed
  150.    else
  151.     Shape4.Brush.Color := clGreen;
  152.    //loker5
  153.    if data5=1 then
  154.     Shape5.Brush.Color := clRed
  155.    else
  156.     Shape5.Brush.Color := clGreen;
  157. end;
  158. procedure TFSewaan.insertdata(nomorloker:string);
  159. begin
  160.        zquery1.close;
  161.        zquery1.SQL.Clear;
  162.        zquery1.SQL.add('insert into uji1 (Barcode, Noloker, Status, Isiloker) values (:a, :b, 1, 1)');
  163.        zquery1.ParamByName('a').AsString:= barkode;
  164.        zquery1.ParamByName('b').AsString:= nomorloker;
  165.        zquery1.ExecSQL;
  166.        zquery1.close;
  167. end;
  168.  
  169. //Tombol
  170. procedure TFSewaan.BhomeClick(Sender: TObject);
  171. begin
  172.   Bbayar.Visible:= False;
  173.   Info.Caption:='Silahkan pilih loker yang'+LineEnding+'ingin digunakan' ;
  174.   close;
  175.   Home.Show;
  176. end;
  177.  
  178. procedure TFSewaan.CloseDialog;
  179. begin
  180.    close;
  181. end;
  182.  
  183. procedure TFSewaan.Button1Click(Sender: TObject);
  184. begin
  185.   if Shape1.Brush.Color = clRed  then
  186.            begin
  187.            Info.Caption:='Loker ini telah terisi';
  188.            end
  189.   else
  190.    begin
  191.         try
  192.            begin
  193.                 Info.Caption:='Silahkan lakukan pembayaran';
  194.                 flagbayar:=true;
  195.                 mssge.Show;
  196.            end;
  197.         finally
  198.         terpilihloker:='1';
  199.        end;
  200.    end;
  201. end;
  202.  
  203. procedure TFSewaan.Button2Click(Sender: TObject);
  204. begin
  205.   if Shape2.Brush.Color = clRed
  206.      then
  207.      Info.Caption:='Loker ini telah terisi'
  208.      else
  209.        begin
  210.        Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  211.        flagbayar:=true;
  212.        end;
  213. end;
  214.  
  215. procedure TFSewaan.Button3Click(Sender: TObject);
  216. begin
  217.   try
  218.   begin
  219.    if Shape3.Brush.Color = clRed
  220.      then
  221.      Info.Caption:='Loker ini telah terisi'
  222.      else
  223.        begin
  224.        while flagterbayar=false do
  225.              begin
  226.              Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  227.              flagbayar:=true;
  228.              if flagterbayar=true then
  229.               exit;
  230.               end;
  231.        end;
  232.   end;
  233.   finally
  234.       insertdata('3');
  235.   end;
  236.  
  237. end;
  238.  
  239. procedure TFSewaan.Button4Click(Sender: TObject);
  240. begin
  241.   if Shape4.Brush.Color = clRed
  242.      then
  243.      Info.Caption:='Loker ini telah terisi'
  244.      else
  245.        begin
  246.        Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  247.        flagbayar:=true;
  248.        end;
  249. end;
  250.  
  251. procedure TFSewaan.Button5Click(Sender: TObject);
  252. begin
  253.   if Shape5.Brush.Color = clRed
  254.      then
  255.      Info.Caption:='Loker ini telah terisi'
  256.      else
  257.        begin
  258.        Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  259.        flagbayar:=true;
  260.        end;
  261. end;
  262.  
  263. procedure TFSewaan.Button6Click(Sender: TObject);
  264. begin
  265.   mssge.show;
  266. end;
  267.  
  268. procedure TFSewaan.FormCreate(Sender: TObject);
  269. begin
  270.   sc:=15;
  271.   flagbayar:=false;
  272.   komfirmasi.Visible:=false;
  273. end;
  274.  
  275. procedure TFSewaan.KomfirmasiClick(Sender: TObject);
  276. begin
  277.         zquery1.close;
  278.         zquery1.SQL.Clear;
  279.         zquery1.SQL.add('update uji1 set Status = 1 where Barcode = :a order by Tanggal desc limit 1');
  280.         zquery1.ParamByName('a').AsString:=barkode;
  281.         zquery1.ExecSQL;
  282.         zquery1.close;
  283.         Komfirmasi.Visible:=false;
  284. end;
  285.  
  286. procedure TFSewaan.BbayarClick(Sender: TObject);
  287. begin
  288.   if Shape1.Brush.Color = clRed
  289.   then
  290.   Shape1.Brush.Color:=clGreen
  291.   else
  292.   Shape1.Brush.Color:=clRed;
  293. end;
  294.  
  295. // Baca RFID
  296. procedure TFSewaan.BacaRFID;
  297. var
  298.   datarfid :string;
  299.   R :Longint;
  300. begin
  301.      begin
  302.   // Set up the process; as an example a recursive directory search is used
  303.   // because that will usually result in a lot of data.
  304.   AProcess := TProcess.Create(nil);
  305.  
  306.   // The commands for Windows and *nix are different hence the $IFDEFs
  307.  
  308.   {$IFDEF Unix}
  309.     AProcess.Executable := '/home/pi/MFRC522-python/Read.py';
  310.   {$ENDIF Unix}
  311.  
  312.   // Process option poUsePipes has to be used so the output can be captured.
  313.   // Process option poWaitOnExit can not be used because that would block
  314.   // this program, preventing it from reading the output data of the process.
  315.   AProcess.Options := [poUsePipes];
  316.  
  317.   // Start the process (run the dir/ls command)
  318.   AProcess.Execute;
  319.  
  320.   // Create a stream object to store the generated output in. This could
  321.   // also be a file stream to directly save the output to disk.
  322.   OutputStream := TMemoryStream.Create;
  323.  
  324.   // All generated output from AProcess is read in a loop until no more data is available
  325.   repeat
  326.     // Get the new data from the process to a maximum of the buffer size that was allocated.
  327.     // Note that all read(...) calls will block except for the last one, which returns 0 (zero).
  328.     BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);
  329.  
  330.     // Add the bytes that were read to the stream for later usage
  331.     OutputStream.Write(Buffer, BytesRead)
  332.  
  333.   until BytesRead = 0;  // Stop if no more data is available
  334.  
  335.   // The process has finished so it can be cleaned up
  336.   AProcess.Free;
  337.  
  338.   // Or the data can be shown on screen
  339.   with TStringList.Create do
  340.   begin
  341.     OutputStream.Position := 0; // Required to make sure all data is copied from the start
  342.     LoadFromStream(OutputStream);
  343.     cekkode.Caption:=Text;
  344.     Free
  345.   end;
  346.     datarfid:='176,64,150,90'+LineEnding;
  347.     R:=CompareText(cekkode.Caption,datarfid);
  348.   // Clean up
  349.   OutputStream.Free;
  350. end;
  351.    if R=0
  352.    then
  353.    begin
  354.       Info.Caption:= 'Pembayaran Selesai';
  355.    end
  356.    else
  357.       Info.Caption:= 'Saldo anda kurang';
  358.  
  359. end;
  360.  
  361. // Print
  362. procedure TFSewaan.Printsaja;
  363. begin
  364.      try
  365.      frReport1.LoadFromFile('/home/pi/Desktop/Berhasil/LazReportt/test_1.lrf');
  366.      frReport1.FindObject('Bar1').Memo.Text := InttoStr(Random(999999));
  367.      barkode:=  frReport1.FindObject('Bar1').Memo.Text;
  368.      if frReport1.PrepareReport then
  369.      frReport1.PrintPreparedReport('1',1)
  370.      else
  371.        ShowMessage('error');
  372.    except
  373.      on e: exception do
  374.        ShowMessage(e.Message);
  375.        end;
  376. end;
  377.  
  378. procedure TFSewaan.cekloker;
  379. begin
  380.   //cek isiloker
  381.    zquery1.close;
  382.    zquery1.SQL.Clear;
  383.    zquery1.SQL.add('select * from uji1 where Noloker = 1 order by Tanggal desc limit 1');
  384.    zquery1.ExecSQL;
  385.    zquery1.open;
  386.    while not zquery1.EOF do begin
  387.     data1:=zquery1.FieldByName('Isiloker').AsInteger;
  388.     zquery1.Next;
  389.    end;
  390.    zquery1.close;
  391.    zquery1.SQL.Clear;
  392.    zquery1.SQL.add('select * from uji1 where Noloker = 2 order by Tanggal desc limit 1');
  393.    zquery1.ExecSQL;
  394.    zquery1.open;
  395.    while not zquery1.EOF do begin
  396.     data2:=zquery1.FieldByName('Isiloker').AsInteger;
  397.     zquery1.Next;
  398.    end;
  399.    zquery1.close;
  400.    zquery1.SQL.Clear;
  401.    zquery1.SQL.add('select * from uji1 where Noloker = 3 order by Tanggal desc limit 1');
  402.    zquery1.ExecSQL;
  403.    zquery1.open;
  404.    while not zquery1.EOF do begin
  405.     data3:=zquery1.FieldByName('Isiloker').AsInteger;
  406.     zquery1.Next;
  407.    end;
  408.    zquery1.close;
  409.    zquery1.SQL.Clear;
  410.    zquery1.SQL.add('select * from uji1 where Noloker = 4 order by Tanggal desc limit 1');
  411.    zquery1.ExecSQL;
  412.    zquery1.open;
  413.    while not zquery1.EOF do begin
  414.     data4:=zquery1.FieldByName('Isiloker').AsInteger;
  415.     zquery1.Next;
  416.    end;
  417.    zquery1.close;
  418.    zquery1.SQL.Clear;
  419.    zquery1.SQL.add('select * from uji1 where Noloker = 5 order by Tanggal desc limit 1');
  420.    zquery1.ExecSQL;
  421.    zquery1.open;
  422.    while not zquery1.EOF do begin
  423.     data5:=zquery1.FieldByName('Isiloker').AsInteger;
  424.     zquery1.Next;
  425.    end;
  426.    zquery1.close;
  427. end;
  428.  
  429. procedure TFSewaan.findbarkode(bar:string);
  430. begin
  431.        zquery1.close;
  432.        zquery1.SQL.Clear;
  433.        zquery1.SQL.add('select * from uji1 where Barcode = :a order by Tanggal desc limit 1');
  434.        zquery1.ParamByName('a').AsString:=bar;
  435.        zquery1.open;
  436.        while not zquery1.EOF do begin
  437.         caridata:=zquery1.FieldByName('Isiloker').AsInteger;
  438.         zquery1.Next;
  439.        end;
  440.        zquery1.close;
  441. end;
  442. procedure TFSewaan.tutupstatusloker(kolombarkode:string);
  443. begin
  444.         zquery1.close;
  445.         zquery1.SQL.Clear;
  446.         zquery1.SQL.add('update uji1 set Isiloker=0 where Barcode = :a order by Tanggal desc limit 1');
  447.         zquery1.ParamByName('a').AsString:=kolombarkode;
  448.         zquery1.ExecSQL;
  449.         zquery1.close;
  450. end;
  451.  
  452. procedure TFSewaan.Tombolkomfirmasi(uploadbarkode:string);
  453. begin
  454.         zquery1.close;
  455.         zquery1.SQL.Clear;
  456.         zquery1.SQL.add('update uji1 set Status = 1 where Barcode = :a order by Tanggal desc limit 1');
  457.         zquery1.ParamByName('a').AsString:=uploadbarkode;
  458.         zquery1.ExecSQL;
  459.         Zquery1.close;
  460. end;
  461.  
  462. end.
  463.  

Kevinn33

  • New member
  • *
  • Posts: 26
Re: program start normally, then hang and close, Z connection
« Reply #3 on: May 12, 2018, 06:48:09 pm »
@thaddy do you have any suggestion in using a connection to database other than Zeos?, just incase that i have to use another way...


i got a little time to set this project done... :(

GetMem

  • Hero Member
  • *****
  • Posts: 3187
Re: program start normally, then hang and close, Z connection
« Reply #4 on: May 12, 2018, 06:52:31 pm »
I doubt this is the cause of the exception, but you did not correct the issues I told you last time:
Code: Pascal  [Select]
  1.  zquery1.close;
  2.  zquery1.SQL.Clear;
  3.  zquery1.SQL.add('select * from uji1 where Noloker = 1 order by Tanggal desc limit 1');
  4.  zquery1.ExecSQL; // <-- you don't need this line
  5.  zquery1.open;

PS: Where you conntect/disconnect to/from databse? You should always disconnect when you close the application.
« Last Edit: May 12, 2018, 07:16:31 pm by GetMem »

Kevinn33

  • New member
  • *
  • Posts: 26
Re: program start normally, then hang and close, Z connection
« Reply #5 on: May 12, 2018, 07:10:36 pm »
I doubt this is the cause of the exception, but you did not correct the issues I told you last time:
Code: Pascal  [Select]
  1.  zquery1.close;
  2.  zquery1.SQL.Clear;
  3.  zquery1.SQL.add('select * from uji1 where Noloker = 1 order by Tanggal desc limit 1');
  4.  zquery1.ExecSQL; // <-- you don't need this line
  5.  zquery1.open;

PS1: Where you conntect/disconnect to/from databse? You should always disconnect when you close the application.
PS2: http://forum.lazarus.freepascal.org/index.php/topic,41190.msg285573.html#msg285573

oh my, i must be forget to save the project after change it.
but is it normal that the programe freeze after i change that? i'm using Raspberry pi 3 debian Stretch

i connect the database in the object inspection, is it necessary to disconnect database? i mean when the program is running and changing form

Kevinn33

  • New member
  • *
  • Posts: 26
Re: program start normally, then hang and close, Z connection
« Reply #6 on: May 12, 2018, 07:12:36 pm »
@GetMem
i've got a result of changing that, it still freeze and pop out a too much connection messase

GetMem

  • Hero Member
  • *****
  • Posts: 3187
Re: program start normally, then hang and close, Z connection
« Reply #7 on: May 12, 2018, 07:20:43 pm »
Quote
i connect the database in the object inspection, is it necessary to disconnect database? i mean when the program is running and changing form
Don't do that. Always connect from code and make sure you always disconnect on exit.

Kevinn33

  • New member
  • *
  • Posts: 26
Re: program start normally, then hang and close, Z connection
« Reply #8 on: May 12, 2018, 07:33:47 pm »
Quote
i connect the database in the object inspection, is it necessary to disconnect database? i mean when the program is running and changing form
Don't do that. Always connect from code and make sure you always disconnect on exit.

did i do it correctly?

Code: Pascal  [Select]
  1. unit Sewa;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, LR_Class, LR_BarC, Forms, Controls, Graphics,
  9.   Dialogs, StdCtrls, ExtCtrls, Buttons, Process, db, Printers, Menus, ComCtrls,
  10.   ZConnection, ZDataset;
  11.  
  12. type
  13.  
  14.   { TFSewaan }
  15.  
  16.   TFSewaan = class(TForm)
  17.     Bbayar: TButton;
  18.     Bhome: TButton;
  19.     Biaya: TLabel;
  20.     Button1: TButton;
  21.     Button2: TButton;
  22.     Button3: TButton;
  23.     Button4: TButton;
  24.     Button5: TButton;
  25.     Button6: TButton;
  26.     cekkode: TLabel;
  27.     DateTimeSewa: TLabel;
  28.     frBarCodeObject1: TfrBarCodeObject;
  29.     frReport1: TfrReport;
  30.     Info: TLabel;
  31.     Komfirmasi: TButton;
  32.     Label1: TLabel;
  33.     Panel1: TPanel;
  34.     Panel2: TPanel;
  35.     Panel3: TPanel;
  36.     Panel4: TPanel;
  37.     Panel5: TPanel;
  38.     Shape1: TShape;
  39.     Shape2: TShape;
  40.     Shape3: TShape;
  41.     Shape4: TShape;
  42.     Shape5: TShape;
  43.     Timer1: TTimer;
  44.     Timer2: TTimer;
  45.     Timer3: TTimer;
  46.     ZConnection1: TZConnection;
  47.     ZQuery1: TZQuery;
  48.  
  49.     procedure BbayarClick(Sender: TObject);
  50.     procedure BhomeClick(Sender: TObject);
  51.     procedure Button1Click(Sender: TObject);
  52.     procedure Button2Click(Sender: TObject);
  53.     procedure Button3Click(Sender: TObject);
  54.     procedure Button4Click(Sender: TObject);
  55.     procedure Button5Click(Sender: TObject);
  56.     procedure Button6Click(Sender: TObject);
  57.     procedure FormCreate(Sender: TObject);
  58.     procedure KomfirmasiClick(Sender: TObject);
  59.     procedure Printsaja;
  60.     procedure BacaRFID;
  61.     procedure CLoseDialog;
  62.     procedure Timer2Timer(Sender: TObject);
  63.     procedure Timer3Timer(Sender: TObject);
  64.     procedure insertdata(nomorloker:string);
  65.   private
  66.     sc: Integer;
  67.     flagbayar: Boolean;
  68.     terpilihloker :Char;
  69.   public
  70.     data1,data2,data3,data4,data5 :Integer;
  71.     caridata:integer;
  72.     barkode: string;
  73.     flagterbayar: boolean;
  74.     procedure findbarkode(bar:string);
  75.     procedure tutupstatusloker(kolombarkode:string);
  76.     procedure Tombolkomfirmasi(uploadbarkode:string);
  77.     procedure cekloker;
  78.   end;
  79.  
  80. const
  81.   BUF_SIZE = 2048;
  82. var
  83.   FSewaan: TFSewaan;
  84.   AProcess: TProcess;
  85.   OutputStream: Tstream;
  86.   BytesRead : longint;
  87.   Buffer : array[1..BUF_SIZE] of byte;
  88.  
  89. implementation
  90. uses
  91.   MainGUI,message,ambil;
  92. {$R *.lfm}
  93.  
  94. { TFSewaan }
  95.  
  96. //pencetakan struk pembayaran
  97. procedure TFSewaan.Timer2Timer(Sender: TObject);
  98. begin
  99.     if flagbayar=true then
  100.     begin
  101.        BacaRFID;
  102.        if sc<0 then
  103.        begin
  104.           ShowMessage('Pembayaran dibatalkan');
  105.           flagbayar:=false;
  106.           flagterbayar:=false;
  107.           sc:=15;
  108.        end
  109.        else if cekkode.Caption <>''  then
  110.        begin
  111.           flagbayar:=false;
  112.           flagterbayar:=true;
  113.           Printsaja; //Cetak Struk pembayaran
  114.           insertdata(terpilihloker);
  115.           Info.Caption:='Cetak Struk pembayaran';
  116.           sleep(1000);
  117.           Info.Caption:='Pembayaran selesai';
  118.           sleep(1000);
  119.           Info.Caption:= 'Silahkan tekan tombol komfirmasi'+LineEnding+'untuk membuka loker anda'+LineEnding+barkode;
  120.           komfirmasi.Visible:=True;
  121.           sc:=15;
  122.        end
  123.        else
  124.        dec(sc,1);
  125.     end;
  126. end;
  127. //atur cek status loker
  128. procedure TFSewaan.Timer3Timer(Sender: TObject);
  129. begin
  130.    cekloker;
  131.    //perubahan isiloker ke warna GUI
  132.    //loker1
  133.    if data1=1 then
  134.     Shape1.Brush.Color := clRed
  135.    else
  136.     Shape1.Brush.Color := clGreen;
  137.    //loker2
  138.    if data2=1 then
  139.     Shape2.Brush.Color := clRed
  140.    else
  141.     Shape2.Brush.Color := clGreen;
  142.    //loker3
  143.    if data3=1 then
  144.     Shape3.Brush.Color := clRed
  145.    else
  146.     Shape3.Brush.Color := clGreen;
  147.    //loker4
  148.    if data4=1 then
  149.     Shape4.Brush.Color := clRed
  150.    else
  151.     Shape4.Brush.Color := clGreen;
  152.    //loker5
  153.    if data5=1 then
  154.     Shape5.Brush.Color := clRed
  155.    else
  156.     Shape5.Brush.Color := clGreen;
  157. end;
  158. procedure TFSewaan.insertdata(nomorloker:string);
  159. begin
  160.        zconnection1.Connected:=true;
  161.        zquery1.close;
  162.        zquery1.SQL.Clear;
  163.        zquery1.SQL.add('insert into uji1 (Barcode, Noloker, Status, Isiloker) values (:a, :b, 1, 1)');
  164.        zquery1.ParamByName('a').AsString:= barkode;
  165.        zquery1.ParamByName('b').AsString:= nomorloker;
  166.        zquery1.ExecSQL;
  167.        zquery1.close;
  168.        zconnection1.Connected:=true;
  169. end;
  170.  
  171. //Tombol
  172. procedure TFSewaan.BhomeClick(Sender: TObject);
  173. begin
  174.   Bbayar.Visible:= False;
  175.   Info.Caption:='Silahkan pilih loker yang'+LineEnding+'ingin digunakan' ;
  176.   close;
  177.   Home.Show;
  178. end;
  179.  
  180. procedure TFSewaan.CloseDialog;
  181. begin
  182.    close;
  183. end;
  184.  
  185. procedure TFSewaan.Button1Click(Sender: TObject);
  186. begin
  187.   if Shape1.Brush.Color = clRed  then
  188.            begin
  189.            Info.Caption:='Loker ini telah terisi';
  190.            end
  191.   else
  192.    begin
  193.         try
  194.            begin
  195.                 Info.Caption:='Silahkan lakukan pembayaran';
  196.                 flagbayar:=true;
  197.                 mssge.Show;
  198.            end;
  199.         finally
  200.         terpilihloker:='1';
  201.        end;
  202.    end;
  203. end;
  204.  
  205. procedure TFSewaan.Button2Click(Sender: TObject);
  206. begin
  207.   if Shape2.Brush.Color = clRed
  208.      then
  209.      Info.Caption:='Loker ini telah terisi'
  210.      else
  211.        begin
  212.        Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  213.        flagbayar:=true;
  214.        end;
  215. end;
  216.  
  217. procedure TFSewaan.Button3Click(Sender: TObject);
  218. begin
  219.   try
  220.   begin
  221.    if Shape3.Brush.Color = clRed
  222.      then
  223.      Info.Caption:='Loker ini telah terisi'
  224.      else
  225.        begin
  226.        while flagterbayar=false do
  227.              begin
  228.              Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  229.              flagbayar:=true;
  230.              if flagterbayar=true then
  231.               exit;
  232.               end;
  233.        end;
  234.   end;
  235.   finally
  236.       insertdata('3');
  237.   end;
  238.  
  239. end;
  240.  
  241. procedure TFSewaan.Button4Click(Sender: TObject);
  242. begin
  243.   if Shape4.Brush.Color = clRed
  244.      then
  245.      Info.Caption:='Loker ini telah terisi'
  246.      else
  247.        begin
  248.        Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  249.        flagbayar:=true;
  250.        end;
  251. end;
  252.  
  253. procedure TFSewaan.Button5Click(Sender: TObject);
  254. begin
  255.   if Shape5.Brush.Color = clRed
  256.      then
  257.      Info.Caption:='Loker ini telah terisi'
  258.      else
  259.        begin
  260.        Info.Caption:='Silahkan lakukan pembayaran'+LineEnding+'dengan Tap Kartu';
  261.        flagbayar:=true;
  262.        end;
  263. end;
  264.  
  265. procedure TFSewaan.Button6Click(Sender: TObject);
  266. begin
  267.   mssge.show;
  268. end;
  269.  
  270. procedure TFSewaan.FormCreate(Sender: TObject);
  271. begin
  272.   sc:=15;
  273.   flagbayar:=false;
  274.   komfirmasi.Visible:=false;
  275. end;
  276.  
  277. procedure TFSewaan.KomfirmasiClick(Sender: TObject);
  278. begin
  279.         zquery1.close;
  280.         zquery1.SQL.Clear;
  281.         zquery1.SQL.add('update uji1 set Status = 1 where Barcode = :a order by Tanggal desc limit 1');
  282.         zquery1.ParamByName('a').AsString:=barkode;
  283.         zquery1.ExecSQL;
  284.         zquery1.close;
  285.         Komfirmasi.Visible:=false;
  286. end;
  287.  
  288. procedure TFSewaan.BbayarClick(Sender: TObject);
  289. begin
  290.   if Shape1.Brush.Color = clRed
  291.   then
  292.   Shape1.Brush.Color:=clGreen
  293.   else
  294.   Shape1.Brush.Color:=clRed;
  295. end;
  296.  
  297. // Baca RFID
  298. procedure TFSewaan.BacaRFID;
  299. var
  300.   datarfid :string;
  301.   R :Longint;
  302. begin
  303.      begin
  304.   // Set up the process; as an example a recursive directory search is used
  305.   // because that will usually result in a lot of data.
  306.   AProcess := TProcess.Create(nil);
  307.  
  308.   // The commands for Windows and *nix are different hence the $IFDEFs
  309.  
  310.   {$IFDEF Unix}
  311.     AProcess.Executable := '/home/pi/MFRC522-python/Read.py';
  312.   {$ENDIF Unix}
  313.  
  314.   // Process option poUsePipes has to be used so the output can be captured.
  315.   // Process option poWaitOnExit can not be used because that would block
  316.   // this program, preventing it from reading the output data of the process.
  317.   AProcess.Options := [poUsePipes];
  318.  
  319.   // Start the process (run the dir/ls command)
  320.   AProcess.Execute;
  321.  
  322.   // Create a stream object to store the generated output in. This could
  323.   // also be a file stream to directly save the output to disk.
  324.   OutputStream := TMemoryStream.Create;
  325.  
  326.   // All generated output from AProcess is read in a loop until no more data is available
  327.   repeat
  328.     // Get the new data from the process to a maximum of the buffer size that was allocated.
  329.     // Note that all read(...) calls will block except for the last one, which returns 0 (zero).
  330.     BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);
  331.  
  332.     // Add the bytes that were read to the stream for later usage
  333.     OutputStream.Write(Buffer, BytesRead)
  334.  
  335.   until BytesRead = 0;  // Stop if no more data is available
  336.  
  337.   // The process has finished so it can be cleaned up
  338.   AProcess.Free;
  339.  
  340.   // Or the data can be shown on screen
  341.   with TStringList.Create do
  342.   begin
  343.     OutputStream.Position := 0; // Required to make sure all data is copied from the start
  344.     LoadFromStream(OutputStream);
  345.     cekkode.Caption:=Text;
  346.     Free
  347.   end;
  348.     datarfid:='176,64,150,90'+LineEnding;
  349.     R:=CompareText(cekkode.Caption,datarfid);
  350.   // Clean up
  351.   OutputStream.Free;
  352. end;
  353.    if R=0
  354.    then
  355.    begin
  356.       Info.Caption:= 'Pembayaran Selesai';
  357.    end
  358.    else
  359.       Info.Caption:= 'Saldo anda kurang';
  360.  
  361. end;
  362.  
  363. // Print
  364. procedure TFSewaan.Printsaja;
  365. begin
  366.      try
  367.      frReport1.LoadFromFile('/home/pi/Desktop/Berhasil/LazReportt/test_1.lrf');
  368.      frReport1.FindObject('Bar1').Memo.Text := InttoStr(Random(999999));
  369.      barkode:=  frReport1.FindObject('Bar1').Memo.Text;
  370.      if frReport1.PrepareReport then
  371.      frReport1.PrintPreparedReport('1',1)
  372.      else
  373.        ShowMessage('error');
  374.    except
  375.      on e: exception do
  376.        ShowMessage(e.Message);
  377.        end;
  378. end;
  379.  
  380. procedure TFSewaan.cekloker;
  381. begin
  382.   //cek isiloker
  383.    zconnection1.Connected:=true;
  384.    zquery1.close;
  385.    zquery1.SQL.Clear;
  386.    zquery1.SQL.add('select * from uji1 where Noloker = 1 order by Tanggal desc limit 1');
  387.    zquery1.open;
  388.    while not zquery1.EOF do begin
  389.     data1:=zquery1.FieldByName('Isiloker').AsInteger;
  390.     zquery1.Next;
  391.    end;
  392.    zquery1.close;
  393.    zquery1.SQL.Clear;
  394.    zquery1.SQL.add('select * from uji1 where Noloker = 2 order by Tanggal desc limit 1');
  395.    zquery1.open;
  396.    while not zquery1.EOF do begin
  397.     data2:=zquery1.FieldByName('Isiloker').AsInteger;
  398.     zquery1.Next;
  399.    end;
  400.    zquery1.close;
  401.    zquery1.SQL.Clear;
  402.    zquery1.SQL.add('select * from uji1 where Noloker = 3 order by Tanggal desc limit 1');
  403.    zquery1.open;
  404.    while not zquery1.EOF do begin
  405.     data3:=zquery1.FieldByName('Isiloker').AsInteger;
  406.     zquery1.Next;
  407.    end;
  408.    zquery1.close;
  409.    zquery1.SQL.Clear;
  410.    zquery1.SQL.add('select * from uji1 where Noloker = 4 order by Tanggal desc limit 1');
  411.    zquery1.open;
  412.    while not zquery1.EOF do begin
  413.     data4:=zquery1.FieldByName('Isiloker').AsInteger;
  414.     zquery1.Next;
  415.    end;
  416.    zquery1.close;
  417.    zquery1.SQL.Clear;
  418.    zquery1.SQL.add('select * from uji1 where Noloker = 5 order by Tanggal desc limit 1');
  419.    zquery1.open;
  420.    while not zquery1.EOF do begin
  421.     data5:=zquery1.FieldByName('Isiloker').AsInteger;
  422.     zquery1.Next;
  423.    end;
  424.    zquery1.close;
  425.    zconnection1.Connected:=false;
  426. end;
  427.  
  428. procedure TFSewaan.findbarkode(bar:string);
  429. begin
  430.        zconnection1.Connected:=true;
  431.        zquery1.close;
  432.        zquery1.SQL.Clear;
  433.        zquery1.SQL.add('select * from uji1 where Barcode = :a order by Tanggal desc limit 1');
  434.        zquery1.ParamByName('a').AsString:=bar;
  435.        zquery1.open;
  436.        while not zquery1.EOF do begin
  437.         caridata:=zquery1.FieldByName('Isiloker').AsInteger;
  438.         zquery1.Next;
  439.        end;
  440.        zquery1.close;
  441.        zconnection1.Connected:=False;
  442. end;
  443. procedure TFSewaan.tutupstatusloker(kolombarkode:string);
  444. begin
  445.         zconnection1.Connected:=true;
  446.         zquery1.close;
  447.         zquery1.SQL.Clear;
  448.         zquery1.SQL.add('update uji1 set Isiloker=0 where Barcode = :a order by Tanggal desc limit 1');
  449.         zquery1.ParamByName('a').AsString:=kolombarkode;
  450.         zquery1.ExecSQL;
  451.         zquery1.close;
  452.         zconnection1.Connected:=False;
  453. end;
  454.  
  455. procedure TFSewaan.Tombolkomfirmasi(uploadbarkode:string);
  456. begin
  457.         zconnection1.Connected:=true;
  458.         zquery1.close;
  459.         zquery1.SQL.Clear;
  460.         zquery1.SQL.add('update uji1 set Status = 1 where Barcode = :a order by Tanggal desc limit 1');
  461.         zquery1.ParamByName('a').AsString:=uploadbarkode;
  462.         zquery1.ExecSQL;
  463.         Zquery1.close;
  464.         zconnection1.Connected:=False;
  465. end;
  466.  
  467. end.
  468.  

GetMem

  • Hero Member
  • *****
  • Posts: 3187
Re: program start normally, then hang and close, Z connection
« Reply #9 on: May 12, 2018, 07:37:15 pm »
Quote
did i do it correctly?
No. You only need to connect once, when your application starts and disconnect in the end. You may check from time to time if the connection is alive, if not then reconnect.

PS: You're code should also work, but it's not optimal.
PS1: Try you restart the server first.
« Last Edit: May 12, 2018, 07:44:37 pm by GetMem »

Kevinn33

  • New member
  • *
  • Posts: 26
Re: program start normally, then hang and close, Z connection
« Reply #10 on: May 12, 2018, 08:13:39 pm »
Quote
did i do it correctly?
No. You only need to connect once, when your application starts and disconnect in the end. You may check from time to time if the connection is alive, if not then reconnect.

PS: You're code should also work, but it's not optimal.
PS1: Try you restart the server first.

tried to use connect and disconnect once,  and also restart server. my program still some times freeze

i should tell that i'm using 3 form, and the code i gave is not the main form
Myproject is almost done, but it freeze some times, while it takes 25% of the processing in raspberry pi 3

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: program start normally, then hang and close, Z connection
« Reply #11 on: May 12, 2018, 08:21:54 pm »
In the case of a RaspberryPi 3 that means it runs one core at 100%. Since that is also your GUI core that is not a good idea. Your code (or Zeos!) is still not good. Look at the pointers getmem gave you: he has more experience withe Zeos, I guess. I abandoned it after too many problems and use the basic standard components. These are stable, even over multiple threads. Written on RPi3...
« Last Edit: May 12, 2018, 08:29:48 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: program start normally, then hang and close, Z connection
« Reply #12 on: May 12, 2018, 08:34:42 pm »
Oh, and Zeos is not a database..... It is a means of connecting to databases in a very convenient way..At least, that's advertised... There are native components for almost every database you want with FPC. Use those. They are stable, better written and support threading if you need it..
« Last Edit: May 12, 2018, 08:37:24 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

GetMem

  • Hero Member
  • *****
  • Posts: 3187
Re: program start normally, then hang and close, Z connection
« Reply #13 on: May 12, 2018, 08:41:39 pm »
You should post a small demo application(database included) so we can run a few test. Unfortunately I don't have a raspberry, but the problem is more likely somewhere else.

GAN

  • Full Member
  • ***
  • Posts: 189
Re: program start normally, then hang and close, Z connection
« Reply #14 on: May 12, 2018, 10:26:20 pm »
Where is your TDataSource?
Lazarus 1.6 FPC 3.0.0 Linux Mint Mate 17.2 x86_64 GTK-2
Zeos 7.1.3 - Sqlite 3.8.2

Foro Lazarus en español http://forum.lazarus.freepascal.org/index.php/board,73.0.html

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus