473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Oct 29 '08 #1
7 3739
"seegoon" <se*******@yahoo.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.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

Oct 29 '08 #2
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.
Oct 29 '08 #3
On Oct 29, 2:04*pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"seegoon" <seegoo...@yahoo.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.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
Oct 30 '08 #4
On Oct 29, 5:55*pm, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
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
Oct 30 '08 #5
"seegoon" <se*******@yahoo.comschrieb
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

Oct 30 '08 #6
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.
Oct 30 '08 #7
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" <se*******@yahoo.comwrote in message
news:95**********************************@l42g2000 hsc.googlegroups.com...
On Oct 29, 2:04 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
snip <
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

Oct 30 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Richard | last post by:
I'm new to this type of programming, so please bear with me. I just came over from Top Down style Pascal. (Yes lots of books, evidently the wrong books). My program will read data from an ADC...
0
by: Haluk Gokmen | last post by:
I had an inquiry about the use of MSComm OCX in a C# application awhile ago and I really appreciated Nicholas Paldino's help, which is listed below... My code worked well in VS2003. I am now...
7
by: Michael Chong | last post by:
I wrote a program that communicate with SerialComm. In every 300 milliseconds, my program continuously send & receive data via the serial port once the program starts. My program is once in a...
7
by: Noozer | last post by:
I have a timer on a form. It isn't firing at all. I know that the timer is enabled, and that the interval is low (4000, which should be 4 seconds). To ensure the timer wasn't being inadvertantly...
4
by: wg | last post by:
Can someone help me with this. I have a application in which a timer runs all the time. I set some flags in side the timer so there is nothing inside to cause a problem. However if I run the app,...
3
by: geskerrett | last post by:
We have been asked to develop and application for a client that is a 'notification" system. We would like to use python, but are struggling to find the right starting point. Any suggestions, tips...
4
by: Jamie Risk | last post by:
I'm trying to implement a protocol that has a concept of a GAP timer in the serial stream; if two properly framed characters are spaced in time by so many milliseconds or longer, there is an...
2
by: Lou | last post by:
I have a class that creates an instance of the seril Port. Every thing works fine except whenever I receive data I cannot display the recieved data. I get no errors but the recived data seems to...
3
by: Beemer Biker | last post by:
Unaccountably, I cannot re-enable a timer from an background thread. The disable works fine, I just cannot get it to start back up. There is no method "InvokeRequired" like there is for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.