Hello Guest it is March 28, 2024, 09:28:47 AM

Author Topic: VB compile error: syntax error  (Read 3980 times)

0 Members and 1 Guest are viewing this topic.

VB compile error: syntax error
« 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.

Offline Tweakie.CNC

*
  • *
  •  9,196 9,196
  • Super Kitty
    • View Profile
Re: VB compile error: syntax error
« Reply #1 on: June 07, 2020, 01:11:03 AM »
If you post your script we could perhaps help.

Tweakie.
PEACE
Re: VB compile error: syntax error
« Reply #2 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.

Offline Tweakie.CNC

*
  • *
  •  9,196 9,196
  • Super Kitty
    • View Profile
Re: VB compile error: syntax error
« Reply #3 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.
« Last Edit: June 07, 2020, 05:07:10 AM by Tweakie.CNC »
PEACE
Re: VB compile error: syntax error
« Reply #4 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. 

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: VB compile error: syntax error
« Reply #5 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
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: VB compile error: syntax error
« Reply #6 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.

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: VB compile error: syntax error
« Reply #7 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
« Last Edit: June 07, 2020, 08:22:34 AM by TPS »
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: VB compile error: syntax error
« Reply #8 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!

Offline TPS

*
  •  2,501 2,501
    • View Profile
Re: VB compile error: syntax error
« Reply #9 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

anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.