Page 1 of 1

onidle question

Posted: Fri Aug 26, 2011 9:10 pm
by spectrum
Dear all,

i am using OnIdle event/callback on a wxFrame to read bytes from the serial port, to avoid further threads. My actual approach is:

Code: Select all

 void MyFrame::OnIdle (wxIdleEvent &e)
{
      int i;
      if ((i=serial->Read())>0)
     { 
         --- process data and eventually update some status messages in the frame ---
     }
     wxMilliSleep(20);
     e.RequestMore();
}
This works, but i am not sure it is the correct/portable way to do it.
Also, i am looking for faster data detection, with a read interval even < 20msec.

Any help is appreciated,
thanks
regards

Re: onidle question

Posted: Sat Aug 27, 2011 12:13 pm
by JimFairway
Hi,

Have you considered using a wxTimer instead?
See http://docs.wxwidgets.org/stable/wx_wxtimer.html
This example has a timer in it: http://wiki.wxwidgets.org/Making_a_rend ... ng_a_timer

Hope that helps,

Jim

Re: onidle question

Posted: Sat Aug 27, 2011 3:09 pm
by dlchnr
As Jim wrote, wxTimer is the solution - here the code lines, I solved the same problem:

Code: Select all

class cMainFrame : public wxFrame {
  // :
  wxTimer tim;
  bool timlock;
  // :
  // privat member functions
  // privat copy ctor and assignment operator, if these aren't defined
  cMainFrame (const cMainFrame& obj);
  cMainFrame& operator=(const cMainFrame& obj);
  // any class providing RTTI must use this macro
  DECLARE_CLASS(cMainFrame)
  // any class wishing to process wxWidgets events must use this macro
  DECLARE_EVENT_TABLE()
public:
  // ctor
  cMainFrame(cPrgData& PrgDat);
  // dtor
  virtual ~cMainFrame();
  // basic member functions
  // :          
  // member functions
  // :
  bool PollFunction();
  // :
  void FollowAuxColouring();
  // event handlers (these functions should _not_ be virtual)
  // :
  void OnTimer(wxTimerEvent& event);
  // :
};


cMainFrame::cMainFrame(cPrgData& PrgDat) :  
            wxFrame (NULL, wxID_ANY, _T(""), wxDefaultPosition, wxSize(888,748)),
            prgdata(PrgDat), startUp(PrgDat.AutoExec(), *PrgDat.pC()), 
            pBook(), pPageTRC(), pPageOUT(), pPagePRG(), 
            tim(this, wxID_ANY), timlock(false), movOnRdy(false), 
            autoMode(false), exec_1st(false), busycnt(0), traceALL(0) {
              
  // set the frame icon
  SetIcon(wxICON(pCAN_icon));

  // create the file menu
  // :

  // :

  // :
  tim.Start(20);
  Connect(tim.GetId(), wxEVT_TIMER, wxTimerEventHandler(cMainFrame::OnTimer), NULL, this); 
}


void cMainFrame::OnTimer(wxTimerEvent& event) {

  if (!timlock) {
    timlock = true;
    PollFunction();
    timlock = false;
  }
}