Hello Guest it is March 28, 2024, 08:05:58 AM

Author Topic: For Loops inside of If Statement Help Please  (Read 5149 times)

0 Members and 1 Guest are viewing this topic.

For Loops inside of If Statement Help Please
« on: February 12, 2010, 10:19:33 PM »
Can Someone tell me what is wrong with this piece of VB code It seems right but throws up Compile Error syntax error!

Code: [Select]
If IsOutputActive(OUTPUT1) And GetVar(1) = 300 And Not IsActive(OEMTRIG1) Then
For counter = 1 To 3
DeActivateSignal(OUTPUT1) 'turn off output1
Message ("Waiting For THC To Establish An Arc") 'display message
Sleep (3000)
ActivateSignal(OUTPUT1)
Sleep (3000) 'sleep time in milliseconds
Message ("")
If IsOutputActive(OUTPUT1) And IsActive(OEMTRIG1) Then
DoOEMButton(1000)
Message ("ARC ESTABLISHED")
Sleep (2000)
Message ("")
Exit For
If counter = 3 Then
MachMsgTypeOK = 0 'Define some constants for MachMsg dialog types
MachMsgReturnOK = 1 'Define some constants for MachMsg return codes 'Display an Abort/Retry/Ignore dialog
Ret = MachMsg("Plasma Failed to Start three times. Press OK to Abort program", _"FLAME OUT WARNING", MachMsgTypeOK)

If Ret = MachMsgReturnOK Then
DoOEMButton(1003)
End If
End If



End If
Next
End If

Thanks In Advance
Christian
Re: For Loops inside of If Statement Help Please
« Reply #1 on: February 13, 2010, 02:20:11 AM »
Looks to me like it is a Cypress VB type conversion issue.  I've found that the VB interpreter in mach can do this sometimes.
This can be avoided by not letting variables be created on the fly as variants.

Both versions of the code below don't give errors.
( you will have to take out the extra CRLFs inserted by the board width limitation when this was pasted)

I also threw in some notes as pointers to other things you might want to consider doing.

Dave


Option Explicit   ' it's a good habit to require explicit declarations of all variables

Dim counter As Integer
Dim Ret As Integer      ' only needed for first version, not for 2nd version

Const MachMsgTypeOK As Integer = 0    'Define some constants for MachMsg dialog types                     
Const MachMsgReturnOK As Integer = 1   'Define some constants for MachMsg return codes                                                                     

' Try not to use Mach's magic numbers.
' They will be changing in Mach v4, using a name will make conversion to V4 easier as
' the named values will only need to be changed in one place for V4 conversion
Const OEMStartButtonCode = 1000
Const OEMStopButtonCode = 1003

If IsOutputActive(OUTPUT1) And GetVar(1) = 300 And Not IsActive(OEMTRIG1) Then
   For counter = 1 To 3
      DeActivateSignal(OUTPUT1)               'turn off output1
      Message ("Waiting For THC To Establish An Arc")         'display message
      Sleep (3000)
      ActivateSignal(OUTPUT1)
      Sleep (3000)                     'sleep time in milliseconds
       Message ("")
      If IsOutputActive(OUTPUT1) And IsActive(OEMTRIG1) Then
         DoOEMButton(OEMStartButtonCode)
         Message ("ARC ESTABLISHED")
         Sleep (2000)
         Message ("")
         Exit For
         If counter = 3 Then
            Ret = MachMsg ("Plasma Failed to Start three times. Press OK to Abort program", "FLAME OUT WARNING", MachMsgTypeOK)
            If Ret = MachMsgReturnOK Then
               DoOEMButton(OEMStopButtonCode)
            End If   
         End If
      End If
   Next
End If



' Ret is not needed in the above code fragment, if not needed elsewhere in the code, you might consider this version:

