Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VC++ Get Current Time

Status
Not open for further replies.

dkennington

Programmer
Jul 11, 2006
4
US
I have two pieces of equipment that run on different software and I need to sync them up.

I'm using VC++6 on Windows XP, and I need to do a time delay so that they alternate their task by a second or two.

I've tried normal time delays, except the programs always slowly get out of phase and then they end up trying to write/read a file at the same time and they crash.

The only way I can think of doing this is by using the current date and time on the computer. Except I have no idea how to do this? I'm not a programmer, so could you be very specific on what I need to type and include?

Please help,
Thanks,
Dave
 
Well first, do you have the source code for the 2 programs, or are you trying to keep them in sync through a 3rd program that you're writing?

Do the programs run continuously, or are you trying to alternate running one, then the other...?
 
I have two pieces of equipment, each was provided with software to run them. One uses VC++6 and it has the source code, and the other uses LabView, which isn't C++ at all, I'll have to figure out how to get this to sync up myself.

Both programs will be running seperatly and continuously but at the same time. One program reads an image file, which modifies a laser. The second program then analysis the change and writes a new image file based on the data.

So one program is continuously writing to an image file while the other is continuously reading that same file.

If read and write happen at same time, *error*, no more continuous run.

So I found where it reads the image file and its in a for loop. All I need to do is have the loop wait for a certain time before each cycle. And I cant use a normal delay, because the programs slowly get out of sync till I get the error. Both programs need to be using a common clock, like the one on the cpu?

(I can't get both pieces of equipment running in the same compiler because that would cost $750! this is a school project and I'm not willing to spend more than $30)
 
I'm not sure what LabView is? I found this on Google:
If there's a way for you to modify the code for both programs (whether it's C++ or not), then one quick & dirty way to keep the programs from stepping on each others toes is like this (I'll call the programs Reader & Writer):

Reader:
1. Check if lock file exists.
2. If lock file exists, wait x seconds, then go back to step 1.
3. If lock file doesn't exist, create a lock file.
4. Read the file.
5. Close the file & delete the lock file.

Writer:
1. Check if lock file exists.
2. If lock file exists, wait x seconds, then go back to step 1.
3. If lock file doesn't exist, create a lock file.
4. Write to the file.
5. Close the file & delete the lock file.

Basically, whichever program has control of the lock file is allowed to have exclusive access the file you're trying to synchronize. The lock file doesn't need to have any data in it, but both programs need to know exactly what directory to look in for the existence of the lock file.

The downfalls to this approach are:
1. If one of the programs ends or crashes before deleting the lock file, the other program will never get control of the file until you manually delete it.
2. If performance is an issue, this solution is slower since it requires slow disk I/O for the lock file.
 
To get the current time -
CTime Today = CTime::GetCurrentTime();

To then check the time -
if(Today.GetSecond()==0 && {machine1})
{process};
else if(Today.GetSecond()==30 && {machine2})
{process};

The {machine1} & {machine2} identifies would be variables populated from an environment variable on the respective machines to distinguish them from each other.


john.
 
Looks like LabView has parallel port IO capabilitys, see
what type machines are you running on? could they be able to be linked so machine 2 gets the time from machine 1? google has some interesting stuff use LabView timing as your search string. but check out

You get to have all the fun : ))
CosmicTruth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top