Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Microsecond timer

Microsecond timer

Scheduled Pinned Locked Moved C / C++ / MFC
19 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A azhari24

    Hi... I have a program to read data from serial port,and I want to repeat the program every 1 microsecond. this is my program, but I just can repeat every 1 milisecond

    void CStripDlg::OnTimer(UINT nIDEvent)
    {
    int dcount=0;
    CString data[4];
    VARIANT in_dat;

    {
    		in\_dat = m\_comm.GetInput(); // read port
    		CString strInput(in\_dat.bstrVal);
    		m\_input.Format("%s", strInput); // show data
    		m\_comm.SetInBufferCount(0); // clear buffer
    		}
    }
    		UpdateData(FALSE);
    CDialog::OnTimer(nIDEvent);
    

    void CStripDlg::OnStart()
    {
    SetTimer(1,1,NULL); // set data 1 milisecond
    }
    }

    thx

    A Offline
    A Offline
    azhari24
    wrote on last edited by
    #8

    microcontroller send data every 2 ms, my problem when the microcontroller gets the input, the program can not display the data directly, but must read the entire remainder of the data in the buffer. this is setting port, and recevied data. I use Microsoft Communication Control

    	TRY
    {
    m\_comm.SetCommPort(12); // use port 12
    m\_comm.SetSettings("9600,N,8,1"); // setting port
    m\_comm.SetInputLen(1); // read 1 character
    m\_comm.SetRTSEnable(FALSE);
    m\_comm.SetRThreshold(0);
    m\_comm.SetPortOpen(true); // open port
    UpdateData(FALSE);
    	MessageBox("Port opened successfully");
    }
    CATCH(CException, e)
    {
    	MessageBox("Error opening port");
    }
    END\_CATCH
    

    so I use the clear buffer to clear the buffer. but rather the execution time becomes slow and missing data

    m\_comm.SetInBufferCount(0); // clear buffer
    		}
    

    can you offer some advice?

    L 1 Reply Last reply
    0
    • A azhari24

      microcontroller send data every 2 ms, my problem when the microcontroller gets the input, the program can not display the data directly, but must read the entire remainder of the data in the buffer. this is setting port, and recevied data. I use Microsoft Communication Control

      	TRY
      {
      m\_comm.SetCommPort(12); // use port 12
      m\_comm.SetSettings("9600,N,8,1"); // setting port
      m\_comm.SetInputLen(1); // read 1 character
      m\_comm.SetRTSEnable(FALSE);
      m\_comm.SetRThreshold(0);
      m\_comm.SetPortOpen(true); // open port
      UpdateData(FALSE);
      	MessageBox("Port opened successfully");
      }
      CATCH(CException, e)
      {
      	MessageBox("Error opening port");
      }
      END\_CATCH
      

      so I use the clear buffer to clear the buffer. but rather the execution time becomes slow and missing data

      m\_comm.SetInBufferCount(0); // clear buffer
      		}
      

      can you offer some advice?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #9

      What sort of HW is it, Who makes it? Doesnt it come with a proper driver or some other interface to the system? I cant believe any firm can sell HW that is pumping data that quick to a com port, it just isnt going to get serviced in windows. Is there perhaps a configuraiton to the device whereby you can increase the receive buffer or some such?

      ============================== Nothing to say.

      1 Reply Last reply
      0
      • A azhari24

        Hi... I have a program to read data from serial port,and I want to repeat the program every 1 microsecond. this is my program, but I just can repeat every 1 milisecond

        void CStripDlg::OnTimer(UINT nIDEvent)
        {
        int dcount=0;
        CString data[4];
        VARIANT in_dat;

        {
        		in\_dat = m\_comm.GetInput(); // read port
        		CString strInput(in\_dat.bstrVal);
        		m\_input.Format("%s", strInput); // show data
        		m\_comm.SetInBufferCount(0); // clear buffer
        		}
        }
        		UpdateData(FALSE);
        CDialog::OnTimer(nIDEvent);
        

        void CStripDlg::OnStart()
        {
        SetTimer(1,1,NULL); // set data 1 milisecond
        }
        }

        thx

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #10

        You need to do the following. 1. Determine the actual baud rate. In your other code you suggested 9600. Presumably that is correct. 2. Create a thread. 3. That thread does the following and NOTHING else. a. Block on the serial port. (You don't use a timer but rather you wait until data is available.) b. Read one byte c. Add that byte to a thread safe queue. d. Go back to a. 4. In your GUI code (or wherever) you read data from the queue and do whatever you want with it. Note that this occurs in a different thread.

        enhzflepE L A 3 Replies Last reply
        0
        • J jschell

          You need to do the following. 1. Determine the actual baud rate. In your other code you suggested 9600. Presumably that is correct. 2. Create a thread. 3. That thread does the following and NOTHING else. a. Block on the serial port. (You don't use a timer but rather you wait until data is available.) b. Read one byte c. Add that byte to a thread safe queue. d. Go back to a. 4. In your GUI code (or wherever) you read data from the queue and do whatever you want with it. Note that this occurs in a different thread.

          enhzflepE Offline
          enhzflepE Offline
          enhzflep
          wrote on last edited by
          #11

          Where's the 'Great Answer' button? My 5.

          1 Reply Last reply
          0
          • J jschell

            You need to do the following. 1. Determine the actual baud rate. In your other code you suggested 9600. Presumably that is correct. 2. Create a thread. 3. That thread does the following and NOTHING else. a. Block on the serial port. (You don't use a timer but rather you wait until data is available.) b. Read one byte c. Add that byte to a thread safe queue. d. Go back to a. 4. In your GUI code (or wherever) you read data from the queue and do whatever you want with it. Note that this occurs in a different thread.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #12

            jschell wrote:

            b. Read one byte

            You want to read the FIFO taking out as many bytes at a time as you can. It is only for special control characters that you need to read individual bytes, such as DTR RTS CTS etc etc etc and you set those in the 'wait on mask'.

            ============================== Nothing to say.

            modified on Tuesday, September 13, 2011 3:16 AM

            P J A 3 Replies Last reply
            0
            • L Lost User

              jschell wrote:

              b. Read one byte

              You want to read the FIFO taking out as many bytes at a time as you can. It is only for special control characters that you need to read individual bytes, such as DTR RTS CTS etc etc etc and you set those in the 'wait on mask'.

              ============================== Nothing to say.

              modified on Tuesday, September 13, 2011 3:16 AM

              P Offline
              P Offline
              pandit84
              wrote on last edited by
              #13

              Few months back I was having the same issue of Microsecond. Windows does not provide granularity of 1 microsecond. For this reason we have used Real Time OS which is an extension to Microsoft Windows . The Timer granularity is well handled by RTOS ( provides upto 1 Microsecond , which can further modified by modifying the value of 'Ticks' of clock using some API of that RTOS.

              A 1 Reply Last reply
              0
              • P pandit84

                Few months back I was having the same issue of Microsecond. Windows does not provide granularity of 1 microsecond. For this reason we have used Real Time OS which is an extension to Microsoft Windows . The Timer granularity is well handled by RTOS ( provides upto 1 Microsecond , which can further modified by modifying the value of 'Ticks' of clock using some API of that RTOS.

                A Offline
                A Offline
                azhari24
                wrote on last edited by
                #14

                you have any articles or sample programs?and Can I ask articles or sample program?

                1 Reply Last reply
                0
                • L Lost User

                  jschell wrote:

                  b. Read one byte

                  You want to read the FIFO taking out as many bytes at a time as you can. It is only for special control characters that you need to read individual bytes, such as DTR RTS CTS etc etc etc and you set those in the 'wait on mask'.

                  ============================== Nothing to say.

                  modified on Tuesday, September 13, 2011 3:16 AM

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #15

                  Fat__Eric wrote:

                  You want to read the FIFO taking out as many bytes at a time as you can

                  I presume my answer wasn't clear. If you block on a read and there is in fact data available then you do not block. So many bytes are read. And a serial port is a single byte stream. So excluding an intermediate buffer mechanism it is only going to read one byte at a time. But if the API supports a buffered read then using that is certainly an option.

                  A L 2 Replies Last reply
                  0
                  • J jschell

                    You need to do the following. 1. Determine the actual baud rate. In your other code you suggested 9600. Presumably that is correct. 2. Create a thread. 3. That thread does the following and NOTHING else. a. Block on the serial port. (You don't use a timer but rather you wait until data is available.) b. Read one byte c. Add that byte to a thread safe queue. d. Go back to a. 4. In your GUI code (or wherever) you read data from the queue and do whatever you want with it. Note that this occurs in a different thread.

                    A Offline
                    A Offline
                    azhari24
                    wrote on last edited by
                    #16

                    okey... thank's. i will try your suggest...

                    1 Reply Last reply
                    0
                    • J jschell

                      Fat__Eric wrote:

                      You want to read the FIFO taking out as many bytes at a time as you can

                      I presume my answer wasn't clear. If you block on a read and there is in fact data available then you do not block. So many bytes are read. And a serial port is a single byte stream. So excluding an intermediate buffer mechanism it is only going to read one byte at a time. But if the API supports a buffered read then using that is certainly an option.

                      A Offline
                      A Offline
                      azhari24
                      wrote on last edited by
                      #17

                      okey, thanks do you have sample program?

                      modified on Wednesday, September 14, 2011 1:17 AM

                      1 Reply Last reply
                      0
                      • L Lost User

                        jschell wrote:

                        b. Read one byte

                        You want to read the FIFO taking out as many bytes at a time as you can. It is only for special control characters that you need to read individual bytes, such as DTR RTS CTS etc etc etc and you set those in the 'wait on mask'.

                        ============================== Nothing to say.

                        modified on Tuesday, September 13, 2011 3:16 AM

                        A Offline
                        A Offline
                        azhari24
                        wrote on last edited by
                        #18

                        okey... thank...

                        1 Reply Last reply
                        0
                        • J jschell

                          Fat__Eric wrote:

                          You want to read the FIFO taking out as many bytes at a time as you can

                          I presume my answer wasn't clear. If you block on a read and there is in fact data available then you do not block. So many bytes are read. And a serial port is a single byte stream. So excluding an intermediate buffer mechanism it is only going to read one byte at a time. But if the API supports a buffered read then using that is certainly an option.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #19

                          jschell wrote:

                          And a serial port is a single byte stream. So

                          Very very few are. As stated a single byte rcv buffer UART is a very shonky piece of hardware. The stock (what is it, 82530 or some such compatible UART on most PCs) has an 8 byte RX FIFO. Better quality UARTs on serial PCI cards will have far bigger FIFOs.

                          ============================== Nothing to say.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups