I receive a message from serial port that is passed to a variable once every few seconds. The message is generated by a microprocessor and it is basically a simple string containing "1/r" which represent one complete turn of a motor (every time a microswitch is pressed, the micro prints "1/r) on the serial port). In order to show in a text box the revolution per minute of such motor, based on the time interval between the update of the variable, I would like to implement a timespan method. I should therefore obtain the time elapsed between the start and the end of a turn and return the result for further actions to the original method.
This is what I have done so far but does not work:
public void CalcolaRPM (string giriRicevuti, out double RPM)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
{
if (giriRicevuti == "1/r")
{
stopWatch.Stop();
}
}
double elapsedT = stopWatch.ElapsedMilliseconds;
stopWatch.Restart();
RPM = elapsedT;
giriRicevuti = "";
}
}
The result I get is 0.0, how can I get the time in milliseconds between updates of the variable "giriRicevuti"?