C++ Windows time and com ports
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: C++ Windows time and com ports

  1. #1
    Junior Member
    Join Date
    Mar 2003
    Posts
    26

    Question C++ Windows time and com ports

    I was just wondering what functions/API's I should be looking at to a) retrieve the current time on the systems clock (preferably in an integer) and b) what functions and such are used to interact with the COM ports on a windows machine.

    any help appreciated.
    Never argue with an idiot, they\'ll just bring you down to their level and then beat you with experience

  2. #2
    Junior Member
    Join Date
    Mar 2003
    Posts
    26
    while I'm at it, quick question, how do you make a pointer to a boolean array a parameter for a function and then how do you call it?
    Never argue with an idiot, they\'ll just bring you down to their level and then beat you with experience

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    472
    The system time.... It's just gonna work in Visual C++ with MFC, because the GetLocalTime function is in MFC. There are of course som api function that will do the job, but I don't remember which those are... Anyway...

    //get the system time like this
    SYSTEMTIME *pSysTime = new SYSTEMTIME();
    GetLocalTime(pSysTime);

    //and then access the SYSTEMTIME like this
    int day = pSysTime->wDay
    int month = pSysTime->wMonth

    Boolean array question:

    void mymethod(bool* par)
    {
    bool newpar = *par;
    }

    main()
    {
    bool var;
    mymethod(&var);
    }

    Hope this helps
    ---
    proactive

  4. #4
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    As far as accessing serial ports is concerned...

    The "correct" Win32 way of doing this, as far as I know, is with CreateFile() on "com1" etc.

    Here is some code I wrote a while ago...

    Code:
    static HANDLE hCom;
    
    void vecserial_init()
    {
       char *pcCommPort = "COM1";
       DCB dcb;
       BOOL fSuccess;
    
       hCom = CreateFile( pcCommPort,
                        GENERIC_READ | GENERIC_WRITE,
                        0,    // must be opened with exclusive-access
                        NULL, // no security attributes
                        OPEN_EXISTING, // must use OPEN_EXISTING
                        0,    // not overlapped I/O
                        NULL  // hTemplate must be NULL for comm devices
                        );
    // TODO: check open succeeded
    Ok, that opens the com port, but what about setting speed, etc? You want to be able to do this.

    Code:
       fSuccess = GetCommState(hCom, &dcb); // TODO: check fSuccess
       // Fill in DCB: 38400 bps, 8 data bits, no parity, and 1 stop bit.
    
       dcb.BaudRate = CBR_38400;     // set the baud rate
       dcb.ByteSize = 8;             // data size, xmit, and rcv
       dcb.Parity = NOPARITY;        // no parity bit
       dcb.StopBits = ONESTOPBIT;    // one stop bit
    
       fSuccess = SetCommState(hCom, &dcb); // TODO: checkfSuccess
    My actual code is available upon request, but essentially the same but with more error handling.

    To read/write the serial port, you can then use ReadFile and WriteFile respectively (read M$ doc for more info)

    Slarty

  5. #5
    Junior Member
    Join Date
    Mar 2003
    Posts
    26
    hmm, that makes it look like I'm going to have to write my own device drivers to do what I want to do.

    you see I'm trying to write a program to control a little LED panel that I'm going to make up.

    Noone happens to have any links or tutorials they think I might find useful do they?
    Never argue with an idiot, they\'ll just bring you down to their level and then beat you with experience

  6. #6
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    hmm, that makes it look like I'm going to have to write my own device drivers to do what I want to do.
    What is it that you want to do? Writing device drivers is not desireable.

    AFAIK, you can do most things with the serial port without writing any weird drivers. There are more serial port functions, I just described the ones I thought you were likely to use.

    Slarty

  7. #7
    Junior Member
    Join Date
    Mar 2003
    Posts
    26
    Originally posted here by Fabs
    you see I'm trying to write a program to control a little LED panel that I'm going to make up.
    I want to be able to switch on and off individual LED's on the panel. I'm also unsure as to what sort of IC's I would need for the panel..
    Never argue with an idiot, they\'ll just bring you down to their level and then beat you with experience

  8. #8
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    You probly need a serial to parallel converter IC, and use the output bits to switch on/off the individual LED. Do a search on "serial to parallel". Or you may even try to control them via paralel (LPT) port...

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  9. #9
    Junior Member
    Join Date
    Mar 2003
    Posts
    26
    so why so I need to convert from serial to parallel? I don't know a whole lot about how com ports and such work.
    Never argue with an idiot, they\'ll just bring you down to their level and then beat you with experience

  10. #10
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    Writting apps to controll ( simple hardware ) via a parallel port is realy easy..

    the parallel port can easily be used fo 16bit output to your hardware..
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •