Serial port in .NET

M

max_mont

Hi all,
I'm a newbie in .NET technology.
I've already developed Serial communication applications in C++
(WIN32).
And I wanted to migrate to .NET technology.
There is a serial component in framework to read and write on serial
port.
I would like to make asynchronous reception. I saw that we can pass a
delegate to the serial class which is call when some data is readen on
the port.
I suppose that .NET starts a thread to read permanently on the port.

What is the priority of this thread. Is there a way to be sure that
data will be readen before all other events like GUI events ?
How do the delegate work. Is it simply a pointer on a function which
is call or is it an event mechanism ?

Where can I find some details or explanations about that ?

Many thanks in advance for your help.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
max_mont said:
Hi all,
I'm a newbie in .NET technology.
I've already developed Serial communication applications in C++
(WIN32).
And I wanted to migrate to .NET technology.
There is a serial component in framework to read and write on serial
port.
I would like to make asynchronous reception. I saw that we can pass a
delegate to the serial class which is call when some data is readen on
the port.
I suppose that .NET starts a thread to read permanently on the port.

You have to check the docs, but must probably it's not there so you will
have to trust that it will call it :)
What is the priority of this thread.

Again, this is internal implementation, yo should not have to worry about
it. In case you need to know use a tool like reflector and analyze the code.
(not sure if you can legally do it even)
Is there a way to be sure that
data will be readen before all other events like GUI events ?

They are two different sources, why are you worried about it?
How do the delegate work. Is it simply a pointer on a function which
is call or is it an event mechanism ?

A event has a delegate as its type. It's basically a function pointer. You
should read the C# docs about it. MSDN has a good explanation about
events/delegates
 
M

max_mont

Hi,

--
Ignacio Machinhttp://www.laceupsolutions.com



You have to check the docs, but must probably it's not there so you will
have to trust that it will call it :)


Again, this is internal implementation, yo should not have to worry about
it. In case you need to know use a tool like reflector and analyze the code.
(not sure if you can legally do it even)


They are two different sources, why are you worried about it?


A event has a delegate as its type. It's basically a function pointer. You
should read the C# docs about it. MSDN has a good explanation about
events/delegates

Thanks for your response.

In fact, for the moment, I've just develop in C++ where I start a
thread with a high priority to read data on the serial port.
When some data is readen, I call a callback. As I know that my thread
priority is higher than main thread priority, I'm sure that all GUI
actions don't have impact on serial communication.

In .NET technology, I don't know what the serial port priority is.
The serial port sends an event, then a delegate is called, but WHEN ?
and by HOW ?
I'm not sure that for example if a user clicks every seconds on a
button, if the serial data reception won't be blocked.
In fact, in .NET, everything is managed by the framework. So we don't
know what the impact is on the real time aspect.
 
P

Peter Duniho

In fact, for the moment, I've just develop in C++ where I start a
thread with a high priority to read data on the serial port.
When some data is readen, I call a callback. As I know that my thread
priority is higher than main thread priority, I'm sure that all GUI
actions don't have impact on serial communication.

First, you should not be changing thread priorities without a really
good reason. And I don't think that handling serial i/o is a good
reason. Have you seen any indication of a performance issue without
changing the thread priority? Have you even written any .NET i/o code
yet?

Second, changing the thread priority does not ensure "that all GUI
actions don't have impact on serial communication". The Windows thread
scheduler will not preempt a thread that's already running even if a
higher priority thread becomes runnable. So, if data comes in on the
serial port while the GUI thread is already executing, it is
theoretically possible that that data will simply have to wait until
the GUI thread is done with its timeslice.

Also, if a higher priority thread is hogging the CPU, lower priority
threads eventually get a priority boost allowing them to run.

So, if it is possible at all for the GUI thread to interfere with the
serial i/o (which seems unlikely to me, but for the sake of
argument...), raising the thread priority for the serial i/o is not
guaranteed to eliminate that. If it were possible, raising the thread
priority could improve the situation, but it can't fix it completely.

But it's very unlikely that you need to change the thread priority
anyway. The serial i/o is (or at least should be) buffered, and
there's no reason at all that your serial i/o should not be able to be
processed frequently enough to keep up with the i/o rate even when
running at the normal thread priority.
In .NET technology, I don't know what the serial port priority is.

