Solved

How to start saving data in app using TTimer

Posted on 2008-10-31
10
160 Views
Last Modified: 2012-05-05
Hello Experts,
I need help with my problem. I want to start transmission from RS232 at specified time and finish it after 12 hours.
For example I`ve started my app at 4.32 a.m. and I want to start saving data at 5.00 a.m, finish it at 5 p.m. - of course I`m talking about automatic process without user activity.
BR
Vaalar
0
Comment
Question by:Vaalar
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 22851020
Are you wanting to run your application for this entire length of time?

As a thought, as I am not clear on exactly what you are desiring, I would have a service that runs on this PC and keeps track of the time and the forwarding of information via the PCs RS232 port.

Your PCs the one which sends and the one which receives should have the time synced very closely.

There are good tutorials on the web on how to write Delphi Service apps. I do something similar with a database that at midnight updates leave and other stuff.

Google "how to write Delphi service application"

John
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 22858755
You could start a thread as from a specified time
using a TTimer to see if you have reached the time
and then to the same to Terminate the thread

it would off course start immediately between 5am and 5pm


type
  TForm1 = class(TForm)
  private
    fYourRS232Thread: TRS232SavingThread;
    fStartTime: TDateTime;
    fEndTime: TDateTime;
    fStarting: Boolean;
  end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  fStartTime := Trunc(Now) + 5/24; // Today at 5 am
  fEndTime := fStartTime + 1/2; // half day
  fYourRS232Thread := Nil;
  fStarting := True;
end;
 
procedure TForm1.Timer1OnTimer(Sender: TObject);
begin
  if fStarting and (fYourRS232Thread = nil)
    and (Now > fStartTime)  and (Now < fEndTime) then 
  begin
       fYourRS232Thread := TRS232SavingThread.Create;
       fStarting := False;
  end else if not fStarting and (fYourRS232Thread <> nil)
    and (Now > fEndTime) then
  begin
    fYourRS232Thread.Terminate;
    fYourRS232Thread := nil;
    fStarting := False;
  end;
end;

Open in new window

0
 

Author Comment

by:Vaalar
ID: 22865805
Hello Guys,
Johnjces, yes I want to keep app running all the time. So I have to run only the comport.OnRxChar event.
I thought it would be easier.
Here is what I made so far:

Timer with interval = 60000
It`s not working
procedure TForm1.Timer1Timer(Sender: TObject);
var
StartTime : TDateTime;
H,M,S : word;
new_S, Timer : string;
begin
  StartTime:= Now();
  DecodeDate(StartTime, H, M, S);
  new_S:= IntToStr(S);
  Timer:= '11:15:'+new_S;
  Label33.Caption:=TimeToStr(StartTime);
  if StartTime=StrToTime(Timer) then
   begin
    if ADOConnection1.Connected=False then
      ADOConnection1.Connected:=True;
    if Form10.ComPort1.Connected=False then
    begin
      Form10.ComPort1.Connected:=True;
      Form10.ComPort2.Connected:=True;
      Form10.ComPort3.Connected:=False;
      Form10.ComPort4.Connected:=False;
      Form10.ComPort5.Connected:=False;
    end;
   end;
   if TimeToStr(Time)='22:00' then
     begin
       if Form10.ComPort1.Connected=True then
         begin
           Form10.ComPort1.Connected:=False;
           Form10.ComPort2.Connected:=False;
           Form10.ComPort3.Connected:=False;
           Form10.ComPort4.Connected:=False;
           Form10.ComPort5.Connected:=False;
           ADOConnection1.Connected:=False;
         end;
  end;
end;
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22865829
are you trying to collect data from a RS232 port and saving this to a database in a specified time frame ?

at first glance:
you are using forms and you don't want user interaction
then there is no need for forms

you can create a Commport component manually in a TObject
what you put in the form looks like opening and closing the commport at specified times

nothing about the receiving of data ...


0
 

Author Comment

by:Vaalar
ID: 22866145
Ok once again,
The problem occures becouse customer comes to the office at 8.00 a.m. but the production starts at 5.00 a.m. so i have to start saving data from rs232( comport) to database. I don`t have a problems with data saving I need only a piece of code or an idea how to start my comport1, comport2, comport3, comport4 and comport5 to receive data at speciefied time. Exactly how to cause such situation that at 5.00 a.m. the ComPort1.Connected option will be set up to Enabled.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22866349
the main problem will be the Timer Event
this is an event that was put on the Queue by the timer
the application processes the Queue 1 event at a time
the good thing is, that computers are really fast nowadays, but
it is possible that you may not check at a certain second with a Timer interval = 1000
the real time between processing events may differ from 1 sec to ???
so you need to check if the start time is passed or not

this may be your problem:
if StartTime=StrToTime(Timer) then

if showed you the code with
if ...
  and (Now > fStartTime)  and (Now < fEndTime) then

but since you are not using threads (or so it seems)
the processing of the events will never (or very slowly) happen

that's why i suggested using threads

no matter how slow the thread is, it will allways be stopped / started
0
 

Author Comment

by:Vaalar
ID: 22866361
Something is wrong with my app - I have tried to show a clock:
  Label33.Caption:=TimeToStr(Now());
with interval of 1000 and I`m able to show only the start value. Label doesn`t change every one sec.
I have no idea what is wrong with this TTimer
0
 

Author Comment

by:Vaalar
ID: 22866416
Ok Geert It seems you are right. I`ll try to do it with threads but It`s a piece of work for me becouse i have never done it before.
I`ll accept your code with thread as a solution
Thx for your help.
BR
Vaalar
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22867065
if you need help, let us know
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22868171
steps to keep in mind:

you may want to have 1 thread for each comm port
this shouldn't pose too much problems other than you need to stop/start all these threads

you may want to have 1 thread for the database saving of data
with ADO : at the beginning of the db thread call CoInitialize and at the end call CoUnitialize
and define a separate connection for the thread
5 threads -> write data to buffer -> 1 db thread -> save buffer to db

make sure you synchronize the read/writing of the buffer if in memory
when the db saving is slow, i tend to have the separate threads create data files on the harddisk
if the db thread fails the data is on the drive and not lost in memory
you could leave the db thread running and only stop/start the comm threads


0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video teaches viewers about errors in exception handling.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question