Timer won't start from serial port receive routine

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seegoon

    #1

    Timer won't start from serial port receive routine

    Hi to all.
    I have a application that receives data via the serial port. I don't
    know the number of bytes that are going to
    be coming in. It could be 10 up to 150. (9600 baud)
    I have decided to start a timer when the first byte arrives and wait
    for a pre determined time and then check
    the serial buffer for the data.
    I can't seem to get the timer to start from within the serial receive
    routine though.
    I enable it , start it and set the interval , but tick event never
    fires.
    What could be causing this?
    Also , is there a better way of tackling this problem , without the
    use of a timer?

    Cheers
    Rob
  • Armin Zingler

    #2
    Re: Timer won't start from serial port receive routine

    "seegoon" <seegoon99@yaho o.comschrieb
    Hi to all.
    I have a application that receives data via the serial port. I don't
    know the number of bytes that are going to
    be coming in. It could be 10 up to 150. (9600 baud)
    I have decided to start a timer when the first byte arrives and wait
    for a pre determined time and then check
    the serial buffer for the data.
    I can't seem to get the timer to start from within the serial
    receive routine though.
    I enable it , start it and set the interval , but tick event never
    fires.
    What could be causing this?
    Also , is there a better way of tackling this problem , without the
    use of a timer?
    Haven't done it yet, but the example in the documentation reads in another
    thread in a loop. You can specify a Timeout while reading. (ReadTimeout
    property).

    Independently answering the question about the Timer: you should mention
    which kind of Timer. There are three. I guess it's a Windows.Forms.T imer. I
    also guess you start it in the DataReceived event. Then, it doesn't work
    because, as the documentation on the DataReceived event says, the event is
    raised in another thread. That thread does not have a message loop like your
    main thread. Therefore, a Forms.Timer is not the right choice.


    Armin

    Comment

    • Dick Grier

      #3
      Re: Timer won't start from serial port receive routine

      Hi,

      Don't use a timer. Use code. For example,
      Dim StartTime As Long = Now.TimeOfDay.T otalMillisecond s

      To start timing, the call Now.TimeOfDay.T otalMillisecond s subsequently, and
      subtract StartTime from it to measure elaplsed time. I do this to handle
      similar protocols, and it works well.

      Dick
      --
      Richard Grier, MVP
      Hard & Software
      Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
      Edition,
      ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
      2006.
      See www.hardandsoftware.net for details and contact information.


      Comment

      • seegoon

        #4
        Re: Timer won't start from serial port receive routine

        On Oct 29, 2:04 pm, "Armin Zingler" <az.nos...@free net.dewrote:
        "seegoon" <seegoo...@yaho o.comschrieb
        >
        Hi to all.
        I have a application that receives data via the serial port. I don't
        know the number of bytes that are going to
        be coming in. It could be 10 up to 150. (9600 baud)
        I have decided to start a timer when the first byte arrives and wait
        for a pre determined time and then check
        the serial buffer for the data.
        I can't seem to get the timer to start from within the serial
        receive routine though.
        I enable it , start it and set the interval , but tick event never
        fires.
        What could be causing this?
        Also , is there a better way of tackling this problem , without the
        use of a timer?
        >
        Haven't done it yet, but the example in the documentation reads in another
        thread in a loop. You can specify a Timeout while reading. (ReadTimeout
        property).
        >
        Independently answering the question about the Timer: you should mention
        which kind of Timer. There are three. I guess it's a Windows.Forms.T imer.I
        also guess you start it in the DataReceived event. Then, it doesn't work
        because, as the documentation on the DataReceived event says, the event is
        raised in another thread. That thread does not have a message loop like your
        main thread. Therefore, a Forms.Timer is not the right choice.
        >
        Armin
        Thanks.
        Used a different timer(system.ti mers.timer) , and it works great.
        Is there a more elegant way of handling unknown serial data lengths?

        Cheers
        Rob

        Comment

        • seegoon

          #5
          Re: Timer won't start from serial port receive routine

          On Oct 29, 5:55 pm, "Dick Grier" <dick_grierNOSP AM@.msn.comwrot e:
          Hi,
          >
          Don't use a timer.  Use code.  For example,
          Dim StartTime As Long = Now.TimeOfDay.T otalMillisecond s
          >
          To start timing, the call Now.TimeOfDay.T otalMillisecond s subsequently, and
          subtract StartTime from it to measure elaplsed time.    I do this to handle
          similar protocols, and it works well.
          >
          Dick
          --
          Richard Grier, MVP
          Hard & Software
          Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
          Edition,
          ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
          2006.
          Seewww.hardands oftware.netfor details and contact information.
          Thanks , I'll look at this as an option.
          Cheers
          Rob

          Comment

          • Armin Zingler

            #6
            Re: Timer won't start from serial port receive routine

            "seegoon" <seegoon99@yaho o.comschrieb
            Used a different timer(system.ti mers.timer) , and it works great.
            Is there a more elegant way of handling unknown serial data lengths?
            My latest serial port experience is from VB6 times. :) I'd do it as shown in
            the example that I mentioned: Run in a separate thread and read and read
            and.... add everything to your buffer and do with it what you need.


            Armin

            Comment

            • Dick Grier

              #7
              Re: Timer won't start from serial port receive routine

              You can do what I suggested (I do this on occasion), or use
              Windows.Timers. Timer. I also use it. Some care is required, because of
              threading issues, but it should be OK, too.

              Dick

              --
              Richard Grier, MVP
              Hard & Software
              Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
              Edition,
              ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
              2006.
              See www.hardandsoftware.net for details and contact information.


              Comment

              • James Hahn

                #8
                Re: Timer won't start from serial port receive routine

                It depends on the message. For instance, GPS data is variable length, but
                since the message format is defined by the header label, it's relatively
                easy to detect the end of the message. Just keep retrieving data from the
                buffer and adding it to a data queue. Handle the serial port in the
                separate thread and add received characters to the queue as received. When
                the data in the queue is a complete message remove it from the queue and
                process it. Or, if your messages have an end of message indicator, process
                into the queue until the EoM indicator appears, then remove and process the
                queue contents. You don't need a timer unless you want to watch for
                interruptions to the transmission, or perhaps you want to process a partial
                message if the remainder isn't received within a reasonable time (for GPS,
                partial messages are usually discarded, as they can't be checked for
                accuracy). Depending on what other processing is required, you may also want
                to use a timer to control how frequently the queue is checked for a complete
                message.

                "seegoon" <seegoon99@yaho o.comwrote in message
                news:95a5aa04-11f4-4003-b74c-b9df23a2b15a@l4 2g2000hsc.googl egroups.com...
                On Oct 29, 2:04 pm, "Armin Zingler" <az.nos...@free net.dewrote:
                snip <
                Thanks.
                Used a different timer(system.ti mers.timer) , and it works great.
                Is there a more elegant way of handling unknown serial data lengths?

                Cheers
                Rob

                Comment

                Working...