Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:07:03+00:00 2026-06-06T17:07:03+00:00

I’m developing an application that works with 8 COM-ports, when data is received from

  • 0

I’m developing an application that works with 8 COM-ports, when data is received from one of COM-ports, the event handler is called, the application creates the wrapper for message received via COM-port, and sends it via TCP to remote host. In fact after wrapping, it becomes a command for device somewhere in ethernet-based net (start measuring command).

I’m using SerialPort and SerialDataReceivedEventHandler for working with COM-ports. The problem is that I have an timeout (0-1000 ms) specified differently for every COM-port via form. After this timeout I have to send another command to get some data from device (get data command).

  • this command is different for every of 8 COM-ports
  • this command should be sended only after receiving data via COM-port and sending first command
  • timeout between two commands is different for every com port

Have you got any suggestions about timeout organization? Thanks.

I have one idea, but I’m not sure if it possible:
I have 8 event handler functions for every COM-port.

...something
sendFirstCommand();
Thread.Sleep(comPortNTimeout);
sendSecondCommand();

Can I use same construction in every of them? Won’t handler for 1st port be frozen, if data Thread.Sleep() would be called in handler for 2nd port for example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:07:07+00:00Added an answer on June 6, 2026 at 5:07 pm

    1) Introduce some kind of CommandSourceContext class which encapsulates all parameters related to each COM port (See below)

    2) To see whether handlers will freeze while an other port event has been raised – just test it in debugger and see whether all port events are invoked in the same thread. You can use Visual Studio 2010 Threads window, just put a break point in COM port event handler and see what is the current Thread Id. If you are using older Visual Studio – just log thread Id accessing it via Thread.CurrentThread.ManagedThreadId. So if events from diferent ports are invoked in the same thread – obviously handlers would block each one, otherwise would run in parallel since invoked in different threads. At least MSDN says (see at the bottom of this answer) that data received events are raised not in the main thread, so you have to be care when accessing UI controls.

    interface ICommandSourceContext
    {
       // Since each port has own specific command
       // we can encapsulate it in the context as well
       ICommand Command { get; }
    
       int PortNumber { get; }
       long TimeIntervalMilliseconds { get; }
       Action<SerialDataReceivedEventArgs> Callback { get; }     
    }
    
    // setup and add all contexts
    IList<ICommandSourceContext> contexts = new List<ICommandSourceContext>();
    
    // ideally your main code block should looks like below (this is only pseudo code)
    foreach (var context in contexts)
    {
       // to execute it asyncronously you can use TPL Task.Start()
       // so it would not block other handlers in case of single thread
       context.Command.Execute();
       Thread.Sleep(context.TimeIntervalMilliseconds);
    }
    

    SerialPort.DataReceived Event remarks:

    The DataReceived event is raised on a secondary thread when data is
    received from the SerialPort object. Because this event is raised on a
    secondary thread, and not the main thread, attempting to modify some
    elements in the main thread, such as UI elements, could raise a
    threading exception. If it is necessary to modify elements in the main
    Form or Control, post change requests back using Invoke, which will do
    the work on the proper thread

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.