Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Dec 08, 2019 5:02 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: How to end infinite loop
PostPosted: Sun May 01, 2011 3:50 am 
Offline

Joined: Tue Feb 22, 2011 1:14 pm
Posts: 22
I've been working for months on an application to collect data for a sequence of rapidly changing events on serial ports in near real time. Too do this, I created a pushbuttonStart. It has an infinite loop that collects and saves the data as a line in a file each time it goes through the loop. I put a stop flag in the loop but can't change it while the loop is running. My problem is I can't stop the loop without quitting the application. As soon as I start the loop, I no longer can do anything else except force quit. I need some type of interrupt to exit the loop or change the stop flag state. How? :?: I tried another push button, but it doesn't respond while the loop is running. I can make the loop a non-infinite loop by using a counter but this doesn't satisfy the need because in the real happening I don't know in advance how many data lines I need.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Sun May 01, 2011 3:58 am 
Offline
User avatar

Joined: Wed Nov 15, 2006 3:50 pm
Posts: 2353
Location: England
Use a timer.
Collect data every time the timer fires.
That will leave the window responsive to button clicks.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Sun May 01, 2011 7:08 am 
Offline
User avatar

Joined: Sun Feb 19, 2006 3:10 pm
Posts: 1583
Location: Hamburg, Germany
Or handle the incoming data in the serial port’s DataAvailable event (see “Reading Data” in http://docs.realsoftware.com/index.php/UsersGuide:Chapter_13:Communicating_With_Serial_Devices). No loop necessary, much less an infinite one.

_________________
Michael J. Hußmann
http://digicam-experts.de


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Sun May 01, 2011 2:24 pm 
Offline

Joined: Tue Feb 22, 2011 1:14 pm
Posts: 22
DATA Available:

Tried and tried using the data available event. The problem is that the read variable overwrites the previous read variable so I can't compare the previous to the new. By going outside the dataavailable event, I can call the write command to have the serial device send a new value. Then I can assign a new value and an old value, compare the two and process according to the result. I couldn't figure out a way to do this with the data available event. Also, data available event only fires after I send a command through the serial port to my device to tell it to send me data. Every time the dataavailable event fires it basically restarts the event and overwrites all the previous event.This is ok for applications where one doesn't have to compare successive readings for a change in value.

I have a loop within a loop. I read a new value, then read a second value, if the second value differs from the first value by a preset value, I write the first value to file , set the second value as the first value and go through the loop looking for a new second value that differs from the first value. So, I am constantly recording the changed value, but not recording values that haven't changed.
TIMER:
I tried a timer, but I can't get the timer to fire quickly enough. I am reading new data faster than every 20 ms, as fast as 2 microseconds.

In the old ms basic this was a simple problem to solve.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Sun May 01, 2011 3:04 pm 
Offline
User avatar

Joined: Sun Feb 19, 2006 3:10 pm
Posts: 1583
Location: Hamburg, Germany
Capngene wrote:
Tried and tried using the data available event. The problem is that the read variable overwrites the previous read variable so I can't compare the previous to the new.

You could store any number of chunks of data received this way – in a property of the window for example. I don’t see the difficulty in comparing one chunk of data with the previous one. You are not limited to local variables.

Capngene wrote:
Also, data available event only fires after I send a command through the serial port to my device to tell it to send me data.

You didn’t mention that before but still: where’s the problem? You can issue a command through the serial port, handle incoming data in the DataAvailable event, then request the next chunk of data. Your user interface stays responsive that way and your app doesn’t burn more CPU cycles than strictly necessary.

Polling for data in a loop is bad, for several reasons. One is that it locks the user interface, as you have found out. Anything (including Jeff’s suggestion to use a Timer) would be better than using a loop.

_________________
Michael J. Hußmann
http://digicam-experts.de


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Tue May 10, 2011 3:16 am 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
just use " exit "

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Tue May 10, 2011 8:19 am 
Offline

Joined: Tue Feb 22, 2011 1:14 pm
Posts: 22
See my post in Getting Started: Post subject: Can't Reliably read serial data
Can't use exit because the interface is locked.


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Tue May 10, 2011 8:26 am 
Offline
User avatar

Joined: Wed Nov 15, 2006 3:50 pm
Posts: 2353
Location: England
Quote:
The problem is that the read variable overwrites the previous read variable so I can't compare the previous to the new.


Why read it into the same buffer?


NOW = ""
LAST = ""

//every read event
LAST= NOW
NOW = new data
if NOW = LAST then
//same
end if
//end of event


Top
 Profile  
Reply with quote  
 Post subject: Re: How to end infinite loop
PostPosted: Tue May 10, 2011 8:36 am 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
I think you're using the Serial port control incorrectly. It has an internal buffer. When data is available the DataAvailable property is set. When you do a ReadAll it reads the data currently in the buffer (not necessarily all of the data).

You'll need to know if there is an 'end of data' character and use your own buffer (a string can do this) and if your data doesn't have the end of data character(s) you just add that to your internal buffer string and add the next data to it.

It also doesn't sound like you're using the Serial controls with Events. Doing that would make life a lot easier IMO.

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group