I am working with an electrometer using RS-232(Serial) communication. It accepts one command at a time and sends back a response indicating if the command completed successfully or not.
For demonstration only consider the following method...
private void SetDevice()
{
sc.SetBias();
sc.SetRange();
sc.SetCollectionTime();
sc.SetID();
}
The method signature for sc.SetID()
is as follows.
public void SetID()
{
ComPort.Write("*IDN?");
}
The above gives you an idea of what the other individual command calls are like.
Since the DataReceived Event indicates it is raised on a secondary thread how should I proceed with waiting for a response between each command call? Presently after sc.SetBias()
is called sc.SetRange()
fires without regards to if the previous command executed successfully.
I can appreciate that by starting the DataReceivedEvent
in a new thread it prevents my UI from locking but in this case when it is critical that the response gets paired with the appropriate command call I'm not sure of the best available option and would appreciate some guidance in my research. Thus far I'm considering getting a handle to the current UI thread and using Thread.Sleep(sleepTime)
but without knowing exactly how reliable the response time is of the electrometer is this really best practice? And would this not need then to be called after every command sent?