Page 1 of 1

How to stop en endless loop ?

Posted: Tue Jun 06, 2006 6:52 pm
by Raymond Fischbach
Hello,

Let me expose the situation of what I need to do:

By pressing on a button, the user will ask the program to start to read the data from a GPS device.

The program then enters into an endless loop that will do the following:
- read the GPS data
- save the data into a file
- wait a few seconds
- loop

To stop the loop, the user will push another button.

How can this be done?

Thanks for any hint.

Best regards, Raymond

Re: How to stop en endless loop ?

Posted: Tue Jun 06, 2006 8:16 pm
by Enrico Maria Giordano
Just put something like

IF lStop; EXIT; ENDIF

inside the loop and

ACTION lStop := .T.

in the stop button action.

EMG

Posted: Wed Jun 07, 2006 12:10 pm
by Jon Munro
Raymond,
My suggestion is to use a timer function to control the polling of the GPS and read data available. Use a suitable control to enable or disable the timer so as to stop and start the read cycle and a variable to set the timer period.
hth

Posted: Wed Jun 07, 2006 4:37 pm
by Raymond Fischbach
Enrico,

I tried your suggestion but I cannot make it work.
As soon as the loop is running, the "Stop" button is disabled.


Jon,

Thank you for the idea.
My problem is that I am NOT
a clipper programmer
a fivewin user
Therefore my knowledge is very poor :(

How can I control a timer? How can I start it? How can I stop it?
Could you please share some code to get me started?

Many thanks in advance.

Raymond

Posted: Thu Jun 08, 2006 1:12 am
by Jon Munro
Raymond,
Thanks to Enrico for his suggestion! - see Enrico's post 'Serial Port' of 16 January for some nice code samples. Use 'activate' and 'deactivate' methods to start and stop the timer and hence the serial read function. Let me know if you need a fully working sample...
hth

Posted: Thu Jun 08, 2006 4:33 am
by Jon Munro
Raymond,
The sample program 'BlueTime' illustrates the timer function. This example just reads 20 chs each time - you'll need to work out when your GPS data message is complete. You'll need the Mobile 2005 emulator for comms to work reliably...

// BlueTime

#include "FWCE.ch"

#define GENERIC_READ 0x80000000
#define GENERIC_WRITE 0x40000000
#define GENERIC_REWRITE 0xC0000000
#define OPEN_EXISTING 3
#define FILE_ATTRIBUTE_NORMAL 0x00000080

function Main()

local oWnd, oTimer, oChk
local hPort
local nPollTime := 1000
local lOnOff := .T.

hPort := CreateFile( "COM1:",GENERIC_REWRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL )

if hPort == -1
MSGALERT("Cannot open port")
return -1
endif


DEFINE WINDOW oWnd TITLE "BlueTime"

@ 2, 2 BUTTON "GPS On/Off" SIZE 80, 30 ACTION (lOnOff := OnOff( hPort, oTimer, lOnOff ), oChk:refresh)

@ 10, 2 CHECKBOX oChk VAR lOnOff PROMPT "On/Off" OF oWnd SIZE 80, 20

if hPort != -1
DEFINE TIMER oTimer OF oWnd;
INTERVAL nPollTime;
ACTION ReadGPS(hPort, oTimer)
endif

ACTIVATE WINDOW oWnd

CloseHandle( hPort )

return nil

function ReadGPS( hPort, oTimer )

local nChr := 1
local cText := ""
local n := 1

oTimer:DeActivate() // stops timer - waits for data

do while nChr > 0 .and. n < 21
nChr := ReadByte( hPort )
cText := cText + Chr( nChr )
n++
end do

msginfo( cText )

oTimer:Activate() // restarts timer

return nil

function OnOff( hPort, oTimer, lOnOff )
if hPort != -1
if lOnOff
oTimer:DeActivate() // stops timer
else
oTimer:Activate() // starts timer
endif
endif
return !(lOnOff)

hth
regards

Posted: Thu Jun 08, 2006 8:14 am
by Raymond Fischbach
Hi Jon,

Thank you for these info.

I will git it a try and will post teh results.

Best regards,
Raymond