473,738 Members | 11,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SerialPort: DataReceived thread ...

What thread does the DataReceived event run on?

I'm preparing a design that uses the DataReceived event to
stimulate a protocol state machine (PSM). The ambition is to
have the serial process happen in the background, and when
certain conditions are met, the PSM then triggers an event
(myPacketIsHere for instance).

The PSM will only ever deal with the serial data sequentially,
i.e. through the use of locks and such, only one instance of the
PSM will run at any given time.

My questions then are:
* What thread does the DataReceivedEve ntHandler run on?
(Is it from the threadpool?)
* What's the best practiced method for having an event
handler keep state information between successive calls?
* When the PSM triggers its own event, what thread is the
event handler for that going to be running on.
(myPacketIsHere EventHandler())

If it isn't obvious that I'm weak on Delegates, and Threading,
I apologize ... the fact is I am.

- Jamie
Feb 26 '07 #1
3 11038
Hi,

The DataReceived event executes in the (background) thread context of the
SerialPort receive thread. All code called directly from the DataReceived
event executes in the background tread context.

You can keep state information in any suitable structure. Typically a set
of private (perhaps shared) variables, or in an object (Class, so that you
can encapsulate both data - states - and methods and events that might be
called depeding on state) that you might instatiate when you open the port
and begin communications. If you create an object to handle the PSM, it may
use either the background thread of the DataReceive event, or it can
delegate operations to a separate background thread.

The more threads used, the more complex becomes debugging. And, usually,
additional threads will not provide any improvement in performance or
maintainability (IMO).

BTW, the object method for creating a state machine is the approach that I
prefer, unless the number of states is fairly small. Realize that if your
DataReceived code or code in the PSM interact with the User Interface
(STAThread), then this interaction must be marshaled to the UI thread via a
delegate (Invoke or BeginInvoke).

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 #2
Thank you for the response. I have two more questions.
The DataReceived event executes in the (background) thread context of the
SerialPort receive thread. All code called directly from the DataReceived
event executes in the background tread context.
What about events published by the DataReceived handler. On
which thread are those events picked up? Will these be a
background thread as well?
BTW, the object method for creating a state machine is the approach that I
prefer, unless the number of states is fairly small. Realize that if your
DataReceived code or code in the PSM interact with the User Interface
(STAThread), then this interaction must be marshaled to the UI thread via a
delegate (Invoke or BeginInvoke).
I'm not sure what you mean by "the object method for creating a
state machine". Could you elaborate with, perhaps a pointer to
some reading material?

- Jamie
Feb 26 '07 #3
Hi,
>>
What about events published by the DataReceived handler. On
which thread are those events picked up? Will these be a
background thread as well?
<<

Yes (unless these events are specifically generated by code in a different
thread).
>>
I'm not sure what you mean by "the object method for creating a
state machine". Could you elaborate with, perhaps a pointer to
some reading material?
<<

What this means is that you create a class that implements all of the
features of your FSM (instead of inline code). Then, you create an instance
of that class when you start your program, perhaps when you open the port.
Methods in the class might send commands, wait responses, or process serial
data via a method that is called from the DataReceived event, where you pass
it the serial data that generated the event.

The class itself would have data that represents state (private variables)
and data that represent results that you want to relay to the calling
code -- and events for notification. Unless this class creates a seperate
thread (and it probably won't), any event notifications based on
DataReceived data will execute in the thread context of the background
thread.

I have a FSM that decodes GPS data (for example) in my book that uses this
form. I'm sure there are others around. However, these sort of things tend
to be "designed for a purpose" and while useful as guides will be just that.

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 27 '07 #4

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

Similar topics

0
1884
by: LordHog | last post by:
Hello all, I would like to use the new SerialPort class in Visual C++ 2005 Express edition, but I am having problems adding my event handler to the DataReceived event. In the header file I have the method that will handle the event defined as private: void SerialPortReceive_EventHandler( System::Object ^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e );
4
29283
by: Ben Kim | last post by:
Hello all, Does anyone have an example on how to implement the new SerialPort Class in VB.NET? I have been able to create the class, send (.WriteLine) to the port and Read from the port but cannot figure out how to use the Events to tell my program that data is available in the serial port buffer. I have searched the internet and MSDN and there doesn't appear to be any full example on how to do this simple task.
0
1189
by: no-one | last post by:
Hi Guys, I am fairly new to Dot Net and I am trying to write a program that listens to the serial port and displays the string in a text box or listbox. I am using VS 2005 and framework 2.0 my code is thus ( don't laugh to much) Public Class Form1 Delegate Sub SetTextCallback(ByVal As String)
5
2259
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 to one in a set of special characters - and of course synchronous and asynchronous using events and including timeouts. And even before RSX-11 could do this out of the box.
4
5550
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 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...
7
3943
by: Simon | last post by:
Hi all, I'm writing a PocketPC / CE application which communicates with a Bluetooth GPS reciever that is paired to a serial port but I'm experiencing odd behaviour from the CF 2.0 SerialPort class. Everything is fine first time around, but if I quit my application and restart a few times, pretty soon (usuaully the second or third restart) the DataReceived event stops firing on the SerialPort object.
5
6792
by: agloth | last post by:
Hi, I have a serial port reader application that uses datareceived event to read incoming data. The application also send response some of the messages. The problem is the application sometimes behaves weird. (My application does not respond some of the messages) And i think its because of the threading. I know that datareceived event uses a
0
1018
by: =?Utf-8?B?TGVuIFdlbHRtYW4=?= | last post by:
I've created MySerialPort, which inherits from SerialPort, and want to set an internal flag when DataReceived is signalled. If I were firing the event myself, it would be a simple matter of flag = true; if ( OnDataReceived != null) OnDataReceived(); The ugly way out is to create a public SetFlag() method and call it from the DataReceived callback routine in the main program. Another alternative is to poll for data myself and fire my...
0
2490
by: cronusf | last post by:
I set up two virtual COM ports 3 and 4 using com0com. I tried to test it with the following program. However, the DataReceived event handlers never get called. Can anyone with SerialPort class experience double check my code so I can narrow it down to com0com not being setup correctly? using System; using System.Collections.Generic; using System.IO.Ports; using System.IO;
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.