Port reading

S

Shark

Hi,

I need a help. My application reads data from COM port, this data is then
parsed and displyed on:
1. two plotters
2. text box.

I'm using Invoke method to update UI when new data is received (through
delegate).

void UpdateUI(IAbstract data)
{
if(data is Result)
// update texbox
else if (data is PlotterResult)
// update two plotter ctrls
}

Every 20 miliseconds I'm receiving data for plotter and every 1 sec data for
textbox.
Unfortunately it doesn't work fine. Data in textbox is not displayed every 1
sec (instead 5,6 sec). I noticed that it's caused by
repainting plotter control because it consume some time.
I tried BeginInvoke instead Invoke but things went even worse.

How to deal with such problem? Maybe I have to create each UI component in
separate thread? (how?).

Thanks for any advise.

Shark
 
W

Willy Denoyette [MVP]

Shark said:
Hi,

I need a help. My application reads data from COM port, this data is then
parsed and displyed on:
1. two plotters
2. text box.

I'm using Invoke method to update UI when new data is received (through
delegate).

void UpdateUI(IAbstract data)
{
if(data is Result)
// update texbox
else if (data is PlotterResult)
// update two plotter ctrls
}

Every 20 miliseconds I'm receiving data for plotter and every 1 sec data
for textbox.
Unfortunately it doesn't work fine. Data in textbox is not displayed every
1 sec (instead 5,6 sec). I noticed that it's caused by
repainting plotter control because it consume some time.
I tried BeginInvoke instead Invoke but things went even worse.

How to deal with such problem? Maybe I have to create each UI component in
separate thread? (how?).

Thanks for any advise.

Shark

We need more code, pseudo code isn't of any help here.
We also need to know on what thread you are executing UpdateU and what other
threads you have running.
+5 seconds looks like UpdateUI is running on a thread that gets starved, in
other words there must be a thread which pegs the CPU (probably performing a
tight loop).

Willy.
 
S

Shark

Uzytkownik "Willy Denoyette said:
We need more code, pseudo code isn't of any help here.
We also need to know on what thread you are executing UpdateU and what
other threads you have running.
+5 seconds looks like UpdateUI is running on a thread that gets starved,
in other words there must be a thread which pegs the CPU (probably
performing a tight loop).

Willy.
OK, so I have one thread which is listing on COM. There is data on COM every
20 miliseconds for charts and every 1 second for text box (in different
format)
I Invoke (or BeginInvoke) delegate which points method which belongs to main
window (MainForm): This method looks like this:

public void UpdateData(AbstractObj obj){

if(obj is Business.Models.Result)

{

Business.Models.Result r = (Business.Models.Result)obj;

this.UpdateSensors(r);

}

else if(obj is Business.Models.ChartResult)

{

Business.Models.ChartResult r = (Business.Models.ChartResult)obj;

this.UpdatePlotter(r);

}

}

UpdateSensors (every 1sec) and UpdatePlotter (every 20 ms) execute in main
thread. First one simply put text in textbox control whereas second one do
more job. It draws new point on plotter and call Refresh() and Update() to
repaint itself. I now that problem is with repainting charts.

So I have to threads, one for COM and second with GUI.



Shark
 
W

Willy Denoyette [MVP]

Shark said:
OK, so I have one thread which is listing on COM. There is data on COM
every 20 miliseconds for charts and every 1 second for text box (in
different format)
I Invoke (or BeginInvoke) delegate which points method which belongs to
main window (MainForm): This method looks like this:

public void UpdateData(AbstractObj obj){

if(obj is Business.Models.Result)

{

Business.Models.Result r = (Business.Models.Result)obj;

this.UpdateSensors(r);

}

else if(obj is Business.Models.ChartResult)

{

Business.Models.ChartResult r = (Business.Models.ChartResult)obj;

this.UpdatePlotter(r);

}

}

UpdateSensors (every 1sec) and UpdatePlotter (every 20 ms) execute in main
thread. First one simply put text in textbox control whereas second one do
more job. It draws new point on plotter and call Refresh() and Update() to
repaint itself. I now that problem is with repainting charts.

So I have to threads, one for COM and second with GUI.



Shark



A little better but still not enough, what is the COM thread doing, it looks
like he's in a tight loop reading COM port data and posting results back to
the UI thread. What scares me is the so called every 20 msecs. How did you
(think to) accomplish this?
We really need more code about COM reading stuff, also tell us what's this
threads priority level is, did you set it explicitly or not?
All I can say for now is that the COM reading thread is pegging the (one and
only I guess) CPU , such that the UI thread has no chance to run his quantum
in a timely fashion.

Willy.
 
S

Shark

Uzytkownik "Willy Denoyette said:
A little better but still not enough, what is the COM thread doing, it
looks like he's in a tight loop reading COM port data and posting results
back to the UI thread. What scares me is the so called every 20 msecs. How
did you (think to) accomplish this?
We really need more code about COM reading stuff, also tell us what's this
threads priority level is, did you set it explicitly or not?
All I can say for now is that the COM reading thread is pegging the (one
and only I guess) CPU , such that the UI thread has no chance to run his
quantum in a timely fashion.

Willy.

COM thread looks like this:
public void read(object recObj)

{

string str = null;

while (isRunning)

{

try

{

str = serialPort.ReadLine();

IReceiver rec = recObj as IReceiver;

if (rec != null)

{

rec.Receive(str);

}

}

catch (TimeoutException)

{

}

}

}

and implementation of IReceiver.Receive method is following:
public void Receive(string data)

{

if (parser != null)

{

AbstractObj obj = parser.Parse(data, testPilot);

if (obj != null)

{

Result res = obj as Result;

if (res != null)

{

res.Pressure = PressureReader.Pressure;

}

foreach (IDataHandler h in handlers)

{

h.HandleData(obj);

}

}

}

}

Parse method simply extracts data from received string and HandlaData Invoke
delegate to update UI.
I do not set thread priority (shall I?)

I wrotte the program witch write string to COM every 20 ms and I'm using the
program which links two ports as they were connected using null modem cable.

Thanks

Shark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top