Attribute VB_Name = "ModFLProbe"
'''Personalizzazione Elettronica
Public hDmcProbe As Long
Public MovingProbe As Integer
Public FpDone As Integer
Public FpError As Integer
Public ProbeGalilFile As String
Public InJogCheProbe As Integer
Public SpeedMaxProbe As Long
Public AccMaxProbe As Long
Public LamCheProbe As Integer
Public BistPulsProbe As Integer
Public EnablePulsProbe As Integer

Public PositionProbeAX As Long
Public PositionProbeAY As Long
Public PositionProbeBX As Long
Public PositionProbeBY As Long
Public MasterRefXProbeA As Long
Public MasterRefYProbeA As Long
Public MasterRefXProbeB As Long
Public MasterRefYProbeB As Long

Public ImportXStart As String
Public ImportYStart As String
Public ImportProbeAX As String
Public ImportProbeAY As String
Public ImportProbeBX As String
Public ImportProbeBy As String
Public ImportRefX As String
Public ImportRefY As String

Sub CheckErrProbe(tcmd$, errcode&, ermsg$)
        If errcode& <> 0 Then
            ermsg$ = Space$(256)
            sc& = DMCError(hDmcProbe, errcode&, ermsg$, 256&)
            ch% = InStr(ermsg$, Chr$(0))
            If ch% > 0 Then ermsg$ = Left$(ermsg$, ch% - 1)
            ermsg$ = Trim$(ermsg$)
            If ermsg$ <> "" Then Call updatelog("Galil 2 <Fl Probe> Error>" + tcmd$ + ":" + ermsg$, 1)
        End If
End Sub

Sub OpenGalilProbe()
    If hDmcProbe <> -1 Then Exit Sub
    Controller% = 2
    For i% = 1 To 10
        rc& = DMCOpen(Controller%, 0, hDmcProbe)
        
        If rc& = 0 Then Exit For
    Next i%
    Call CheckErrProbe("OPEN", rc&, ermsg$)
    If rc& = 0 Then
        Call updatelog(mss$(118), 0)
        Rl% = 256
        Call updatelog(mss$(119), 2)
        rc& = DMCClear(hDmcProbe)
        Call CheckErrProbe("CLEAR", rc&, ermsg$)
        Call GalilCmdProbe(";", res$, ermsg$)
        Call GalilCmdProbe("AB", res$, ermsg$)
        Call GalilCmdProbe("SH XYZW", res$, ermsg$)
        Call GalilCmdProbe("AC 200000,200000,200000,200000", res$, ermsg$)
        Call GalilCmdProbe("DC 200000,200000,200000,200000", res$, ermsg$)
        rc& = DMCDownloadFile(hDmcProbe, thepath$ + ProbeGalilFile, "")
        Call CheckErrProbe("DOWNLOAD", rc&, ermsg$)
        Call updatelog(mss$(120), 2)
        td& = timeGetTime&()
        Do
            Call readdevice(0, 0, 5)
            ttdd& = timeGetTime&()
            If ttdd& - td& > 5000 Then
                Call updatelog(mss$(121), 1)
                Exit Do
            End If
            DoEvents
        Loop While d0.I_Inibizione_Azionamenti = 1
        'Call ProbeUp(checkok%)
        checkok% = 1
        If checkok% = 0 Then
            MsgBox ("Unable to raise probes")
        Else
            Call GalilCmdProbe("XQ #HOME,1", res$, ermsg$)
        End If
    Else
        hDmcProbe = -1
        Call updatelog(mss$(122), 1)
    End If
End Sub

Sub GalilCmdProbe(tcmd$, res$, ermsg$)
    'invia un comando al controller e verifica se ci sono errori sul comando
    If debugging% = 1 Then Call updatedebug(tcmd$)
    tcmd$ = UCase$(tcmd$)
    Response$ = Space$(256)
    Rl% = 256
    If res$ = "A" Then
        Rl% = 2560
        res$ = ""
        Response$ = Space$(Rl%)
    End If
    ermsg$ = ""
    If hDmcProbe <> -1 Then
        rc& = DMCCommand(hDmcProbe, tcmd$, Response$, Rl%)
        Call CheckErrProbe(tcmd$, rc&, ermsg$)
        cr% = InStr(Response$, Chr$(13) + Chr$(10))
        If cr% > 0 Then res$ = Trim$(Left$(Response$, cr% - 1))
        If debugging% = 1 Then Call updatedebug(res$)
   
    Else
        ermsg$ = mss$(117)
    End If
 End Sub
 
Sub GotoPosWProbe(GotoWProbe)
    dontprobe% = 0
    If hDmcProbe = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    Call GalilCmdProbe("TDW", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoWProbe = Trim$(Mid$(tres$, cm% + 1))
    End If
    If GotoWProbe <> "" Then
        Call GalilCmdProbe("ST W", res$, ermsg$)
        tcmd$ = "SP ,,," + Str(SpeedMaxProbe)
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        tcmd$ = "PA ,,," + GotoWProbe + ";BG W"
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        FrmEuro.TmrMoveProbe.Interval = 100
        FrmEuro.TmrMoveProbe.Enabled = True
        MovingProbe = 1
    End If
End Sub

Sub GotoPosXYProbe(GotoXProbe$, GotoYProbe$)
    If hDmcProbe = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    If GotoXProbe$ <> "" And GotoYProbe$ <> "" Then
        Call GalilCmdProbe("ST XYZ", res$, ermsg$)
        tcmd$ = "SP" + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe)
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        tcmd$ = "PA " + GotoXProbe$ + "," + GotoYProbe$ + ";BG XY"
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        FrmEuro.TmrMoveProbe.Interval = 100
        FrmEuro.TmrMoveProbe.Enabled = True
        MovingProbe = 1
    End If
End Sub

Sub GotoPosXYZWProbe(GotoXProbe, GotoYProbe, GotoZProbe, GotoWProbe)
    Static TGotoZProbe As String
    Static TGotoWProbe As String
    dontprobe% = 0
    Call GalilCmdProbe("TDZW", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoZProbe = Trim$(Left$(tres$, cm% - 1))
        TGotoWProbe = Trim$(Mid$(tres$, cm% + 1))
    End If
    If GotoXProbe <> "" And GotoYProbe <> "" And GotoZProbe <> "" And GotoWProbe <> "" Then
        If Abs(Val(TGotoZProbe) - Val(GotoZProbe)) > 5 Or Abs(Val(TGotoWProbe) - Val(GotoWProbe)) > 5 Then
            Call ProbeUp(checkok%)
            If checkok% = 0 Then
                MsgBox ("Unable to raise probes")
                FrmEuro.TmrMoveProbe.Interval = 100
                FrmEuro.TmrMoveProbe.Enabled = True
                MovingProbe = 1
                Exit Sub
            End If
        End If
        Call GalilCmdProbe("ST XYZW", res$, ermsg$)
        tcmd$ = "SP" + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe)
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        tcmd$ = "PA " + GotoXProbe + "," + GotoYProbe + "," + GotoZProbe + "," + GotoWProbe + ";BG XYZW"
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        FrmEuro.TmrMoveProbe.Interval = 100
        FrmEuro.TmrMoveProbe.Enabled = True
        MovingProbe = 1
    End If
End Sub

Sub GotoPosZProbe(GotoZProbe)
    dontprobe% = 0
    If hDmcProbe = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    Call GalilCmdProbe("TDZ", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoZProbe = Trim$(Mid$(tres$, cm% + 1))
    End If
    If GotoZProbe <> "" Then
        Call GalilCmdProbe("ST Z", res$, ermsg$)
        tcmd$ = "SP ,," + Str(SpeedMaxProbe)
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        tcmd$ = "PA ,," + gotoz$ + ";BG Z"
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        FrmEuro.TmrMoveProbe.Interval = 100
        FrmEuro.TmrMoveProbe.Enabled = True
        MovingProbe = 1
    End If
End Sub

Sub GotoPosZWProbe(GotoZProbe, GotoWProbe)
    dontprobe% = 0
    If hDmcProbe = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    Call GalilCmdProbe("TDZW", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoZProbe = Trim$(Left$(tres$, cm% - 1))
        TGotoWProbe = Trim$(Mid$(tres$, cm% + 1))
    End If
    If GotoZProbe <> "" And GotoWProbe <> "" Then
        If Abs(Val(TGotoZProbe) - Val(GotoZProbe)) > 5 Or Abs(Val(TGotoWProbe) - Val(GotoWProbe)) > 5 Then
            Call ProbeUp(checkok%)
            If checkok% = 0 Then
                MsgBox ("Unable to raise probes")
                FrmEuro.TmrMoveProbe.Interval = 100
                FrmEuro.TmrMoveProbe.Enabled = True
                MovingProbe = 1
                Exit Sub
            End If
        End If
        Call GalilCmdProbe("ST ZW", res$, ermsg$)
        tcmd$ = "SP ,," + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe)
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        tcmd$ = "PA ,," + GotoZProbe + "," + GotoWProbe + ";BG ZW"
        Call GalilCmdProbe(tcmd$, res$, ermsg$)
        FrmEuro.TmrMoveProbe.Interval = 100
        FrmEuro.TmrMoveProbe.Enabled = True
        MovingProbe = 1
    End If
End Sub

Public Sub MoveProbe(MProbeAX As String, MProbeAY As String, MProbeBX As String, MProbeBY As String, MoveMode As Integer, Optional CoordMode As Integer, Optional MoveWProbe As Integer)
    Dim TmpMoveCommand As String
    Dim TmpMoveRes As String
    
    Static TmpMProbeAX As Long
    Static TmpMProbeAy As Long
    Static TmpMProbeBX As Long
    Static TmpMProbeBy As Long
    
    Static TmpMoveProbeStartTime As Long
    Static TmpMoveProbeStopTime As Long
    
    Static TmpPosAttX As String
    Static TmpPosAttY As String
    Static ProbeStMove As Integer
    
    If Frmedit.Visible = True Then
        Frmedit.Enabled = False
        Frmedit.MousePointer = 11
    End If
    
    If frmTstProbe.Visible = True Then frmTstProbe.cmdTstProbe.Enabled = False
    If CoordMode <> 0 And CoordMode <> 1 Then CoordMode = 0
    'MoveMode = 0 Non attende il posizionamento
    'MoveMode = 1 Attende la Fine del Posizionamento
    Call ProbeUp
    Call AddLogProbe(Str(Date) + ":" + Str(Time))
    
    Call GetPosXYProbe(TmpPosAttX, TmpPosAttY)
    Call AddLogProbe("XAtt=" + TmpPosAttX)
    Call AddLogProbe("YAtt=" + TmpPosAttY)
    Call GetPosZWProbe(TmpPosAttX, TmpPosAttY)
    Call AddLogProbe("XAtt=" + TmpPosAttX)
    Call AddLogProbe("YAtt=" + TmpPosAttY)
    
    TmpMoveCommand = "CORSA_XZ=" + Str(OffSetPuntaliX)
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    TmpMoveCommand = "CORSA_YW=" + Str(OffSetPuntaliY)
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    
    If CoordMode = 1 Then
        TmpMProbeAX = Val(MProbeAX)
        If TmpMProbeAX <= 0 Then TmpMProbeAX = 1
        TmpMProbeAy = Val(MProbeAY)
        If TmpMProbeAy <= 0 Then TmpMProbeAy = 1
        TmpMProbeBX = Val(MProbeBX)
        If TmpMProbeBX <= 0 Then TmpMProbeBX = 1
        TmpMProbeBy = Val(MProbeBY)
        If TmpMProbeBy <= 0 Then TmpMProbeBy = 1
    Else
        TmpMProbeAX = Val(MProbeAX)
        If TmpMProbeAX <= 0 Then TmpMProbeAX = 1
        
        TmpMProbeAy = Val(MProbeAY)
        If TmpMProbeAy <= 0 Then TmpMProbeAy = 1
        
        TmpMProbeBX = OffSetPuntaliX - Val(MProbeBX)
        If (TmpMProbeBX <= 0) Then TmpMProbeBX = 1
        If (TmpMProbeBX > 15000) Then TmpMProbeBX = 15000
        
        TmpMProbeBy = OffSetPuntaliY - Val(MProbeBY)
        If TmpMProbeBy <= 0 Then TmpMProbeBy = 1
        If (TmpMProbeBX > 15000) Then TmpMProbeBX = 15000
        
    End If
    CoordMode = 0
    
    FpDone = 0
    Call GalilCmdProbe("FPDONE=0", res$, ermsg$)
    TmpMoveCommand = "ST"
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    TmpMoveCommand = "SHXYZW"
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    
    TmpMoveCommand = "FPDONE=0"
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    
    TmpMoveCommand = "XN=" + Trim(Str(TmpMProbeAX))
    Call AddLogProbe(TmpMoveCommand)
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    
    TmpMoveCommand = "YN=" + Trim(Str(TmpMProbeAy))
    Call AddLogProbe(TmpMoveCommand)
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    
    TmpMoveCommand = "ZN=" + Trim(Str(TmpMProbeBX))
    Call AddLogProbe(TmpMoveCommand)
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    
    TmpMoveCommand = "WN=" + Trim(Str(TmpMProbeBy))
    Call AddLogProbe(TmpMoveCommand)
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    
    TmpMoveProbeStartTime = timeGetTime
    Select Case MoveWProbe
        Case 0
            'Muove Entrambi i Puntali
            TmpMoveCommand = "XQ#NPOS"
        
        Case 1
            'Muove Solo Il Probe A
            TmpMoveCommand = "XQ#NPOSA"
        
        Case 2
            'Muove Solo Il Probe B
            TmpMoveCommand = "XQ#NPOSB"
        
        Case 3
            'Muove Prima Il Probe A Poi IL Probe B
            
            
    End Select
    Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    If frmTstProbe.Visible = True Then frmTstProbe.lblFPStatus.Caption = ""
    If MoveMode = 1 Then
        TmpMoveCommand = "FPDONE="
        Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
        If (Val(TmpMoveRes) <> 0) Then
            FpDone = 1
        Else
            FpDone = 0
        End If
        Do While Not FpDone = 1
            DoEvents
            'controllo FPDONE
            TmpMoveCommand = "FPDONE="
            Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
            TmpMoveProbeStopTime = timeGetTime
            If frmTstProbe.Visible = True Then frmTstProbe.lblFPStatus.Caption = "FPDone: " + Str(Val(TmpMoveRes))
            If TmpMoveProbeStopTime - TmpMoveProbeStartTime >= 40000 Then TmpMoveRes = "3"
            DoEvents
            Select Case Val(TmpMoveRes)
                Case 0
                    FpDone = 0
                    
                Case 1
                    FpDone = 1
                    Exit Do
                Case 2
                    FpDone = 2
                    Call MsgBox("ERROR IN THE MOTION OF THE PROBES", vbOKOnly)
                    Exit Do
                Case 3
                    FpDone = 1
                    Call MsgBox("TIME OUT OR ERROR IN THE MOTION OF THE PROBES", vbOKOnly)
                    Exit Do
                End Select
        Loop
        If frmTstProbe.Visible = True Then frmTstProbe.cmdTstProbe.Enabled = True
        TmpMoveCommand = "FPDONE=0"
        Call GalilCmdProbe(TmpMoveCommand, TmpMoveRes, ermsg$)
    End If
    If (Frmedit.Visible = True) Or (EditMode = 1) Then
        Frmedit.Enabled = True
        Frmedit.MousePointer = 0
        Frmedit.ZOrder 0
    End If
End Sub

Public Function TestProbe() As Integer
    Static TmpStatusProbe As Integer
    Static TmpCommand As String
    Static TmpMoveRes As String
    
    TestProbe = 0
    TmpCommand = "FPDONE="
    Call GalilCmdProbe(TmpCommand, TmpMoveRes, ermsg$)
    TmpStatusProbe = Val(TmpMoveRes)
    FpDone = Val(TmpMoveRes)
    TestProbe = TmpMoveRes
End Function


Sub JogXYProbe()
    If flagpuntali% = 2 Then Exit Sub
    Call UpdatePosXYProbe
    If MovingProbe > 0 Then Exit Sub
    Static enabler%
    Static enabler1%
    Static enabler2%
    Static ocmd$
    Static spxx&
    Static spyy&
    Static clk%
    Static crk%
    Static cuk%
    Static cdk%
    Static intProbe1 As Integer
    Static intProbe2 As Integer

    ths% = 15
    dt& = timeGetTime()
    a& = GetCursorPos&(pt)
    thexx& = Screen.Width / (2 * Screen.TwipsPerPixelX)
    theyy& = Screen.Height / (2 * Screen.TwipsPerPixelY)
    dx& = pt.x - thexx&
    dY& = theyy& - pt.y
    'accelerazioni assi
    accx$ = AccMaxProbe
    accy$ = AccMaxProbe
    spx& = Int((mult& * dx&) / (dt& - odt&))
    spy& = Int((mult& * dY&) / (dt& - odt&))
    Select Case spx&
    Case Is < -SpeedMaxProbe
        spx& = -SpeedMaxProbe
    Case Is > SpeedMaxProbe
        spx& = SpeedMaxProbe
    End Select
    Select Case spy&
    Case Is < -SpeedMaxProbe
        spy& = -SpeedMaxProbe
    Case Is > SpeedMaxProbe
        spy& = SpeedMaxProbe
    End Select
    intProbe1 = GetAsyncKeyState(&H31)
    intProbe2 = GetAsyncKeyState(&H32)
    ck% = GetAsyncKeyState%(speedkey%) 'ctrl key
    lk% = GetAsyncKeyState%(&H25) 'left key
    uk% = GetAsyncKeyState%(&H26) 'up key
    rk% = GetAsyncKeyState%(&H27) 'right key
    dk% = GetAsyncKeyState%(&H28) 'down key
    esk% = GetAsyncKeyState%(27) 'escape key
    enk% = GetAsyncKeyState%(13) 'enter key
    EnablePulsProbe = GetAsyncKeyState%(&H1)
'    lmk% = GetAsyncKeyState%(&H1) 'left mouse button
    rmk% = GetAsyncKeyState%(&H2) 'right mouse button
    shk% = GetAsyncKeyState%(&H10) 'shift key
    
    Select Case intProbe1
        Case 0
        
        Case Else
            InJogCheProbe = 1
            BistPulsProbe = 1
    End Select
    
    Select Case intProbe2
        Case 0
        
        Case Else
            InJogCheProbe = 2
            BistPulsProbe = 0
    End Select

    Select Case shk%
    Case 0
        enabler2% = enabler2% Or 1
    Case Else
        If enabler2% = 1 Then
            'ho spinto shift
            enabler2% = 0
            If dispstep% = 0 Then
                dispstep% = 1
            Else
                dispstep% = 0
            End If
        End If
    End Select

    Select Case esk%
    Case 0
        enabler% = enabler% Or 1
    Case Else
        If enabler% = 15 Then
            'ho spinto esc
            escjog% = 1
            enabler% = 0
        End If
    End Select

    Select Case enk%
    Case 0
        enabler% = enabler% Or 2
    Case Else
        If enabler% = 15 Then
            getv% = 1
            escjog% = 1
            enabler% = 0
            'ho spinto enter
        End If
    End Select
    
    Select Case lmk%
    Case 0
        enabler% = enabler% Or 4
    Case Else
        If enabler% = 15 Then
            getv% = 1
            escjog% = 1
            enabler% = 0
            'ho spinto il tasto sinistro
        End If
    End Select
   
    Select Case rmk%
    Case 0
        enabler% = enabler% Or 8
    Case Else
        If enabler% = 15 Then
            '''mario''' getv% = 1
            '''mario''' escjog% = 1
            '''mario''' enabler% = 0
            Call ProbeUpDown
            'ho spinto il tasto destro
        End If
    End Select
    
    Select Case lk%
    Case 0
        'non sto spigendo
        clk% = 0
        enabler1% = enabler1% Or 1
        If spxx& < 0 Then
            spxx& = 0
            kk% = 1
        End If
    Case Else
        If enabler1% = 15 Then
            clk% = clk% + 1
            If lk% = 1 Then clk% = 1
            If clk% > ths% Then
                clk% = ths%
                spxx& = -SpeedMaxProbe
            Else
                spxx& = -(SpeedMaxProbe / ths%) * clk%
            End If
            'sto spingendo
            kk% = 1
            If ck% <> 0 Then
                acx% = 1
                spxx& = 2 * spxx&
            End If
        Else
            clk% = 0
        End If
    End Select
    
    Select Case rk%
    Case 0
        'non sto spigendo
        crk% = 0
        enabler1% = enabler1% Or 2
        If spxx& > 0 Then
            spxx& = 0
            kk% = 1
        End If
    Case Else
        If enabler1% = 15 Then
            'sto spingendo
            crk% = crk% + 1
            If rk% = 1 Then crk% = 1
            If crk% > ths% Then
                crk% = ths%
                spxx& = SpeedMaxProbe
            Else
                spxx& = (SpeedMaxProbe / ths%) * crk%
            End If
            kk% = 1
            If ck% <> 0 Then
                spxx& = 2 * spxx&
                acx% = 1
            End If
        Else
            crk% = 0
        End If
    End Select
    
    Select Case uk%
    Case 0
        'non sto spigendo
        cuk% = 0
        enabler1% = enabler1% Or 4
        If spyy& > 0 Then
            spyy& = 0
            kk% = 1
        End If
    Case Else
        If enabler1% = 15 Then
            'sto spingendo
            cuk% = cuk% + 1
            If uk% = 1 Then cuk% = 1
            If cuk% > ths% Then
                cuk% = ths%
                spyy& = SpeedMaxProbe
            Else
                spyy& = (SpeedMaxProbe / ths%) * cuk%
            End If
            kk% = 1
            If ck% <> 0 Then
                spyy& = 2 * spyy&
                acy% = 1
            End If
        Else
            cuk% = 0
        End If
    End Select

    Select Case dk%
    Case 0
        'non sto spigendo
        cdk% = 0
        enabler1% = enabler1% Or 8
        If spyy& < 0 Then
            spyy& = 0
            kk% = 1
        End If
    Case Else
        'sto spingendo
        If enabler1% = 15 Then
            cdk% = cdk% + 1
            If dk% = 1 Then cdk% = 1
            If cdk% > ths% Then
                cdk% = ths%
                spyy& = -SpeedMaxProbe
            Else
                spyy& = -(SpeedMaxProbe / ths%) * cdk%
            End If
            kk% = 1
            If ck% <> 0 Then
                spyy& = 2 * spyy&
                acy% = 1
            End If
        Else
            cdk% = 0
        End If
    End Select
    
    If kk% = 1 Then
        accx$ = accmin$
        If acx% = 1 Then accx$ = AccMaxProbe
            accy$ = accmin$
            If acy% = 1 Then accy$ = AccMaxProbe
            spx& = spxx&
            spy& = spyy&
        End If
        If spx& > 8000 Then spx& = 8000
        If spx& < -8000 Then spx& = -8000
        If spy& > 8000 Then spy& = 8000
        If spy& < -8000 Then spy& = -8000
        
        If odt& <> 0 And (spx& <> 0 Or spy& <> 0) Then
            Call ProbeUp(checkok%)
            If checkok% = 0 Then spx& = 0: spy& = 0
            Select Case InJogCheProbe
                Case 1
                    tcmd$ = "JG " + Str$(spx&) + "," + Str$(spy&) + ";" + "BG XY"
                Case 2
                    tcmd$ = "JG,, " + "+" + Str$(-spx&) + ",+" + Str$(-spy&) + ";" + "BG ZW"
            End Select
        Else
            Select Case InJogCheProbe
                Case 1
                    tcmd$ = "ST XY;SP" + Str(SpeedMaxProbe / 10) + "," + Str(SpeedMaxProbe / 10)
                Case 2
                    tcmd$ = "ST ZW;SP,," + Str(SpeedMaxProbe / 10) + "," + Str(SpeedMaxProbe / 10)
            End Select
        End If
        If escjog% = 1 Then
            Select Case InJogCheProbe
                Case 1
                    tcmd$ = "ST XY;SP" + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe) + ";AC " + accx$ + "," + accy$
                Case 2
                    tcmd$ = "ST ZW;SP,," + Str(SpeedMaxProbe) + "," + Str(SpeedMaxProbe) + ";AC,," + accx$ + "," + accy$
            End Select
        End If
        Select Case InJogCheProbe
            Case 1
                tcmd$ = tcmd$ + ";AC" + accx$ + "," + accy$
            Case 2
                tcmd$ = tcmd$ + ";AC,," + accx$ + "," + accy$
        End Select
        If ocmd$ <> tcmd$ Then
            'Debug.Print tcmd$
            Call GalilCmdProbe(tcmd$, res$, ermsg$)
        End If
        ocmd$ = tcmd$
        a& = SetCursorPos(thexx&, theyy&)
        odt& = dt&

        If escjog% = 1 Then
            If getv% = 1 Then
                Select Case InJogCheProbe
                    Case 1
                        Call GalilCmdProbe("TD XY", res$, ermsg$)
                    Case 2
                        Call GalilCmdProbe("TD ZW", res$, ermsg$)
                End Select
                cm% = InStr(res$, ",")
                If cm% > 0 Then
                    thex& = Val(Trim$(Left$(res$, cm% - 1)))
                    they& = Val(Trim$(Mid$(res$, cm% + 1)))
                End If
            End If
            Do
                esk% = GetAsyncKeyState%(27) 'escape key
                enk% = GetAsyncKeyState%(13) 'enter key
                lmk% = GetAsyncKeyState%(&H1) 'left mouse button
                rmk% = GetAsyncKeyState%(&H2) 'right mouse button
                tk% = esk% + enk% + lmk% + rmk%
                DoEvents
            Loop While tk% <> 0
            Call offjog
            enabler% = 0
            enabler1% = 0
            ocmd$ = ""
            Select Case InJogCheProbe
                Case 1
                    statjog% = 60
                Case 2
                    statjog% = 61
            End Select
            
            Static GapPuntaliX As Long
            Static GapPuntaliY As Long
            Select Case statjog%
                Case 60
                    If getv% = 1 Then
                        'Posizione Probe A
                        GapPuntaliX = thex& - Val(Frmedit.TxtRefXProbeA.Tag)
                        GapPuntaliY = they& - Val(Frmedit.TxtRefYProbeA.Tag)
                        PositionProbeAX = GapPuntaliX
                        Frmedit.txtProbeAX.Tag = Trim(Str(PositionProbeAX))
                        Frmedit.txtProbeAX.Text = formatter$(step2mm#(PositionProbeAX, stept!), 3, 2)
                        PositionProbeAY = GapPuntaliY
                        Frmedit.txtProbeAY.Tag = Trim(Str(PositionProbeAY))
                        Frmedit.txtProbeAY.Text = formatter$(step2mm#(PositionProbeAY, stept!), 3, 2)
                    End If
                    statjog% = 0
                    Frmedit.ZOrder 0
                    Frmedit.Visible = True

                Case 61
                    If getv% = 1 Then
                        'Posizione Probe B
                        GapPuntaliX = Val(Frmedit.TxtRefXProbeB.Tag) - thex&
                        GapPuntaliY = Val(Frmedit.TxtRefYProbeB.Tag) - they&
                        PositionProbeBX = GapPuntaliX
                        Frmedit.txtProbeBX.Tag = Trim(Str(PositionProbeBX))
                        Frmedit.txtProbeBX.Text = formatter$(step2mm#(PositionProbeBX, stept!), 3, 2)
                        PositionProbeBY = GapPuntaliY
                        Frmedit.txtProbeBY.Tag = Trim(Str(PositionProbeBY))
                        Frmedit.txtProbeBY.Text = formatter$(step2mm#(PositionProbeBY, stept!), 3, 2)
                    End If
                    statjog% = 0
                    Frmedit.ZOrder 0
                    Frmedit.Visible = True

                Case 62
                    If getv% = 1 Then
                        'Reference Probe A
                        MasterRefXProbeA = thex&
                        Frmedit.TxtRefXProbeA.Tag = Trim(Str(MasterRefXProbeA))
                        Frmedit.TxtRefXProbeA.Text = formatter$(step2mm#(thex&, stept!), 3, 2)
                        MasterRefYProbeA = they&
                        OldRefAX = MasterRefXProbeA
                        
                        Frmedit.TxtRefYProbeA.Tag = Trim(Str(MasterRefYProbeA))
                        Frmedit.TxtRefYProbeA.Text = formatter$(step2mm#(they&, stept!), 3, 2)
                        If (MasterRefXProbeA <> 0) And (MasterRefYProbeA <> 0) And (MasterRefXProbeB <> 0) And (MasterRefYProbeB <> 0) Then
                            OffSetPuntaliX = MasterRefXProbeB + MasterRefXProbeA
                            OffSetPuntaliY = MasterRefYProbeB + MasterRefYProbeA
                            'If OffSetPuntaliX < 15000 Then OffSetPuntaliX = 15000
                            'If OffSetPuntaliY < 15000 Then OffSetPuntaliY = 15000
                        Else
                            OffSetPuntaliX = 15000
                            OffSetPuntaliY = 15000
                        End If
                        OldRefAY = MasterRefYProbeA
                    End If
                    statjog% = 0
                    Frmedit.ZOrder 0
                    Frmedit.Visible = True
                    
                Case 63
                    If getv% = 1 Then
                        'Reference Probe B
                        MasterRefXProbeB = thex&
                        Frmedit.TxtRefXProbeB.Tag = Trim(Str(MasterRefXProbeB))
                        Frmedit.TxtRefXProbeB.Text = formatter$(step2mm#(thex&, stept!), 3, 2)
                        MasterRefYProbeB = they&
                        OldRefBX = MasterRefXProbeB
                        
                        Frmedit.TxtRefYProbeB.Tag = Trim(Str(MasterRefYProbeB))
                        Frmedit.TxtRefYProbeB.Text = formatter$(step2mm#(they&, stept!), 3, 2)
                        If (MasterRefXProbeA <> 0) And (MasterRefYProbeA <> 0) And (MasterRefXProbeB <> 0) And (MasterRefYProbeB <> 0) Then
                            OffSetPuntaliX = MasterRefXProbeB + MasterRefXProbeA
                            OffSetPuntaliY = MasterRefYProbeB + MasterRefYProbeA
                            'If OffSetPuntaliX < 14000 Then OffSetPuntaliX = 15000
                            'If OffSetPuntaliY < 14000 Then OffSetPuntaliY = 15000
                        Else
                            OffSetPuntaliX = 15000
                            OffSetPuntaliY = 15000
                        End If
                        OldRefBY = MasterRefYProbeB
                    End If
                    statjog% = 0
                    Frmedit.ZOrder 0
                    Frmedit.Visible = True
                    
            End Select
         
        End If
End Sub

Sub UpdatePosXYProbe()
    Select Case InJogCheProbe
        Case 1
            Call GetPosXYProbe(xxx$, yyy$)
        Case 2
            Call GetPosZWProbe(xxx$, yyy$)
    End Select
    With FrmEuro
        .lblposx.Visible = True
        .Lblposy.Visible = True
        If dispstep% = 0 Then
            .lblposx = "X:" + formatter$(step2mm#(Val(xxx$), stept!), 3, 2)
            .Lblposy = "Y:" + formatter$(step2mm#(Val(yyy$), stept!), 3, 2)
        Else
            .lblposx = Trim$(Str(Val(xxx$)))
            .Lblposy = Trim$(Str(Val(yyy$)))
        End If
        If .Picpos.Visible = False Then
            .Picpos.Visible = True
            .Picpos.ZOrder 0
        End If
    End With
End Sub

Sub GetPosXYProbe(xxx$, yyy$)
   Select Case mctype%
    Case 1
        Call GalilCmdProbe("TD XY", res$, ermsg$)
        cl% = InStr(res$, ",")
        xxx$ = ""
        yyy$ = ""
        If cl% > 0 Then
            xxx$ = Trim$(Left$(res$, cl% - 1))
            yyy$ = Trim$(Mid$(res$, cl% + 1))
        End If
    End Select
End Sub

Sub GetPosZWProbe(xxx$, yyy$)
   Select Case mctype%
    Case 1
        Call GalilCmdProbe("TD ZW", res$, ermsg$)
        cl% = InStr(res$, ",")
        xxx$ = ""
        yyy$ = ""
        If cl% > 0 Then
            xxx$ = Trim$(Left$(res$, cl% - 1))
            yyy$ = Trim$(Mid$(res$, cl% + 1))
        End If
    End Select
End Sub

Public Sub TestMovimenti()
    Static IIIIII As Integer
    
    For IIIIII = 0 To 10
        Call Delay(1000)
        Call gotoposxy("0", "0")
        Do While Not moving% = 0
            DoEvents
        Loop
        Call Delay(1000)
        Call gotoposxy("100000", "100000")
    Next IIIIII
    
    For IIIIII = 0 To 10
        Call Delay(1000)
        Call GotoPosXYProbe("0", "0")
        Do While Not MovingProbe = 0
            DoEvents
        Loop
        Call Delay(1000)
        Call GotoPosXYProbe("140000", "0")
        
        Do While Not MovingProbe = 0
            DoEvents
        Loop
        Call Delay(1000)
        Call GotoPosXYProbe("140000", "140000")
        
        Do While Not MovingProbe = 0
            DoEvents
        Loop
        Call Delay(1000)
        Call GotoPosXYProbe("0", "140000")
        Do While Not MovingProbe = 0
            DoEvents
        Loop
    
        Call Delay(1000)
        Call GotoPosZWProbe("0", "0")
        Do While Not MovingProbe = 0
            DoEvents
        Loop
        Call Delay(1000)
        Call GotoPosZWProbe("140000", "0")
        
        Do While Not MovingProbe = 0
            DoEvents
        Loop
        Call Delay(1000)
        Call GotoPosZWProbe("140000", "140000")
        
        Do While Not MovingProbe = 0
            DoEvents
        Loop
        Call Delay(1000)
        Call GotoPosZWProbe("0", "140000")
    
    
    
    
    Next IIIIII
End Sub

Public Sub AddLogProbe(LogString As String)
    FrmEuro.txtLogProbe.SelStart = 65535
    FrmEuro.txtLogProbe.SelText = LogString + TermLinea
    FrmEuro.txtLogProbe.SelStart = 65535
End Sub