The serial port doesn't have a priority. A thread handling serial i/o
may have a priority, and you could easily find this out by using the
Thread class to check the current priority when handling serial i/o in
..NET. My guess is that it's normal priority, because there shouldn't
be any need to use anything different.
The serial port sends an event, then a delegate is called, but WHEN ?

I don't know the exact implementation of the .NET serial i/o class, but
I'd say there's a pretty good chance that it uses i/o completion ports.
Whether it's using IOCP or not, it's likely that some thread waiting
on the i/o is made runnable by the arrival of data, and will be
scheduled normally according to its position in the round-robin order
of runnable threads. When that thread is run, that would be when the
delegate is called.
and by HOW ?

By calling the delegate, just as any delegate would be called. As you
suggested in your original post, a delegate is very much like a
function pointer. It's basically the .NET equivalent to one, and when
code calls a delegate it's calling it directly from somewhere.

Now, some delegates are called via the Windows event queue (for
example, delegates passed to Control.Invoke()), so in that sense
sometimes they are like "an event mechanism". But that's not really
something inherent in delegates themselves; it's part of the
Control.Invoke() mechanism, or similar situations.
I'm not sure that for example if a user clicks every seconds on a
button, if the serial data reception won't be blocked.

Have you tried it? As long as you don't do anything bad in your GUI
code, it will work fine. By "bad", mostly I mean that as long as you
don't perform any time-consuming operations in the GUI code, there's no
chance of a problem. Even if you do perform time-consuming operations
in the GUI code, I suspect that the likelihood of a problem is very
low. The i/o won't get handled as often, but it's unusual for an i/o
thread to consume its entire timeslice (assuming all it's doing is
i/o), so it's likely the net throughput will still be the same, just in
larger bursts.
In fact, in .NET, everything is managed by the framework. So we don't
know what the impact is on the real time aspect.

There is no "real time" in Windows. Windows isn't a real-time OS, and
boosting thread priority in an attempt to make it one isn't going to
work.

What you should do is use the asynchronous methods on the serial class
that you've seen (start with "Begin..." and "End..."). Assuming the
serial class uses IOCP, it's not true that there's a thread dedicated
to a specific call to a "Begin..." method. Instead, a pool of threads
will be waiting to dequeue i/o completions. Windows will manage these
efficiently to ensure that a given thread continues to run in its
timeslice as long as there's some i/o that's completed and ready to
process.

You should not mess with thread priority. For a serial port
especially, the normal asynchronous i/o methods should provide plenty
of performance without worrying at all about thread priority.

Pete
 
M

max_mont

First, you should not be changing thread priorities without a really
good reason. And I don't think that handling serial i/o is a good
reason. Have you seen any indication of a performance issue without
changing the thread priority? Have you even written any .NET i/o code
yet?

Second, changing the thread priority does not ensure "that all GUI
actions don't have impact on serial communication". The Windows thread
scheduler will not preempt a thread that's already running even if a
higher priority thread becomes runnable. So, if data comes in on the
serial port while the GUI thread is already executing, it is
theoretically possible that that data will simply have to wait until
the GUI thread is done with its timeslice.

Also, if a higher priority thread is hogging the CPU, lower priority
threads eventually get a priority boost allowing them to run.

So, if it is possible at all for the GUI thread to interfere with the
serial i/o (which seems unlikely to me, but for the sake of
argument...), raising the thread priority for the serial i/o is not
guaranteed to eliminate that. If it were possible, raising the thread
priority could improve the situation, but it can't fix it completely.

But it's very unlikely that you need to change the thread priority
anyway. The serial i/o is (or at least should be) buffered, and
there's no reason at all that your serial i/o should not be able to be
processed frequently enough to keep up with the i/o rate even when
running at the normal thread priority.


The serial port doesn't have a priority. A thread handling serial i/o
may have a priority, and you could easily find this out by using the
Thread class to check the current priority when handling serial i/o in
.NET. My guess is that it's normal priority, because there shouldn't
be any need to use anything different.


I don't know the exact implementation of the .NET serial i/o class, but
I'd say there's a pretty good chance that it uses i/o completion ports.
Whether it's using IOCP or not, it's likely that some thread waiting
on the i/o is made runnable by the arrival of data, and will be
scheduled normally according to its position in the round-robin order
of runnable threads. When that thread is run, that would be when the
delegate is called.


