So, I have a readSensor function that you guessed it .. reads the sensor.
But the sensors usually take about 100 ms to respond. So in the readSensor function, I just start the timer.
At a point in time, I read serialport and got my answer.
However, this means that my answer is in onTimedEvent when I want it to be in the readSensor function.
Mostly from the main form, I want to be able to do this.
value = readSensor ()
when for a minute all I can do is readSensor (), and then I see that the response is returned, showing it in the message box after the timedEvent has been triggered.
here is my code. (I missed a lot of serialport settings and stuff, but hope you can see the problem I'm in)
I do not want to just wait in a function for 100 ms, although polling a timer, as this will make my program slow.
I want to somehow return the response to the readSensor function, and then return to the form.
using System; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; using System.Timers; namespace readSensor { public partial class readSens : UserControl { public readSens() { InitializeComponent(); } private System.Timers.Timer rTimer; SerialPort sp = new SerialPort(); private void setupTimer() {
In basic form, I basically just say
setupTimer(); readSensor();
at the touch of a button.