Timer won't start from serial port receive routine

S

seegoon

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
 
A

Armin Zingler

seegoon said:
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.Timer. 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
 
D

Dick Grier

Hi,

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

To start timing, the call Now.TimeOfDay.TotalMilliseconds 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.
 
S

seegoon

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.Timer.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.timers.timer) , and it works great.
Is there a more elegant way of handling unknown serial data lengths?

Cheers
Rob
 
S

seegoon

Hi,

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

To start timing, the call Now.TimeOfDay.TotalMilliseconds 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.hardandsoftware.netfor details and contact information.

Thanks , I'll look at this as an option.
Cheers
Rob
 
A

Armin Zingler

seegoon said:
Used a different timer(system.timers.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
 
D

Dick Grier

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.
 
J

James Hahn

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.


Thanks.
Used a different timer(system.timers.timer) , and it works great.
Is there a more elegant way of handling unknown serial data lengths?

Cheers
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top