By calling the delegate, just as any delegate would be called. As you
suggested in your original post, a delegate is very much like a
function pointer. It's basically the .NET equivalent to one, and when
code calls a delegate it's calling it directly from somewhere.

Now, some delegates are called via the Windows event queue (for
example, delegates passed to Control.Invoke()), so in that sense
sometimes they are like "an event mechanism". But that's not really
something inherent in delegates themselves; it's part of the
Control.Invoke() mechanism, or similar situations.


Have you tried it? As long as you don't do anything bad in your GUI
code, it will work fine. By "bad", mostly I mean that as long as you
don't perform any time-consuming operations in the GUI code, there's no
chance of a problem. Even if you do perform time-consuming operations
in the GUI code, I suspect that the likelihood of a problem is very
low. The i/o won't get handled as often, but it's unusual for an i/o
thread to consume its entire timeslice (assuming all it's doing is
i/o), so it's likely the net throughput will still be the same, just in
larger bursts.


There is no "real time" in Windows. Windows isn't a real-time OS, and
boosting thread priority in an attempt to make it one isn't going to
work.

What you should do is use the asynchronous methods on the serial class
that you've seen (start with "Begin..." and "End..."). Assuming the
serial class uses IOCP, it's not true that there's a thread dedicated
to a specific call to a "Begin..." method. Instead, a pool of threads
will be waiting to dequeue i/o completions. Windows will manage these
efficiently to ensure that a given thread continues to run in its
timeslice as long as there's some i/o that's completed and ready to
process.

You should not mess with thread priority. For a serial port
especially, the normal asynchronous i/o methods should provide plenty
of performance without worrying at all about thread priority.

Pete

Thank you very much for your complete response.
I think I will use the serial port with Delegate Event and I will see
if there isn't any problem.
I'm not worried but I developed in c/c++ language where I knew all
mechanisms.
By migrating to .NET, I would like to know how it's working.
By migrating to .NET, a very higher language than C/C++, we change the
philisophy of development.
 
P

Peter Duniho

Thank you very much for your complete response.

You're welcome.
I think I will use the serial port with Delegate Event and I will see
if there isn't any problem.

Good idea. :)
I'm not worried but I developed in c/c++ language where I knew all
mechanisms.

By that, I assume you mean "I knew all the mechanisms involved in my
implementation". You could have implemented your i/o in C++ using
IOCP, and if you had done so then I believe there would be practically
no difference in the actual implementation between that approach and
using the .NET asynchronous methods.
By migrating to .NET, I would like to know how it's working.

You may want to check out a tool called Reflector if you want the gory
details. I suspect that you'll find that beneath the .NET API, there's
a very conventional and good-performing i/o implementation.
By migrating to .NET, a very higher language than C/C++, we change the
philisophy of development.

Yes, definitely true. But hopefully it's changed for the better. That
is, you can leave behind many of the low-level optimization concerns,
trusting the framework to address those issues for you, and focusing
instead of higher-level algorithm design. There are still plenty of
performance "gotchas" to be found in .NET, but most of the time one
should be able to assume that if there's a significantly superior way
to implement some basic functionality, that's how .NET implements it.

And if you do run into a situation where that's not the case, report it
to Microsoft as a bug. :)

Pete
 
M

max_mont

You're welcome.


Good idea. :)


By that, I assume you mean "I knew all the mechanisms involved in my
implementation". You could have implemented your i/o in C++ using
IOCP, and if you had done so then I believe there would be practically
no difference in the actual implementation between that approach and
using the .NET asynchronous methods.


You may want to check out a tool called Reflector if you want the gory
details. I suspect that you'll find that beneath the .NET API, there's
a very conventional and good-performing i/o implementation.


Yes, definitely true. But hopefully it's changed for the better. That
is, you can leave behind many of the low-level optimization concerns,
trusting the framework to address those issues for you, and focusing
instead of higher-level algorithm design. There are still plenty of
performance "gotchas" to be found in .NET, but most of the time one
should be able to assume that if there's a significantly superior way
to implement some basic functionality, that's how .NET implements it.

And if you do run into a situation where that's not the case, report it
to Microsoft as a bug. :)

Pete

I surfed on the website of refletor tool and reflector add-in. It
seems to be a powerful tool for .NET developer.
I'm going immediatly to use it.
Thank you very much
 

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