How to stop en endless loop ?

How to stop en endless loop ?

Postby Raymond Fischbach » Tue Jun 06, 2006 6:52 pm

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
Raymond Fischbach
www.mouches.org
User avatar
Raymond Fischbach
 
Posts: 48
Joined: Sun Oct 30, 2005 9:29 am
Location: Belgium

Re: How to stop en endless loop ?

Postby Enrico Maria Giordano » Tue Jun 06, 2006 8:16 pm

Just put something like

IF lStop; EXIT; ENDIF

inside the loop and

ACTION lStop := .T.

in the stop button action.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8719
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Jon Munro » Wed Jun 07, 2006 12:10 pm

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
User avatar
Jon Munro
 
Posts: 42
Joined: Sun Oct 09, 2005 11:47 am
Location: Brisbane Australia

Postby Raymond Fischbach » Wed Jun 07, 2006 4:37 pm

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
Raymond Fischbach
www.mouches.org
User avatar
Raymond Fischbach
 
Posts: 48
Joined: Sun Oct 30, 2005 9:29 am
Location: Belgium

Postby Jon Munro » Thu Jun 08, 2006 1:12 am

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
User avatar
Jon Munro
 
Posts: 42
Joined: Sun Oct 09, 2005 11:47 am
Location: Brisbane Australia

Postby Jon Munro » Thu Jun 08, 2006 4:33 am

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
User avatar
Jon Munro
 
Posts: 42
Joined: Sun Oct 09, 2005 11:47 am
Location: Brisbane Australia

Postby Raymond Fischbach » Thu Jun 08, 2006 8:14 am

Hi Jon,

Thank you for these info.

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

Best regards,
Raymond
Raymond Fischbach
www.mouches.org
User avatar
Raymond Fischbach
 
Posts: 48
Joined: Sun Oct 30, 2005 9:29 am
Location: Belgium


Return to FiveWin for Pocket PC

Who is online

Users browsing this forum: No registered users and 10 guests

cron