Machsupport Forum

General CNC Chat => Building or Buying a Wood routing table.. Beginnners guide.. => Topic started by: Vernon on June 06, 2020, 01:44:28 PM

Title: VB compile error: syntax error
Post by: Vernon on June 06, 2020, 01:44:28 PM
I am very new to VB scripting but I have written a piece of code to zero x, y and z axes and when I try to run it in Mach3 VB editor I get the aforementioned 'compile error: syntax error'. It doesn't seem to give me any indication of what or where the error is. Should it not highlight the error in some way? Any help gratefully received.
Title: Re: VB compile error: syntax error
Post by: Tweakie.CNC on June 07, 2020, 01:11:03 AM
If you post your script we could perhaps help.

Tweakie.
Title: Re: VB compile error: syntax error
Post by: Vernon on June 07, 2020, 03:56:01 AM
Thanks for your reply. Here is my script:

' Auto zero all axes Or just tool length

CurrentFeed = GetOemDRO(818) 'Get the current feedrate to return to later
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state
CurrentGmode = GetOemDRO(819) 'Get the current G0/G1 state

' Define constant for MachMsg dialog types

MachMsgTypeYesNo = 4

' Define constant for MachMsg return codes

MachMsgReturnYes = 6
MachMsgReturnNo = 7

' Display a Yes or No dialog
Ret = MachMsg("Zero Z axis only?", _
 "Zero Z axis only or all axes", MachMsgTypeYesNo)
If Ret = MachMsgReturnYes Then

Message "(Starting Z Zero...)"
If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Code "(Z axis is now zeroed)" 'puts this message in the status bar
Code "F" &CurrentFeed 'Returns to prior feed rate
Code "G4 P3" 'Delay to allow reading of status bar

Else
If Ret = MachMsgReturnNo Then
Messaage "(Starting X-Y Corner setting...)"

Rem VB Script For probing X axis

Message "(Jog CNC near centre and 5mm inside the hole, then click OK)"
XCurrent = GetDro(0)
YCurrent = GetDro(1)
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "F100"

Rem Probe Left
XNew = XCurrent -50 'probe 50mm to left
Code "G31 X" &XNew
While IsMoving() ' wait for the move to finish
Wend
XPos1 = GetVar(2000) 'get the probe touch location
Code "G0 X" &XCurrent 'rapid move back to start point

Rem Probe Right
XNew = XCurrent +50 'probe 50mm to right
Code "G31 X" &XNew
While IsMoving()
Wend
XPos2 = GetVar(2000)
XCentre = (XPos1 + XPos2)/2 'centre is midway between XPos1 and XPos2
Code "G0 X" &XCentre ' rapid move to x centre location

Rem Probe up
YNew = YCurrent +50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos1 = GetVar(2001)
Code "G0 Y" &YCurrent

Rem Probe down
YNew = YCurrent -50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos2 = GetVar(2001)
YCentre = (YPos1 + YPos2)/2
Code "G0 Y" &YCentre 'rapid move to y centre location
While isMoving()
Wend

Message "(Starting Z Zero...)"
Code "G0 Z20"
Code "G0 X20Y20"

If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Message "(Z axis is now zeroed)" 'puts this message in the status bar

Code "F" &FeedCurrent  'restore starting feed rate
DoOEMButton (1008)
DoOEMButton (1009)

If CurrentAbsInc = 0 Then 'If G91 was in effect before then return to it
Code "G91"

If CurrentGMode = 0 Then 'if G0 was in effect before then return to it
Code "G0"
 
End If

 
I am sure it will be full of bugs but my point is that when I get the syntax error, how do I know where it is?

Many thanks.
Title: Re: VB compile error: syntax error
Post by: Tweakie.CNC on June 07, 2020, 04:18:43 AM
Quote
I am sure it will be full of bugs but my point is that when I get the syntax error, how do I know where it is?

The script editor is not perfect but usually it highlights part of the error line or gives some indication of the area in which the error exists.
Hopefully TPS will be able to give you more information as he is the VB expert.