If IsOutputActive(OUTPUT1) And GetVar(1) = 300 And Not IsActive(OEMTRIG1) Then
   For counter = 1 To 3
      DeActivateSignal(OUTPUT1)               'turn off output1
      Message ("Waiting For THC To Establish An Arc")         'display message
      Sleep (3000)
      ActivateSignal(OUTPUT1)
      Sleep (3000)                     'sleep time in milliseconds
       Message ("")
      If IsOutputActive(OUTPUT1) And IsActive(OEMTRIG1) Then
         DoOEMButton(OEMStartButtonCode)
         Message ("ARC ESTABLISHED")
         Sleep (2000)
         Message ("")
         Exit For
         If counter = 3 Then
            If MachMsg ("Plasma Failed to Start three times. Press OK to Abort program", "FLAME OUT WARNING", MachMsgTypeOK) = MachMsg Then
               DoOEMButton(OEMStopButtonCode)
            End If   
         End If
      End If
   Next
End If



Author of the MachStdMill Extensions for Mach3
www.CalypsoVentures.com
Re: For Loops inside of If Statement Help Please
« Reply #2 on: February 13, 2010, 05:05:37 AM »
Hi Dave

Thanks for the response. I will test it out a little later this evening. I have no prior VB knowledge so in the last few days I have learned a lot. When I wrote the above piece I was cross referencing between a VB.net book we had lying around the office and the other cypress doc and when it appeared right and then didn't function it nearly drove me bonkas!  ;D

Thanks again

Christian

Offline zealous

*
  •  489 489
  • HI!
    • View Profile
    • Artsoft Solutions
Re: For Loops inside of If Statement Help Please
« Reply #3 on: February 13, 2010, 11:10:04 AM »
Acualy your code is fine... you have a
Code: [Select]
_"FLAME OUT WARNING which means you are putting it on the next line ;)
Take out the
Code: [Select]
_ and it will be right.
Re: For Loops inside of If Statement Help Please
« Reply #4 on: February 13, 2010, 10:23:21 PM »
Hi Dave , Zealous

Thanks for the advice. I tried the loop but the program just got stuck in the loop and didn't end. However I have achieved the same result with a somewhat less elegant system of If statements and the incrementation of the Var.

Also I removed the Underscore from the message box code and it fixed it SWEET!  Just A side note I had copied and pasted the code from the Mach3 V3.x Programmer Reference Draft v0.11a.pdf
And it had the underscore it the example so maybe it needs an update Just A thought.


Anyway Thanks very much for the help.

Cheers

Christian

P.S. Dave what did you mean by
Quote
( you will have to take out the extra CRLFs inserted by the board width limitation when this was pasted)
Also the option explicit you added at the top of the code caused the same syntax error I removed and it was fine.

Re: For Loops inside of If Statement Help Please
« Reply #5 on: February 13, 2010, 11:34:33 PM »
Before I posted, the preview view of my post was showing that some lines were being split in two as being to long.
I thought the forum would split the lines - which would have caused a compile error if you cut and pasted and got extra line breaks.
But apparently the preview was pulling my leg - it didn't split them after all.

Re Option Explicit - I consider that a good idea for all VB code.
It makes the compiler not declare things on the fly to match what you typed (or mis-typed) - a bit more trouble at compile time, but a lot less debugging at run time.

Example:
Avar = 2
AAVar = 3

this will result in two variables being created, both of type variant, one with the value of 2 the other 3.

while this example
Option Explicit
Dim AVar as Integer
Avar = 2
AAVar = 3

will result in a compile time error because AAvar was not declared and the Option Explicit makes the use of a non-declared variable an error.

Dave





P.S. Dave what did you mean by
Quote
( you will have to take out the extra CRLFs inserted by the board width limitation when this was pasted)
Also the option explicit you added at the top of the code caused the same syntax error I removed and it was fine.


Author of the MachStdMill Extensions for Mach3
www.CalypsoVentures.com

Offline zealous

*
  •  489 489
  • HI!
    • View Profile
    • Artsoft Solutions
Re: For Loops inside of If Statement Help Please
« Reply #6 on: February 13, 2010, 11:46:14 PM »
BTW: I just noticed the "_" to resolve the Compile Error syntax error...but haven't looked at if what code you have will work  ;D So go with whatever Dave has for you :)