Dear All,
Data comes to my serial port every 6 seconds.
My application is capturing the data @ 1 data/6 seconds initially, properly and converting it into graphs.
But as the time proceeds the capture rate becomes @ 2 data/12 seconds and again after some time @3 data/18 seconds.
At the end of 10 hours its around @ 50-60 data/6 minutes ... and keeps increasing.
Please note that no data is lost even though the application comes to a stand-still for 6 minutes.
I have searched various forums for many days without any success.
I am attaching the code and properties settings.
private void Import_Data_Button_Click(object sender, EventArgs e)
{
if (LiveMenu.Checked)
{
LiveMenu.Checked = false;
Live = false;
SPTimer.Enabled = false;
try
{
if (SP.IsOpen) { SP.Close(); }
}
catch { }
}
else
{
{
LiveMenu.Checked = true;
Live = true;
SPTimer.Enabled = true;
try
{
if (!SP.IsOpen) { SP.Open(); }
}
catch { }
DrawGraphs();
}
}
private void SP_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
Serial_Data += SP.ReadLine();
}
catch { }
}
private void SPTimer_Tick(object sender, EventArgs e)
{
SPTimer.Enabled = false;
if (lblLive.Visible == false) { lblLive.Visible = true; }
else { lblLive.Visible = false; }
if (Serial_Data.Contains("\r"))
{
WriteToDatabase(Serial_Data);
Serial_Data = "";
}
SPTimer.Enabled = true;
}
Any suggestions will be of great help to me.
Thanks!!!
Rupali
Loading
sanas vPosted Apr 9, 2011, 8:27 AM
I think the readexisting will work fine for me!!!
I have replaced readline with readexisting, and tested it for 5 hours, and the data is coming at around 6-17 seconds continously. But there is some data loss after first 2 hours.
Now i will try your suggestion :
EDIT: forgot to mention... if you use ReadExisting, you'll need to modify the Tick event to make sure that the WritetoDatabase call doesn't try to save data beyond the \r. Also, when you reset the buffer, setting it to "" could potentially cause loss of data. Something like this would avoid that:
string[] dataSplit = Serial_Data.Split(@"\r");
WriteToDatabase(dataSplit[0]);
Serial_Data = dataSplit.Length > 1 ? dataSplit[1] : "";
I will revert after trying this.
Thanks a lot!
Sam HobbsPosted Apr 8, 2011, 5:40 AM
sanas vPosted Apr 8, 2011, 4:41 AM
I have tried using ErrorReceived Event handler. But it is not called at all!
I have also tried varying time intervals and write buffer size.
Still the same problem exists.
Now i will try "readexisting" as suggested by you and revert.
Dear Sam,
Thanks for your reply.
I will try flow control and try.
I think there is no need to signal the other device to send new data, as it anyways keeps sending data at every 6 seconds.
Also, note that no data is lost at all, whatsoever, only the problem is that the time intervals keeps increasing and the app "hangs" during that time intervals.
I will get back to you guys, thanks!
Guest UserPosted Apr 7, 2011, 9:04 PM
Why not just process the data in the DataReceived event and remove the timer altogether?
Sam HobbsPosted Apr 7, 2011, 8:55 PM
Do you understand about Flow control? Note that there are two types of flow control; hardware and software. You need to determine what the other device is using. Then your program needs to use the flow control to determine when there is data.
I am not totally sure about this. Perhaps your program needs to use flow control to signal the other device that it is time for new data.
If you do use the timer, then what I would do is that in every tick, I would cancel the current timer and then calculate how much time is needed until the next batch of data is to be received and then start the timer again with the new value. So in other words, for each 6 seconds, the timing is re-adjusted to eliminate any previous delay. Does that make sense?
Guest UserPosted Apr 7, 2011, 11:26 AM
Guest UserPosted Apr 7, 2011, 11:22 AM
You're calling SP.ReadLine() to get the data from the serial port, but in the Tick event you're checking for \r in the data. Assuming your data is sent in lines ending in \r\n, that if statement seems redundant to me. ReadLine returns all characters up to (but not including) the newline character (\n), which means the if statement where you're checking for \r will always evaluate as true. As a test, I would change the ReadLine to ReadExisting (which doesn't wait for newline, it just reads whatever is in the buffer), and see if that makes a difference.
EDIT: forgot to mention... if you use ReadExisting, you'll need to modify the Tick event to make sure that the WritetoDatabase call doesn't try to save data beyond the \r. Also, when you reset the buffer, setting it to "" could potentially cause loss of data. Something like this would avoid that:
string[] dataSplit = Serial_Data.Split(@"\r");
WriteToDatabase(dataSplit[0]);
Serial_Data = dataSplit.Length > 1 ? dataSplit[1] : "";
sanas vPosted Apr 7, 2011, 10:56 AM
The data is coming in a proper manner. When i use hyperterminal the data is received @1 data/6 seconds without any error.
Guest UserPosted Apr 7, 2011, 10:30 AM
sanas vPosted Apr 7, 2011, 10:13 AM
THE APPLICATION SHOULD CAPTURE DATA EVERY SIX SECONDS(WHICH IT DOES INITIALLY FOR FEW MINUTES) BUT AFTER THAT THE TIME INTERVAL KEEPS ON INCREASING.
WHY IS THE TIME INTERVAL INCREASING AND WHAT CAN BE DONE TO CORRECT IT.
MY SECOND PROBLEM IS :
THIS BUG IS COMING IN BETWEEN ME AND MY FIRST MILLION :)
IF YOU SOLVE THIS PROBLEM YOU WILL GET A MILLION BLESSINGS FROM ME :)
FroglegPosted Apr 7, 2011, 9:58 AM
"can you post a question, because I can't see one anywhere in your post"