(your script is bit of a mess, presumably from copy and paste from other scripts, I haven't counted but If's and End If's don't seem to tally - but it's nothing that cant be sorted).

Tweakie.
Title: Re: VB compile error: syntax error
Post by: Vernon on June 07, 2020, 06:16:19 AM
Yes. Some of it is from other scripts found online and some is from the Programmers Reference Manual. 
Title: Re: VB compile error: syntax error
Post by: TPS on June 07, 2020, 06:30:25 AM
here we go, not tested, just made it readable and running.

Code: [Select]
' Auto zero all axes Or just tool length

CurrentFeed = GetOemDRO(818) 'Get the current feedrate to return to later
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state
CurrentGmode = GetOemDRO(819) 'Get the current G0/G1 state

' Define constant for MachMsg dialog types

MachMsgTypeYesNo = 4

' Define constant for MachMsg return codes

MachMsgReturnYes = 6
MachMsgReturnNo = 7

' Display a Yes or No dialog
Ret = MachMsg("Zero Z axis only?", _
 "Zero Z axis only or all axes", MachMsgTypeYesNo)

If Ret = MachMsgReturnYes Then

Message "(Starting Z Zero...)"
If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Code "(Z axis is now zeroed)" 'puts this message in the status bar
Code "F" &CurrentFeed 'Returns to prior feed rate
Code "G4 P3" 'Delay to allow reading of status bar

Else
If Ret = MachMsgReturnNo Then
Message "(Starting X-Y Corner setting...)"

Rem VB Script For probing X axis

Message "(Jog CNC near centre and 5mm inside the hole, then click OK)"
XCurrent = GetDro(0)
YCurrent = GetDro(1)
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "F100"

Rem Probe Left
XNew = XCurrent -50 'probe 50mm to left
Code "G31 X" &XNew
While IsMoving() ' wait for the move to finish
Wend
XPos1 = GetVar(2000) 'get the probe touch location
Code "G0 X" &XCurrent 'rapid move back to start point

Rem Probe Right
XNew = XCurrent +50 'probe 50mm to right
Code "G31 X" &XNew
While IsMoving()
Wend
XPos2 = GetVar(2000)
XCentre = (XPos1 + XPos2)/2 'centre is midway between XPos1 and XPos2
Code "G0 X" &XCentre ' rapid move to x centre location

Rem Probe up
YNew = YCurrent +50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos1 = GetVar(2001)
Code "G0 Y" &YCurrent

Rem Probe down
YNew = YCurrent -50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos2 = GetVar(2001)
YCentre = (YPos1 + YPos2)/2
Code "G0 Y" &YCentre 'rapid move to y centre location
While isMoving()
Wend

Message "(Starting Z Zero...)"
Code "G0 Z20"
Code "G0 X20Y20"

If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Message "(Z axis is now zeroed)" 'puts this message in the status bar

Code "F" &FeedCurrent  'restore starting feed rate
DoOEMButton (1008)
DoOEMButton (1009)
End If
End If

If CurrentAbsInc = 0 Then 'If G91 was in effect before then return to it
Code "G91"
End If

If CurrentGMode = 0 Then 'if G0 was in effect before then return to it
Code "G0"
End If
Title: Re: VB compile error: syntax error
Post by: Vernon on June 07, 2020, 07:23:00 AM
Wow. That's fantastic. Thank you. I will try it later. I would like to be able to debug these scripts myself (I am still trying to educate myself at 70 years old). Can you recommend some reading on the subject (Mach3 orientated).
Thanks again.
Title: Re: VB compile error: syntax error
Post by: TPS on June 07, 2020, 08:20:30 AM
this two documents should give you a good reference and overview

https://www.machsupport.com/wp-content/uploads/2013/02/Mach3_V3.x_Macro_Prog_Ref.pdf
http://www.machsupport.com/wp-content/uploads/2013/02/VBScript_Commands.pdf

as far i know, this is all what is avaliable.

here:

https://www.machsupport.com/forum/index.php?topic=6657.90

you can find Information about DRO's Button's and Led's
Title: Re: VB compile error: syntax error
Post by: Vernon on June 07, 2020, 08:52:15 AM
Many thanks. You are right. Your line inents make the script so much easier to read and logical. However the next problem is that I now get get an 'Error on line: 18 - Sub or function not defined: MachMsg'. I have checked it a dozen times with the Programmers Reference Manual and cannot find the error. It does place the cursor at the beginning of line 18 I am pleased to see. Any ideas? I'm afraid this may not be the last of my questions!
Title: Re: VB compile error: syntax error
Post by: TPS on June 07, 2020, 03:24:44 PM
i have modified the code a Little bit, maybe you can test again, here the original is running.

Code: [Select]
' Auto zero all axes Or just tool length

CurrentFeed = GetOemDRO(818) 'Get the current feedrate to return to later
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state
CurrentGmode = GetOemDRO(819) 'Get the current G0/G1 state

' Define constant for MachMsg dialog types

MachMsgTypeYesNo = 4

' Define constant for MachMsg return codes

MachMsgReturnYes = 6
MachMsgReturnNo = 7

' Display a Yes or No dialog
Ret = MachMsg("Zero Z axis only?", "Zero Z axis only or all axes", MachMsgTypeYesNo)

If Ret = MachMsgReturnYes Then

Message "(Starting Z Zero...)"
If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Code "(Z axis is now zeroed)" 'puts this message in the status bar
Code "F" &CurrentFeed 'Returns to prior feed rate
Code "G4 P3" 'Delay to allow reading of status bar

Else
If Ret = MachMsgReturnNo Then
Message "(Starting X-Y Corner setting...)"

Rem VB Script For probing X axis

Message "(Jog CNC near centre and 5mm inside the hole, then click OK)"
XCurrent = GetDro(0)
YCurrent = GetDro(1)
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "F100"

Rem Probe Left
XNew = XCurrent -50 'probe 50mm to left
Code "G31 X" &XNew
While IsMoving() ' wait for the move to finish
Wend
XPos1 = GetVar(2000) 'get the probe touch location
Code "G0 X" &XCurrent 'rapid move back to start point

Rem Probe Right
XNew = XCurrent +50 'probe 50mm to right
Code "G31 X" &XNew
While IsMoving()
Wend
XPos2 = GetVar(2000)
XCentre = (XPos1 + XPos2)/2 'centre is midway between XPos1 and XPos2
Code "G0 X" &XCentre ' rapid move to x centre location

Rem Probe up
YNew = YCurrent +50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos1 = GetVar(2001)
Code "G0 Y" &YCurrent

Rem Probe down
YNew = YCurrent -50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos2 = GetVar(2001)
YCentre = (YPos1 + YPos2)/2
Code "G0 Y" &YCentre 'rapid move to y centre location
While isMoving()
Wend

Message "(Starting Z Zero...)"
Code "G0 Z20"
Code "G0 X20Y20"

If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Message "(Z axis is now zeroed)" 'puts this message in the status bar

Code "F" &FeedCurrent  'restore starting feed rate
DoOEMButton (1008)
DoOEMButton (1009)
End If
End If

If CurrentAbsInc = 0 Then 'If G91 was in effect before then return to it
Code "G91"
End If

If CurrentGMode = 0 Then 'if G0 was in effect before then return to it
Code "G0"
End If

Title: Re: VB compile error: syntax error
Post by: Vernon on June 07, 2020, 04:00:38 PM
Thanks for that but still getting the same error albeit now on line 17. Very strange that it runs on your setup but not on mine.
Title: Re: VB compile error: syntax error
Post by: TPS on June 08, 2020, 01:22:56 AM
ok made a different Version witch uses MsgBox instead of MachMsg

Code: [Select]
' Auto zero all axes Or just tool length

CurrentFeed = GetOemDRO(818) 'Get the current feedrate to return to later
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state
CurrentGmode = GetOemDRO(819) 'Get the current G0/G1 state


'old version witch did not work *********************************************************
' Define constant for MachMsg dialog types
'MachMsgTypeYesNo = 4

' Define constant for MachMsg return codes
'MachMsgReturnYes = 6
'MachMsgReturnNo = 7

' Display a Yes or No dialog
'Ret = MachMsg("Zero Z axis only?", "Zero Z axis only or all axes", MachMsgTypeYesNo)
'****************************************************************************************


' Define constant for MsgBox dialog types
MsgBoxTypeYesNo = 4

' Define constant for MachMsg return codes
MsgBoxReturnYes = 6
MsgBoxReturnNo  = 7

' Display a Yes or No dialog
Ret = MsgBox ("Zero Z axis only?", MsgBoxTypeYesNo , "Zero Z axis only or all axes")


If Ret = MsgBoxReturnYes Then

Message "(Starting Z Zero...)"
If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Code "(Z axis is now zeroed)" 'puts this message in the status bar
Code "F" &CurrentFeed 'Returns to prior feed rate
Code "G4 P3" 'Delay to allow reading of status bar

Else
If Ret = MsgBoxReturnNo Then
Message "(Starting X-Y Corner setting...)"

Rem VB Script For probing X axis

Message "(Jog CNC near centre and 5mm inside the hole, then click OK)"
XCurrent = GetDro(0)
YCurrent = GetDro(1)
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "F100"

Rem Probe Left
XNew = XCurrent -50 'probe 50mm to left
Code "G31 X" &XNew
While IsMoving() ' wait for the move to finish
Wend
XPos1 = GetVar(2000) 'get the probe touch location
Code "G0 X" &XCurrent 'rapid move back to start point

Rem Probe Right
XNew = XCurrent +50 'probe 50mm to right
Code "G31 X" &XNew
While IsMoving()
Wend
XPos2 = GetVar(2000)
XCentre = (XPos1 + XPos2)/2 'centre is midway between XPos1 and XPos2
Code "G0 X" &XCentre ' rapid move to x centre location

Rem Probe up
YNew = YCurrent +50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos1 = GetVar(2001)
Code "G0 Y" &YCurrent

Rem Probe down
YNew = YCurrent -50
Code "G31 Y" &YNew
While isMoving()
Wend
YPos2 = GetVar(2001)
YCentre = (YPos1 + YPos2)/2
Code "G0 Y" &YCentre 'rapid move to y centre location
While isMoving()
Wend

Message "(Starting Z Zero...)"
Code "G0 Z20"
Code "G0 X20Y20"

If GetOemLed (825)<>0 Then 'Check to see if the probe is already grounded or faulty
Message "(Z-Plate is grounded, check connection and try again)" 'this goes in the status bar if applicable
End If

DoOEMButton (1010) 'zero the Z axis so the probe move will start from here
Code "G4 P3" 'this 3 sec. delay to get from computer to hold probe plate in place
Code "G90 G31Z-25 F100" 'probing move, can set the feed rate here as well as how far to move
While IsMoving() 'wait while it happens
Wend
ZProbePos = GetVar(2002) 'get the exact point the probe was hit
Code "G0 Z" &ZProbePos 'go back to that point, always a very small amount of overrun
While IsMoving ()
Wend

' Define some constants
ZaxisDRO = 2
' Set Z Axis DRO to 1.234 (plate thickness)
SetOEMDRO(ZaxisDRO, 1.234)

Sleep 200 'Pause for Dro to update
Code "G1 Z1. F50" 'put the Z retract height you want here, must be greater than the touch plate thickness
While IsMoving ()
Wend
Message "(Z axis is now zeroed)" 'puts this message in the status bar

Code "F" &FeedCurrent  'restore starting feed rate
DoOEMButton (1008)
DoOEMButton (1009)
End If
End If

If CurrentAbsInc = 0 Then 'If G91 was in effect before then return to it
Code "G91"
End If

If CurrentGMode = 0 Then 'if G0 was in effect before then return to it
Code "G0"
End If


Title: Re: VB compile error: syntax error
Post by: Vernon on June 08, 2020, 11:12:18 AM
Fantastic! Thank you so much. It worked on my laptop (Windows 10) and then on my machine (Windows XP). Needs a little fine tuning to the plate thickness etc but works ok. Thanks again.