I have created a winforms UI that sends and receives strings over a serial port to and from an MCU which has a known and simple command interface. I'm wondering how I can create a chain or queue of commands, or if there is already some built in way of doing this?
I was thinking the chain would consist of a header which points to the location of the next command to be executed. Each command would be associated with a pointer to the next command, eventually leading to a pointer which refers to NULL. Whenever a command is executed, the next command string would be updated to be in the location where the header points, and each subsequent command would move up the chain.
If anybody could point me in the right direction or offer some advice, that would be great.
If you only need a queue data structure, you can use the built-in Queue<T>
type from System.Collections.Generic
namespace. It has all the basic queue functionality. See the documentation here.
Sample code (not tested, ment only for quick example):
using System.Collections.Generic;
var queue = new Queue<string>();
...
queue.Enqueue("CMD1");
queue.Enqueue("CMD2");
...
var nextCommand = queue.Peek(); // will NOT remove the peek item
...
var nextCommandRemoved = queue.Dequeue();
...