0

Is there a control in windows form that would allow me to pause the ongoing serial data receiving process, because I need to check and verify the data that is being plotted on the graph. Once checked, I need to resume the process. It would be like a start.. pause..resume.. pause.. process.

Would be great if anyone could suggest me the ideal procedure for the above. Is background worker the only way to achieve this functionality?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Porcupine
  • 13
  • 4
  • What kind of device/system you are getting data from? I am used to do a lot of plotting data from serialPort but i always control my device: I send command to serialPort then receive data then plot, if i need to pause i stop sending commands – chouaib Mar 28 '14 at 05:39
  • The data is coming from the sensor which sends data only when a specific command is sent to it. As i click on Start button on the windows form, the data is being plotted on the graph. However, my requirement is I put a Pause button, to stop the program temporarily and see what's cooking in the graph, and then click on start button to resume. How would i achieve this? – Porcupine Apr 01 '14 at 15:00

2 Answers2

0

Depending on what protocol you use you might be able to instruct the sender not to send anything while you are paused, but I think the most straight-forward approach will be to buffer the incoming data on a queue or a simple array, or something, and then just not update the screen with new data while the user is in the paused state.

0

My way to do such task:

actually you need to use thread for only one reason, which is to organize time EX:

WRONG:

while(true){
GetDataFromSerialPort(); // you don't know how long it takes 10ms, 56ms, 456ms ...?
DrawData(); // this plots data at randomly spaced intervals
}

RIGHT

while(true){
Thread th1 = new Thread(new ThreadStart(GetDataFromSerialPort)); // thread to acquire
th1.IsBackground = true;                                         // new data
th1.Start();

wait(100);   // main thread waits few milliseconds 

Thread th2 = new Thread(new ThreadStart(DrawData));  // draw on zedGraph on other thread
th2.IsBackground = true;
th2.Start();
}

Now let's do your main quastion (pause/resume...)

you need to define a bool flag that decides your data acquisition/drawing loop:

bool isRunning = false; // initially it's stopped

public void startDrawing()
{
isRunning = true;

while(isRunning)
{
//thread to get data
//wait
//thread to draw it
//refer to the above "right" example
}

}

// Now let's set buttons work
private void button1_Click(object sender, EventArgs e)
{
if(button1.text == "START" || button1.text == "RESUME")
{
button1.text = "PAUSE";
startDrawing();
}
else
{
button.text = "RESUME";
isRunning = false;
}
}
chouaib
  • 2,763
  • 5
  • 20
  • 35