Note that there are some explanatory texts on larger screens.

plurals
  1. POProviding Asynchronous Serial Port Communication
    text
    copied!<p>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).</p> <p>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 <code>succeeded?</code> flag).</p> <p>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?</p> <p>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.</p> <p><strong>Edit</strong>: 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.</p> <p>For example, let's call this singleton class <code>Arduino</code>. <code>Thread A</code> is running, and wants to send a command <code>"*03"</code>, so it calls <code>Arduino.Instance.SendCommand("*03")</code>. Meanwhile, <code>Thread B</code> sends a command <code>"*88"</code>, both of which get sent in near-realtime. Sometime later the <code>Arduino</code>'s <code>SerialPort.Read()</code> thread picks up a reply for <code>*88</code> and then a reply for <code>*03</code> (i.e. in the opposite order they were sent). How do we allow both <code>Thread A</code> and <code>Thread B</code> to block correctly waiting on the specific reply to come in? We're assuming we will use <code>AutoResetEvent</code> inside each thread, with an async callback to let us <code>.Set</code> it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload