Can I start different background tasks depending on if-statement?
Hello.
I have a system which can operate in two different systems: automatic and manual mode.
My goal is to start different background tasks depending on the chosen mode. I can chose the mode with radio buttons and set a global variable (SamplingMethodG) to 1 for automatic or 2 for manual mode. And then with a button I want to start my system. In the function for this button I use an if-statement to check which mode is selected and start the appertaining mode.
My problem is the the background task for the automatic is working. All functions belonging to that task are executed constantly. But if I choose the manual mode it is only executed once.
Do you have an idea why?
Here is the relevant code:
String ctrlName
Variable defaultRM,instr,status
String resourceName = "ASRL8::INSTR"
//opens resource manager
viOpenDefaultRM(defaultRM)
status = viOpenDefaultRM(defaultRM)
if (status < 0)
ReportVISAError("viOpenDefaultRM", instr, status)
endif
//opens session with instrument
viOpen(defaultRM, resourceName, 0, 0, instr)
status = viOpen(defaultRM, resourceName, 0, 0, instr)
if (status < 0)
viClose(defaultRM)
ReportVISAError("viOpen", instr, status)
print "Could not open Relay"
//return 0
endif
//setting attributes for relaycard
status =visetattribute(instr,VI_ATTR_TMO_VALUE,50) //Timeout
status =visetattribute(instr,VI_ATTR_ASRL_BAUD,9600) //Baudrate
status =visetattribute(instr,VI_ATTR_ASRL_DATA_BITS,8) //Data Bits
status =visetattribute(instr,VI_ATTR_ASRL_PARITY,VI_ASRL_PAR_NONE) //Parity
status =visetattribute(instr,VI_ATTR_ASRL_STOP_BITS,VI_ASRL_STOP_ONE) //Stop Bit
status =visetattribute(instr,VI_ATTR_ASRL_FLOW_CNTRL,VI_ASRL_FLOW_NONE) //Flow control for Data Transfer
variable/G root:SamplerGlobals:Relaycard = instr
Printf "Relay=%d\r", instr
//Check which sampling method and start corresponding background task to control relays
NVAR SamplingMethodG=root:SamplerGlobals:SamplingMethodG
if (SamplingMethodG==1) //automatic sampling
CtrlNamedBackground automatic,period=60,proc=AutomaticSampling,start
elseif (SamplingMethodG==2) //manual sampling
ctrlNamedBackground manual,period=60,proc=ManualSampling,start
endif
End
//Running through all samplers and initialize functions checkcondition and checkformula
function ManualSampling(s)
STRUCT WMBackgroundStruct &s
variable sampler
// to check if function is executed once or more
print "bla"
End
//Activates functions to check conditions, formula and if the system should sample and time couting
function AutomaticSampling(s)
STRUCT WMBackgroundStruct &s
//to check if function is executed once or more
print "blabla"
variable sampler,condition
NVAR ParticleSizeG=root:SamplerGlobals:ParticleSizeG
if (ParticleSizeG==1) //PM1: sampler 1-4
for(sampler=1;sampler<5;sampler+=1)
for(condition=1;condition<5;condition+=1)
Checkcondition(sampler,condition)
Checkformula(sampler,condition)
CheckSampling(sampler)
Timecount(sampler)
endfor
endfor
elseif (ParticleSizeG==2) //PM1+PM10: sampler 1-2
for(sampler=1;sampler<3;sampler+=1)
for(condition=1;condition<5;condition+=1)
checkcondition(sampler,condition)
Checkformula(sampler,condition)
CheckSampling(sampler)
Timecount(sampler)
endfor
endfor
endif
return 0
End
Thank you in advance.
I also tried to do a simple version of what I have so far, but now none of the background tasks is working constantly.
Window Panel0() : Panel
PauseUpdate; Silent 1 // building window...
NewPanel /W=(150,50,353,212)
Variable/G gRadioVal= 1
CheckBox check0,pos={52,25},size={78,15},title="Radio 1",value= 1,mode=1,proc=MyCheckProc
CheckBox check1,pos={52,45},size={78,15},title="Radio 2",value= 0,mode=1,proc=MyCheckProc
Button RelayStart_panel,pos={52,85},size={75,40},proc=RelayInitialize,title="Communication Start"
EndMacro
Function MyCheckProc(name,value)
String name
Variable value
NVAR gRadioVal= root:gRadioVal
strswitch (name)
case "check0":
gRadioVal= 1
break
case "check1":
gRadioVal= 2
break
endswitch
CheckBox check0,value= gRadioVal==1
CheckBox check1,value= gRadioVal==2
End
Function RelayInitialize(ctrlName) : ButtonControl
String ctrlName
NVAR gRadioVal= root:gRadioVal
if (gRadioVal==1) //automatic sampling
CtrlNamedBackground automatic,period=60,proc=AutomaticSampling,start
elseif (gRadioVal==2) //manual sampling
ctrlNamedBackground manual,period=60,proc=ManualSampling,start
endif
End
function AutomaticSampling(s)
STRUCT WMBackgroundStruct &s
print "automatic"
End
function ManualSampling(s)
STRUCT WMBackgroundStruct &s
print "manual"
End
September 21, 2018 at 06:33 am - Permalink
In reply to I also tried to do a simple… by neovenecia
I think your background task procedures need return statements.
displayHelpTopic "Background Task Exit Code"
September 21, 2018 at 06:40 am - Permalink
In reply to I think your background task… by tony
Indeed - your task functions need:
return 0
See:
DisplayHelpTopic("Background Task Exit Code")
September 21, 2018 at 06:47 am - Permalink
In reply to I think your background task… by tony
Thank you very much
That was the line I overlooked
September 21, 2018 at 06:55 am - Permalink