Real time clocks and reading Ports

This is a discussion on Real time clocks and reading Ports within the C++ Programming forums, part of the General Programming Boards category; Hi people i am currently in Madrid Spain and working at an internship. my task is to do some C ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    Real time clocks and reading Ports

    Hi people

    i am currently in Madrid Spain and working at an internship.

    my task is to do some C programming.

    my task is to take some information from a port on the computer (COM 3) and save it, sort it and put it into a notepad file. the info coming through is raw hex and is not very important.

    the problem is that i am not sure if it possible or how to read the com port in C programming and how to put in a real time clock into the system. i have seen some theories but nothing works!! the clock is very important as at the start of every hour, a new file must created.

    other wise, the sorting and rearranging i can do perfectly fine.

    any help would be greatly appreciated or even a kick in the right direction!!

    thanks in advance

    shoobsie

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Access to com ports is OS specific (sometimes compiler specific) so those details are important. Most OSs have a file-like API for com ports (open, read, write, etc.) which hide a lot of ugly detail. As for time, check out <ctime>.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    thanks alot.

    i managed to find something that displays the real time.

    the code i will use is:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t rawtime;
    
      time ( &rawtime );
      printf ( "Current date and time are: %s", ctime (&rawtime) );
      
      return 0;
    
    }
    just a follow up question. is it possible to use this time for further tasks and to update it constantly?

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    time() on most machine only has second resolution, so calling it more than once a second will get you identical values. If you want the bits of the time value, investigate 'struct tm' and the routines that work with it (I have this stuff encapsulated in my programs, haven't looked at it in years). I have done similiar things (i.e., naming files after dates), so it is not that difficult.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    once again, thank you!

    i would a routine that executes the writing onto a file, once every second. with a save at the end of the hour and a new file opened up.

    hence i wanted to be able to know when the clock is updated and if the program can use it to do its functions of saving and so on.

  6. #6
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I suggest you investigate 'sleep' or on MS 'Sleep'. You can put your program in a loop checking to see if the clock has changed, but that is a total waste of CPU cycles. Sleep will allow you to ask the OS to wake your program up at a certain time (it is NOT required to!) and your program uses no cycles when that is happening. If you want to write to the file AND detect that the time has changed, you will probably need to learn to multi-thread.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    shoobsie - what OS and compiler are you using? If we know that, we can point you to specific resources.

    gg

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    guys, im using windows 2000 pro
    and Microsoft Visual C++ 6

    thanks to those who have helped

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Serial Communications in Win32

    If you don't mind using C++, I've posted a wrapper class for performing serial communications here: Simple RS232 Question

    As for doing something every second, calling Sleep(1000) will be the easiest way to get you started.

    gg

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21