473,386 Members | 1,757 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,386 software developers and data experts.

SerialPort: How do I time the arrival between characters on a serialstream?

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

So far I'm looking at using the SerialPort.DataReceived event.
It's nice because I can suck in bytes filter it through a state
machine to see if packets are properly framed than raise an
event. The problem as I understand it, the DataReceived event
is minimally guaranteed to fire every time it gets
ReceivedBytesThreshold.

This doesn't help me, if I set the threshold to one (1), on the
odd occasion my event handler may be dealing with two or more
characters in the buffer. All this to write that the
DataReceived event isn't a perfect solution for implementing a
GAP timer.

I'm not comfortable with the idea of using the
SerialPort.ReadTimeout mechanism for two reasons:
1. I'm not sure it times what I want it too (could
someone fill me - see the post script?)
2. The DataReceived event mechanism looks like it would work
more nicely with threading. (As opposed to continually
calling SerialPort.ReadByte and relying on the timeouts.

I'm really, _really_ new at this .Net/C# stuff, design help
would be appreciated.

- Jamie
p.s. Does the ReadTimeout mechanism start counting during some
point to the call of ReadByte/Char/Existing/Line/To?
Feb 23 '07 #1
4 5517
Hi,

With what resolution do you need to do your timing?

Windows provides absolutely no guarantee of any real-time performance. The
problems with serial data timing are manifest. I'll list some of them here.

1. The Windows serial driver provides the interrupt service routine that
recevies data from the UART, and buffers receive data until an application
requests it. This driver can provide notificatations to applications (via
events) that data have arrived, but this notification is done with no
schedule or timing guarantee.
2. The serial port implements a receive FIFO. Nominally, the receive FIFO
depth is 16-bytes, with a threshold of 14-bytes. What this means is that
the ISR may not receive a notification of data arrival for up to 14-byte
times (or with some hardware variations that implement much deeper FIFOs...
much longer times).
3. Your application is multitasking (with other applications), and the
SerialPort object is multithreaded (with other threads in the application).
These mechanisms are non-determistic. How long it takes to execute any
portion of them is how long it takes (sometimes more).

Perhaps you get the idea? If you are looking for gross timing, say 20 mS
data gaps, then you may be able to implement the code that does the job .
Most of the time. How reliably? Only testing will tell you. If you want
to measure gaps in data smaller than that, I'd speculate that the
reliability might be problematic.

In any case, you might want to set the UART receive FIFO threshold to 1
(from the default 14). There is no API for this that I know of, so I think
you would have to do it manually using Control Panel/Device Manager.

BTW, ReadTimeout offers nothing to help you here.

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.
Feb 23 '07 #2
I should have added...

The only truely reliable way to get this to work would be to implement the
receive protocol on a microcontroller (IMO). If the micro had two ports,
one could be used for the timing oriented protocol, and the other could be
used to communicate with the host PC, implementing a more robust non-timing
oriented packet protocol. This is a non-trivial project, but would result
in something that would work as reliably as might be desired.

--
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.
Feb 23 '07 #3
Thanks for the quick response.

I'm not to keen to go beyond .Net framework. Insomuch as the
micro controller, I'm an embedded systems designer by trade so
implementation wouldn't be a problem - I just don't want to get
into custom cabling/power etc..

I really only posted to see if there was a method .Net provided
that I hadn't discovered.

The error timeout is about 2 milliseconds. If I time between
hits on the DataReceived event handler I'm sure I can sell as
much as 50 milliseconds latency, which for this purpose, is
still much faster than human perception.

- Jamie
Feb 23 '07 #4
Hi,

2 mS is a rather short time in a non-deterministic system, even on a fast
machine, when dealing with serial data. My guess is that it would work
(most, perhaps 99%) of the time without any problem. What happens in the
other 1% of the time? Can you live with an occasional "miss?"

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.
Feb 26 '07 #5

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

Similar topics

2
by: Jan | last post by:
Hello there, I've got a strange problem using the readline method in vb2005 for reading the serial port. If I use the readchar method I can receive one character from serialport. This is the...
5
by: herbert | last post by:
back in 1978 (!) the VAX/VMS serial line driver offered everything a developer needs to develop protocols of all kinds: - read x bytes - read to end of line - read to special character - read...
3
by: Magne | last post by:
Using SerialPort ReceivedBytesThreshold = 1 Handling DataReceived event as: int noOfBytesInBuffer = myPort.BytesToRead; byte myBuffer = new byte; myPort.Read(myBuffer, 0, noOfBytesInBuffer) ...
1
by: sranger | last post by:
When I try to use a System.IO.Ports.SerialPort object to read from a serial port, half of the time I have no problems. However, after I successfully read from the serial port, then close the...
0
by: ricolee99 | last post by:
Hi Everyone, I'm using the System.IO.Ports.SerialPort component to attempt to kill an existing component opened by another application. I use the following code: SerialPort serialPort = new...
3
by: arjan321 | last post by:
Hello all, I have a strange problem with the .NET serialPort class. When I write some data to the serialport, not all the data is immediately send: every once in a while, only ~50 characters...
6
by: Keith Lee | last post by:
All: I am attempting to compile the perl module Device::SerialPort and get this error during make -- Manifying blib/man3/Device::SerialPort.3pm Can't open blib/man3/Device::SerialPort.3pm for...
3
by: Steve | last post by:
I am trying to calculate elapsed travel times for flights. My plan is to enter the local departure time, the departure city and the local arrival time and city. These times would be standardised...
8
by: Kevin.M.Jonas | last post by:
I have the following method that handles the data received event: private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { string data = _port.ReadExisting();...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.