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

  • Home
  • SEARCH
  • 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 6214221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:50:19+00:00 2026-05-24T06:50:19+00:00

Currently our application connects to an Arduino over a serial port. We send some

  • 0

Currently our application connects to an Arduino over a serial port. We send some ASCII-formatted commands, and get the same in return. To do this, we have a queue of commands, a thread dedicated to writing those commands to the port, and a thread dedicated to reading and handling all incoming replies. The class itself is responsible for dispatching the replies, which is giving it way too much responsibility (should just be responsible for port operations, not business logic).

We would rather do this in an async manner. Anything in the system can send a command with a callback function and a timeout. If the serial port gets a correct reply, it calls the callback function. Otherwise, it times out and maybe calls a second callback (or possibly a single callback with a succeeded? flag).

However, we’ve only ever consumed async methods (particularly in web operations), not written such a system. Can anyone give us some pointers about how to proceed?

Our current plan is to store a queue of these commands. Upon any reply, if an associated command is found (by comparing ASCII values) it is dequeued and the callback is executed. A timer will periodically check for timeouts, dequeue, and execute the appropriate callback. It seems like a straightforward solution, but the amount of code to support this is increasing substantially and we wanted to ensure there weren’t any better built-in solutions or best practices for this.

Edit: To clarify further, this particular class is a singleton (for better or worse), and there are many other threads running that could access it. For example, one thread may want to request a sensor value, while another thread could be controlling a motor. These commands and their associated replies do not happen in a linear fashion; the timing may be reversed. Thus, a traditional producer-consumer model is not enough; this is more of a dispatcher.

For example, let’s call this singleton class Arduino. Thread A is running, and wants to send a command "*03", so it calls Arduino.Instance.SendCommand("*03"). Meanwhile, Thread B sends a command "*88", both of which get sent in near-realtime. Sometime later the Arduino‘s SerialPort.Read() thread picks up a reply for *88 and then a reply for *03 (i.e. in the opposite order they were sent). How do we allow both Thread A and Thread B to block correctly waiting on the specific reply to come in? We’re assuming we will use AutoResetEvent inside each thread, with an async callback to let us .Set it.

  • 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-05-24T06:50:20+00:00Added an answer on May 24, 2026 at 6:50 am

    If performance is what you’re after, and async at its finest level, I suggest looking into Completion Ports. This is what is ultimately underneath, hidden in the Windows Kernel, and it’s awesome. When I used them, I used C++, even found a Kernel bug because of it, but I was limited to the language.

    I’ve seen this article on CodeProject which might be worth exploring to see where you can take your idea further and/or use the code that’s there.

    The nature of Completion ports is to work on callbacks. That is, in general, you “put” a request in the queue, and when something lands there, the request is read and the callback specified is read. It is in fact, a queue, but like I said, at the lowest (manageable) level (before getting almost on metal).

    EDIT: I’ve written a sort of a FTP server/client testing utility with Completion ports, so the base process is the same – reading and writing of commands in a queuable fashion. Hope it helps.

    EDIT #2: Ok, here’s what I would do, based on your feedback and comments. I would have an “outgoing queue”, ConcurrentQueue<Message>. You can have a separate thread for sending messages by dequeueing each message. Note, if you want it a bit more “safe”, I suggest peeking at the message, sending it, then dequeuing it. Anyway, the Message class can be internal, and look something like this:

    private class Message {
        public string Command { get; set; }
        ... additonal properties, like timeouts, etc. ...
    }
    

    In the singleton class (I’ll call it CommunicationService), I’d also have a ConcurrentBag<Action<Response>>. This is now where the fun starts :o). When a separate concern wants to do something, it registeres itself, for example, if you have a TemepratureMeter I would have it do something like this:

    public class TemperatureMeter {
       private AutoResetEvent _signal = new AutoResetEvent(false);
    
       public TemperatureMeter {
         CommunicationService.AddHandler(HandlePotentialTemperatureResponse);
       }
    
       public bool HandlePotentialTemperatureResponse(Response response) {
         // if response is what I'm looking for
         _signal.Set();
    
         // store the result in a queue or something =)
       }
    
       public decimal ReadTemperature() {
         CommunicationService.SendCommand(Commands.ReadTemperature);
         _signal.WaitOne(Commands.ReadTemperature.TimeOut); // or smth like this
    
         return /* dequeued value from the handle potential temperature response */;
       }
    
    }
    

    And now, in your CommunicationService, when you receive a response, you simply to a

    foreach(var action in this._callbacks) {
       action(rcvResponse);
    }
    

    Voila, separation of concerns. Does it answer your question any better?

    Another possible tactic would be, to couple message and callback, but having the Callback be a Func<Response, bool> and the dispatcher thread checks if the result returned from the Func is true, then this callback is disposed.

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

Sidebar

Related Questions

Im currently developing a Silverlight application that connects to an old webservice. Our old
We currently have an application (Windows service) that connects to another of our applications
We are currently migrating our VB6 application to Net. The VB6 code uses some
Our application currently stores bunch of configuraton & application specific data to files on
Our application is currently made up of two big entities: C# ASPX files, and
We are currently calling web services on our application server through our web server
I'm currently going through the process of running FxCop through our application that has
An application our company is working on currently displays many rectangle shapes with gradients
I am currently introducing a mobile friendly version of our web-application. In a first
We're currently using the following for creating US dollar values in our web application:

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.