Try the code like this, Mach3 can be very fussy about Sleep commands. Add the Declare Sub line to make sure its using the correct time length.
Rule of thumb is :-
If moving then sleep in a loop with a short time 25 to 50 m/s.
If updating DRO's or system variables then NO loop 50 to 100 m/s time is good.
Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
VPALP = GETOEMDRO(1060)
HPALPEUR = GETOEMDRO(1040)
HSURFACE = GETOEMDRO(1050)
NOUVEAUZ = HPALPEUR+HSURFACE
HFIN = GETOEMDRO(1051)
If HFIN < NOUVEAUZ Then
HFIN = NOUVEAUZ
SETOEMDRO(1051,HFIN)
EndIf
CODE "G31 Z-200 F"& VPALP
While ISMOVING()
Sleep(50)
Wend
ZPALP = GETOEMDRO(802)
SLEEP(50)
CODE "G00 Z"& ZPALP
While ISMOVING()
Sleep(50)
Wend
SETOEMDRO(802,NOUVEAUZ)
SLEEP(50)
CODE "G00 Z"& HFIN
While ISMOVING()
Sleep(50)
Wend
For reference here is part of my spindle probe code for finding the top of the work, it will give you some ideas on how the code needs to be.
'Spindle Probe - Top Face Only
Code "G" & 53 + Current_Fixture
'Lets try probing the Z surface
If GetOemLed(825) = 0 Then
'Fast move
Point = Move + Overrun
Code "G49"
Code "G31 G91 Z-" & Point & " F" & ProbeFastFeed
While IsMoving()
Sleep(50)
Wend
If GetOemLed(825) Then
'We have a hit retract and check again
Code "G00 G91 Z" & FirstRetract
While IsMoving()
Sleep(50)
Wend
Sleep(100)
'Slow move
Point = FirstRetract + Overrun
Code "G31 G91 Z-" & Point & " F" & ProbeSlowFeed
While IsMoving()
Sleep(50)
Wend
If GetOemLed(825) Then
'Small retract
Code "G00 G91 Z" & SecondRetract
While IsMoving()
Sleep(50)
Wend
'Sleep(100)
'Creep move
Point = SecondRetract + Overrun
Code "G31 G91 Z-" & Point & " F" & ProbeCreepFeed
While IsMoving()
Sleep(50)
Wend
Sleep(100)
Z_ProbePos = GetVar(2002)
Sleep(100)
Call SetDro(2,0)
Code "G4 P0.5"
Code "G00 G91 Z" & Move
While IsMoving()
Sleep(50)
Wend
Else
Code "(No face found, 2nd probing cancelled)"
Exit Sub
End If
Else
'No face found
Code "G00 G91 Z" & Move + Overrun
While IsMoving()
Sleep(50)
Wend
Code "(No face found, 1st probing cancelled)"
End If
Else
Code "(Probe is triggered or obstructed, cancelled probing)"
Exit Sub
End If