Attribute VB_Name = "Mod2700"
Public initVolt As Boolean
Public initOhm As Boolean

Sub init2700(autozero%, thebeep%, disp%)
   'settaggio autozero
   If autozero% = 0 Then
        Call send(a27%, ":SYST:AZERO:STAT OFF", stat%)     ' Autozero OFF
   Else
        Call send(a27%, ":SYST:AZERO:STAT ON", stat%)     ' Autozero on
   End If
    'settaggio suono beep
    If thebeep% = 0 Then
        Call send(a27%, ":SYST:BEEP:STAT 0", stat%) 'Beep spento
   Else
        Call send(a27%, ":SYST:BEEP:STAT 1", stat%) 'Beep attivo
   End If
   'disabilito Display se richiesto
    If disp% = 0 Then
        Call send(a27%, ":DISP:ENAB OFF", stat%) 'display spento
    Else
        Call send(a27%, ":DISP:ENAB ON", stat%) 'display attivo
    End If
End Sub

Sub init2700Ohm()
If initOhm = True Then Exit Sub
initOhm = True
If a27% = 0 Then a27% = 16
Call send(a27%, "*RST", stat%)
Call send(a27%, ":ABORT", stat%)
Call send(a27%, ":CONF:RES", stat%)

Call send(a27%, ":SENS:VOLT:DIG 6", stat%)
Call send(a27%, ":SENS:FUNC 'RES'", stat%) ' Sense Voltage
Call send(a27%, ":SENS:RES:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
Call send(a27%, ":SENS:RES:RANGE:AUTO 1", stat%)

Call send(a27%, ":SENS:VOLT:AVER:STAT OFF", stat%)

Call send(a27%, ":CALC3:OUTP:LSEN ALOW", stat%) 'output active low
Call send(a27%, ":INIT:CONT ON", stat%)

End Sub

Sub init2700Volt()


If initVolt = True Then Exit Sub
initVolt = True
Call send(a27%, "*RST", stat%)
Call send(a27%, ":ABORT", stat%)
Call send(a27%, ":CONF:" + funcname$, stat%)

Call send(a27%, ":DISP:DIG 6", stat%)
Call send(a27%, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
Call send(a27%, ":SENS:VOLT:DC:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
Call send(a27%, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)

Call send(a27%, ":SENS:VOLT:AVER:STAT OFF", stat%)

Call send(a27%, ":INIT:CONT ON", stat%)

End Sub


Sub rvc2700(filter%, therange!, dig!)
'leggi tensione di continuo
'filtro impostabile
'range impostabile
If a27% = 0 Then a27% = 16
Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%)
Call send(a27%, ":CONF:VOLT", stat%)
'If therange! = 0 Then
'    Call send(a27%, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
'Else
'    Call send(a27%, ":SENS:VOLT:DC:RANGE:AUTO 0", stat%)
'    Call send(a27%, ":SENS:VOLT:DC:RANGE " + Trim$(Str(therange!)), stat%)
'End If
'If filter% = 1 Then
'    Call send(a27%, ":SENS:VOLT:AVER:TCON REP", stat%)
'    Call send(a27%, ":SENS:VOLT:AVER:COUN 10", stat%)
'    Call send(a27%, ":SENS:VOLT:AVER:STAT ON", stat%)
'Else
'    Call send(a27%, ":SENS:VOLT:AVER:STAT OFF", stat%)
'End If
'If dig! = 0 Then dig! = 5
'Call send(a27%, ":SENS:VOLT:DIG " + Trim$(Str(dig!)), stat%)
Call send(a27%, ":INIT:CONT ON", stat%)
End Sub
Sub rrc2700(fourwires%, filter%, therange!, dig!)
'leggi resistenza di continuo
'filtro impostabile
'range impostabile
'risoluzione impostabile
funcname$ = "RES"
If a27% = 0 Then a27% = 16
If fourwires% = 1 Then funcname$ = "FRES"
'Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%)
'Call send(a27%, ":CONF:" + funcname$, stat%)
'If therange! = 0 Then
'    Call send(a27%, ":SENS:" + funcname$ + ":RANGE:AUTO 1", stat%)
'Else
'    Call send(a27%, ":SENS:" + funcname$ + ":RANGE:AUTO 0", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":RANGE " + Trim$(Str(therange!)), stat%)
'End If
'If filter% = 1 Then
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:TCON REP", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:COUN 10", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:STAT ON", stat%)
'Else
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:STAT OFF", stat%)
'End If
'If fourwires% = 1 Then
'    Call send(a27%, ":SENS:" + funcname$ + ":OCOM ON", stat%)
'End If
'If dig! = 0 Then dig! = 5
'Call send(a27%, ":SENS:" + funcname$ + ":DIG " + Trim$(Str(dig!)), stat%)
Call send(a27%, ":INIT:CONT ON", stat%)
End Sub
Sub setcutd2700(vthreshold!, filter%, therange!, dig!)
'imposta un taglio digitale (scatto a una certa tensione impostabile)
'filtro impostabile
'range impostabile
'risoluzione impostabile
Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%) ' Sense Voltage
Call send(a27%, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
Call send(a27%, ":SENS:VOLT:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
Call send(a27%, ":CALC3:LIM2:UPP " + Trim$(Str$(vthreshold!)), stat%)
Call send(a27%, ":CALC3:LIM2:LOW -100", stat%)
If therange! = 0 Then
    Call send(a27%, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
Else
    Call send(a27%, ":SENS:VOLT:DC:RANGE:AUTO 0", stat%)
    Call send(a27%, ":SENS:VOLT:DC:RANGE " + Trim$(Str(therange!)), stat%)
End If
If filter% = 1 Then
    Call send(a27%, ":SENS:VOLT:AVER:TCON REP", stat%)
    Call send(a27%, ":SENS:VOLT:AVER:COUN 10", stat%)
    Call send(a27%, ":SENS:VOLT:AVER:STAT ON", stat%)
Else
    Call send(a27%, ":SENS:VOLT:AVER:STAT OFF", stat%)
End If
If dig! = 0 Then dig! = 5
Call send(a27%, ":SENS:VOLT:DIG " + Trim$(Str(dig!)), stat%)
Call send(a27%, ":TRIG:COUNT 1", stat%)
Call send(a27%, ":TRIG:SOURCE IMM", stat%)
Call send(a27%, ":TRIG:DELAY 0", stat%)
Call send(a27%, ":STAT:QUE:CLEAR", stat%)    '
Call send(a27%, ":STAT:PRES", stat%)    ' Status preset
Call send(a27%, "*SRE 1", stat%)    ' SRQ
Call send(a27%, "*CLS", stat%)       ' reset Status Byte Register  per SRQ
Call send(a27%, ":CALC3:LIM2:STAT 1", stat%)
Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
Call send(a27%, ":CALC3:OUTP:LSEN ALOW", stat%) 'output active low
Call send(a27%, ":CALC3:OUTP:PULS:TIME 0.1", stat%)
Call send(a27%, ":CALC3:OUTP:PULS:STAT ON", stat%)
Call send(a27%, ":CALC3:OUTP:STAT ON", stat%) 'enable outputs
Call send(a27%, ":STAT:MEAS:ENAB 16", stat%)    ' Abilita Limit 2  UP fail
Call send(a27%, ":INIT:CONT ON", stat%)
End Sub
Sub reset2700()
   Call send(a27%, "*RST", stat%)                ' reset DVM
End Sub
Sub ri2700(theval!, filter%, therange!, dig!)
'legge una corrente a single shot
'filtro impostabile
'range impostabile
'risoluzione impostabile
Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%)
Call send(a27%, ":FORM:ELEM READ", stat%)
Call send(a27%, ":SENS:FUNC 'CURR:DC'", stat%) ' Sense Voltage
Call send(a27%, ":SENS:CURR:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
If therange! = 0 Then
    Call send(a27%, ":SENS:CURR:DC:RANGE:AUTO 1", stat%)
Else
    Call send(a27%, ":SENS:CURR:DC:RANGE:AUTO 0", stat%)
    Call send(a27%, ":SENS:CURR:DC:RANGE " + Trim$(Str(therange!)), stat%)
End If
If filter% = 1 Then
    Call send(a27%, ":SENS:CURR:AVER:TCON REP", stat%)
    Call send(a27%, ":SENS:CURR:AVER:COUN 10", stat%)
    Call send(a27%, ":SENS:CURR:AVER:STAT ON", stat%)
Else
    Call send(a27%, ":SENS:CURR:AVER:STAT OFF", stat%)
End If
If dig! = 0 Then dig! = 5
Call send(a27%, ":SENS:CURR:DIG " + Trim$(Str(dig!)), stat%)
Call send(a27%, ":TRIG:COUNT 1", stat%)
Call send(a27%, ":TRIG:SOURCE IMM", stat%)
Call send(a27%, ":TRIG:DELAY 0", stat%)
Call send(a27%, ":SAMP:COUNT 1", stat%)
Call send(a27%, ":READ?", stat%)
rx$ = Space$(255)
Call enter(rx$, 255, Rl%, a27%, stat%)
rx$ = Trim$(rx$)
'For i% = Len(rx$) To 1 Step -1
'    If Mid$(rx$, i%, 1) = "," Then
'        rx$ = Mid$(rx$, i% + 1)
'        Exit For
'    End If
'Next i%
theval! = Val(rx$)
'Call rvc2700(0, 0, 0)
End Sub

Sub rv2700(theval!, filter%, therange!, dig!)
'legge una tensione a single shot
'filtro impostabile
'range impostabile
'risoluzione impostabile
If a27% = 0 Then a27% = 16
Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%)
Call send(a27%, ":FORM:ELEM READ", stat%)
'Call send(a27%, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
'Call send(a27%, ":SENS:VOLT:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
'If therange! = 0 Then
    Call send(a27%, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
'Else
'    Call send(a27%, ":SENS:VOLT:DC:RANGE:AUTO 0", stat%)
'    Call send(a27%, ":SENS:VOLT:DC:RANGE " + Trim$(Str(therange!)), stat%)
'End If
'If filter% = 1 Then
'    Call send(a27%, ":SENS:VOLT:AVER:TCON REP", stat%)
'    Call send(a27%, ":SENS:VOLT:AVER:COUN 10", stat%)
'    Call send(a27%, ":SENS:VOLT:AVER:STAT ON", stat%)
'Else
'    Call send(a27%, ":SENS:VOLT:AVER:STAT OFF", stat%)
'End If
'If dig! = 0 Then dig! = 5
'Call send(a27%, ":SENS:VOLT:DIG " + Trim$(Str(dig!)), stat%)
'Call send(a27%, ":TRIG:COUNT 1", stat%)
'Call send(a27%, ":TRIG:SOURCE IMM", stat%)
'Call send(a27%, ":TRIG:DELAY 0", stat%)
'Call send(a27%, ":SAMP:COUNT 1", stat%)
Call send(a27%, ":READ?", stat%)
rx$ = Space$(255)
Call enter(rx$, 255, Rl%, a27%, stat%)
rx$ = Trim$(rx$)
'For i% = Len(rx$) To 1 Step -1
'    If Mid$(rx$, i%, 1) = "," Then
'        rx$ = Mid$(rx$, i% + 1)
'        Exit For
'    End If
'Next i%
theval! = Val(rx$)
Call rvc2700(0, 0, 0)
'Delay (400)
'strRisposta = MsgBox("V = " & Str(theval!))
End Sub
Sub rr2700(theval!, fourwires%, filter%, therange!, dig!)
'legge una resistenza con single shot
'filtro impostabile
'range impostabile
'modalita' 4 fili impostabile
'risoluzione impostabile
funcname$ = "RES"
If a27% = 0 Then a27% = 16
If fourwires% = 1 Then funcname$ = "FRES"
Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%)
Call send(a27%, ":FORM:ELEM READ", stat%)     ' legge solo la resistenza
'Call send(a27%, ":SENS:FUNC '" + funcname$ + "'", stat%)
'If fourwires% = 0 Then Call send(a27%, ":SENS:" + funcname$ + ":NPLC 1", stat%) ' Integration rate Influenza la velocita'
'If therange! = 0 Then
    Call send(a27%, ":SENS:" + funcname$ + ":RANGE:AUTO 1", stat%)
'Else
'    Call send(a27%, ":SENS:" + funcname$ + ":RANGE:AUTO 0", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":RANGE " + Trim$(Str(therange!)), stat%)
'End If
'If filter% = 1 Then
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:TCON REP", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:COUN 10", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:STAT ON", stat%)
'Else
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:STAT OFF", stat%)
'End If
'If fourwires% = 1 Then
'    Call send(a27%, ":SENS:" + funcname$ + ":OCOM ON", stat%)
'End If
'If dig! = 0 Then dig! = 5
'Call send(a27%, ":SENS:" + funcname$ + ":DIG " + Trim$(Str(dig!)), stat%)
'Call send(a27%, ":TRIG:COUNT 1", stat%)
'Call send(a27%, ":TRIG:SOURCE IMM", stat%)
'Call send(a27%, ":TRIG:DELAY 0", stat%)
'Call send(a27%, ":SAMP:COUNT 1", stat%)
Call send(a27%, ":READ?", stat%)
rx$ = Space$(255)
Call enter(rx$, 255, Rl%, a27%, stat%)
rx$ = Trim$(rx$)
'For i% = Len(rx$) To 1 Step -1
'    If Mid$(rx$, i%, 1) = "," Then
'        rx$ = Mid$(rx$, i% + 1)
'        Exit For
'    End If
'Next i%
theval! = Val(rx$)

Call rrc2700(0, 0, 0, 6)

End Sub

Sub oscan2700()
'apre tutti gli scanner del 2700
Call send(a27%, ":ROUT:OPEN:ALL", stat%)
End Sub

Sub oscans2700(scanlist$)
'apre gli scanner del 2700 selezionati in scanlist
'lista: @BCH  b= board CH=canale sintassi: (@BCH:BCH)
Call send(a27%, ":ROUT:MULT:OPEN " + scanlist$, stat%)
End Sub

Sub cscan2700(scanlist$)
'chiude gli scanner del 2700 selezionati
'lista: @BCH  b= board CH=canale sintassi: (@BCH,BCH)
'es @101,201 (slot 1 ch 1, slot 2 ch 1)
Call send(a27%, ":ROUT:MULT:CLOSE " + scanlist$, stat%)
End Sub
Sub CloseSingleChannel(strChannel As String)
'Questa funzione ha il compito di chiudere un solo canale alla volta

Call send(a27%, ":ROUT:CLOS " + strChannel, stat%)

End Sub
Sub testvolt2700(percentage!, perc1!, guard%, theresval!, level%, prt%, ok%)
ok% = 1
'Call readvolt(theval!)
Call rv2700(theval!, 0, 0, 0)
realper! = Int(1000 * (theresval! - theval!) / theresval!)
realper! = realper! / 10
If perc1! <> 0 Then
    If Abs(realper!) <= Abs(perc1!) Then
            If prt% = 1 Then
                mr$ = "Pretest OK:" + Str(Abs(realper!))
                Call updatereport(mr$, 0)
            End If
            perc1! = 1
            Exit Sub
    Else
       prt% = 0
    End If
End If
perc1! = 0
If level% = 1 Then
    'puo' stare entro valore - percentuale
    If realper! < 0 Then
        tooh% = 1
    End If
    If realper! > percentage! Then
        tool% = 1
    End If
End If
If level% = 2 Then
    'puo' stare entro valore + percentuale
    If realper! > 0 Then
        tool% = 1
    End If
    If realper! < -percentage! Then
        tooh% = 1
    End If
End If
If level% = 3 Then
    'puo' stare entro valore +/- percentuale
    If (realper! > percentage!) Then
        tool% = 1
    End If
    If (realper! < -percentage!) Then
        tooh% = 1
    End If
End If
If tooh% = 1 Then
    ok% = 0
End If
If tool% = 1 Then
    ok% = 0
End If
If prt% = 1 Then
    If ok% = 1 Then
        Select Case level%
        Case 1, 2
            mr$ = "Pretest OK:"
        Case 3
            mr$ = "Test OK:"
        End Select
    Else
        Select Case level%
        Case 1, 2
            mr$ = "Pretest Fail:"
        Case 3
            mr$ = "Test Fail:"
        End Select
    End If
    mr$ = mr$ + Str$(theval!) + " (" + Trim$(Str$(-realper!)) + "%)"
    nok% = ok% Xor 1
    Call updatereport(mr$, nok%)
    theresval! = theval!
Else
    If ok% = 0 Then
       Select Case level%
         Case 1, 2
             mr$ = "Pretest Fail:"
         Case 3
             mr$ = "Test Fail:"
       End Select
       mr$ = mr$ + Str$(theval!) + " (" + Trim$(Str$(-realper!)) + "%)"
       nok% = ok% Xor 1
       Call updatereport(mr$, nok%)
    End If
End If

End Sub
Sub executd2700(cutn%, thetable%, execute%, cuterr%, intProgrammazione As Integer, ByRef cuts$())
                            'attende che la tavola sia in posizione
                            Hasdone% = 0
                            flagwork% = 1
                            cuterr% = 0
                            'use /800 for view number in milimeters
                            cutrefx& = Val(TLasReferenceX$)
                            cutrefy& = Val(TLasReferenceY$)
                            'if start, it is actual laser position
                            cutrefx1& = Val(TLasReferenceX1$)
                            cutrefy1& = Val(TLasReferenceY1$)
                            thematrix$(4002) = "0"
                            thematrix$(4003) = "0"
                            
                            ek% = GetAsyncKeyState%(27) 'esc key
                            If Hasdone% = 2 Or workexit% > 0 Or ek% <> 0 Then
                                'interruzione operatore
                                cuterr% = 4
                                If ek% <> 0 Then
                                    pressesc% = 1
                                    GoTo cutfailed
                                End If
                            End If
                            
                            If execute% = 1 Then
                                Call showstatus("Executing Trim")
                            Else
                                Call showstatus("Testing Trim")
                            End If
                            cuterr% = 0
                            thematrix$(4001) = Trim$(Str(cuterr%))
                            'esegue o testa (execute%=0) taglio singolo digitale con strumento K 2700
                            'cuterr%:=0 nessun errore, =1 gia' scattato =2 non scattato=3 vario=4 interrotto dall'utente
                            
                            'cut's number
                            k% = cutn%
                            'check cut's exists and load cut
                            If k% <= UBound(TrimMatrix) Then
                                myrecord = TrimMatrix(k%)
                            Else
                                cuterr% = 3
                                GoTo cutfailed
                            End If
                            rnom2! = 0
                            rnom1! = 0
                            dcut% = 0
                            uncond% = 0
                            'only move values
                            cutType% = Val(cleanval(myrecord.cutType))
                            cutstep! = Val(cleanval(myrecord.serpstep))
                            funct% = Val(cleanval(myrecord.func))
                            thefunctional% = funct%
                            
                            'favourite only for triming etc.
                            If funct% = 0 Then
                                Call init2700Ohm
                            Else
                                Call init2700Volt
                            End If
                                                        
                            'Posizionamento Probe
                            Static TmpExecCAX As Long
                            Static TmpExecCAY As Long
                            Static TmpExecCBX As Long
                            Static TmpExecCBY As Long
                            
                            TmpExecCAX = Val(cleanval(myrecord.recProbeAX)) + MasterRefXProbeA
                            TmpExecCAY = Val(cleanval(myrecord.recProbeAY)) + MasterRefYProbeA
                            TmpExecCBX = MasterRefXProbeB - Val(cleanval(myrecord.recProbeBX))
                            TmpExecCBY = MasterRefYProbeB - Val(cleanval(myrecord.recProbeBY))
                           ' Delay (1000)
                            
                            'probe move
                            Select Case cutType%
                                Case 2, 3, 6, 7
                                    Call MoveProbe("0", "0", "0", "0", 1)
                                Case Else
                                    Call MoveProbe(Str(TmpExecCAX), Str(TmpExecCAY), Str(TmpExecCBX), Str(TmpExecCBY), 1)
                            End Select
                            'probe down for cut 2 and 3
                            Select Case cutType%
                            Case 2, 3
                            Case Else
                                Call ProbeDown
                            End Select
                            Delay (300)
                            
                            'numero di talgi multipli
                            intNCut = Val(cleanval(myrecord.strNCutMultiple))
                            If Val(cleanval(myrecord.slope)) = 2 Then
                                slope% = DSlope
                            Else
                                slope% = Val(cleanval(myrecord.slope))
                            End If
                            usevision% = Val(cleanval(myrecord.vision))
                            'any sets for some cuts, 7 is ignored
                            Select Case cutType%
                            Case 0, 1
                                '0 doppio
                                '1 L
                                If cutType% = 0 Then
                                    mr$ = "CUT:" + Str(k%) + " (double cut)"
                                Else
                                    mr$ = "CUT:" + Str(k%) + " (L cut)"
                                End If
                                Call updatereport(mr$, 0)
                                If Trim$(cleanval(myrecord.reslabel)) <> "" Then
                                    mr$ = "LABEL: " + cleanval(myrecord.reslabel)
                                    Call updatereport(mr$, 0)
                                End If
                                Call updatereport(mr$, 0)
                                dcut% = 1
                                
                                
                                If funct% = 0 Then
                                   'taglio resistivo
                                    If cleanval(myrecord.ohmvalue) = "PSI" Then
                                        rnom2! = DResistenza
                                    Else
                                        rnom2! = Val(cleanval(myrecord.ohmvalue))
                                        thematrix$(4004) = Trim(Str(rnom2!))
                                    End If
                                    ornom2! = rnom2!
                                    Select Case Val(cleanval(myrecord.cutPre))
                                    Case Is < 0, Is >= 100
                                        cuterr% = 3
                                        GoTo cutfailed
                                    Case Else
                                        rnom2! = rnom2! - (Val(cleanval(myrecord.cutPre)) * rnom2!) / 100
                                    End Select
                                    Select Case Val(cleanval(myrecord.cut1St))
                                    Case Is < 0, Is >= 100
                                        cuterr% = 3
                                        GoTo cutfailed
                                    End Select
                                    
                                    Call rr2700(theValOhm!, 0, 0, 0, 6)
                                    
                                    thematrix$(4003) = Trim(Str(theValOhm!))
                                    thematrix$(4002) = Trim(Str(theValOhm!))
                                    rnom1! = theValOhm! + ((rnom2! - theValOhm!) * Val(cleanval(myrecord.cut1St)) / 100)
                                    Select Case rnom1!
                                    Case 0, Is >= rnom2!
                                        cuterr% = 3
                                        GoTo cutfailed
                                    End Select
                            
                                Else
                                    'taglio funzionale
                                    If cleanval(myrecord.voltvalue) = "PSI" Then
                                        vnom2! = DVoltage
                                    Else
                                        vnom2! = Val(cleanval(myrecord.voltvalue))
                                    End If
                                    ovnom2! = vnom2!
                                    Select Case Val(cleanval(myrecord.cutPre))
                                    Case Is < 0, Is >= 100
                                        cuterr% = 3
                                        GoTo cutfailed
                                    Case Else
                                       If (slope% = 1) Then
                                           vnom2! = vnom2! - (Val(cleanval(myrecord.cutPre)) * vnom2!) / 100
                                        Else
                                           vnom2! = vnom2! + (Val(cleanval(myrecord.cutPre)) * vnom2!) / 100
                                        End If
                                    End Select
                                    Select Case Val(cleanval(myrecord.cut1St))
                                    Case Is < 0, Is >= 100
                                        cuterr% = 3
                                        GoTo cutfailed
                                    End Select
                                    
                                    Call rv2700(theval!, 0, 0, 6)
                                    
                                    vnom1! = theval! + ((vnom2! - theval!) * Val(cleanval(myrecord.cut1St)) / 100)
                                    Select Case vnom1!
                                   ' Case 0, Is >= vnom2!
                                   Case 0
                                        cuterr% = 3
                                        GoTo cutfailed
                                    End Select
                                End If
                            Case 2, 3
                                'Marcatori
                                funct% = 0
                                thefunctional% = 0
                                rnom1! = 100
                                rnom2! = 0
                                vnom1! = 100
                                vnom2! = 0
                                cutk$ = k%
                                Select Case Val(cleanval(myrecord.mark))
                                Case 1
                                    thekg% = Val(cutk$)
                                Case 2
                                    thekb% = Val(cutk$)
                                End Select
                                'GoTo markfunc
endmark:                        'cuterr% = 3
                                'GoTo cutfailed
                            Case 4, 5, 8
                            
                                'serp o singolo
                                If cutType% = 4 Then
                                    mr$ = "CUT:" + Str(k%) + " (serp. cut)"
                                ElseIf cutType% = 5 Then
                                    mr$ = "CUT:" + Str(k%) + " (single cut)"
                                Else
                                    mr$ = "CUT:" + Str(k%) + " (Multiple cut)"
                                End If
                                Call updatereport(mr$, 0)
                                If Trim$(cleanval(myrecord.reslabel)) <> "" Then
                                    mr$ = "LABEL: " + cleanval(myrecord.reslabel)
                                    Call updatereport(mr$, 0)
                                End If
                                Call updatereport(mr$, 0)
                                dcut% = 0
                                If funct% = 0 Then
                                    'Taglio Resistivo
                                    rnom2! = 0
                                    If cleanval(myrecord.ohmvalue) = "PSI" Then
                                        rnom1! = DResistenza
                                    Else
                                        rnom1! = Val(cleanval(myrecord.ohmvalue))
                                        thematrix$(4004) = Trim(Str(rnom1!))
                                    End If
                                    ornom1! = rnom1!
                                    'Call rr2700(theValOhm!, 0, 0, 0, 6)
                                    'thematrix$(4003) = Trim(Str(theValOhm!))
                                    'thematrix$(4002) = Trim(Str(theValOhm!))
                                    Select Case Val(cleanval(myrecord.cutPre))
                                    Case Is < 0, Is >= 100
                                        cuterr% = 3
                                        GoTo cutfailed
                                    Case Else
                                           rnom1! = rnom1! - (Val(cleanval(myrecord.cutPre)) * rnom1!) / 100
                                    End Select
                                                                                          
                                Else
                                    'funzionale
                                    vnom2! = 0
                                    If cleanval(myrecord.voltvalue) = "PSI" Then
                                        vnom1! = DVoltage
                                    Else
                                        vnom1! = Val(cleanval(myrecord.voltvalue))
                                    End If
                                    ovnom1! = vnom1!
                                    Select Case Val(cleanval(myrecord.cutPre))
                                    Case Is < 0, Is >= 100
                                        cuterr% = 3
                                        GoTo cutfailed
                                    Case Else
                                        If (slope% = 1) Then
                                           vnom1! = vnom1! - (Val(cleanval(myrecord.cutPre)) * vnom1!) / 100
                                        Else
                                           vnom1! = vnom1! + (Val(cleanval(myrecord.cutPre)) * vnom1!) / 100
                                        End If
                                    End Select
                                End If
                            Case 6
                                'taglio incondizionato
                                uncond% = 1
                            End Select
                            'If testing% = 1 And uncond% = 1 Then GoTo startcut
                            If funct% = 0 Then
                               If (cutType% <> 2) And (cutType% <> 3) And (cutType% <> 6) And (cutType% <> 7) Then
                                  If rnom1! = 0 And rnom2! = 0 Then
                                  'se non ho fissato rnom1 e rnom2 segnalo un errore
                                  'dato che non posso tarare
                                     cuterr% = 3
                                     GoTo cutfailed
                                  End If
                               End If
                            Else
                                If vnom1! = 0 And vnom2! = 0 And uncond% = 0 Then
                                    cuterr% = 3
                                    GoTo cutfailed
                                End If
                            End If
                            'read start end stop positions
                            gapx& = Val(cleanval(myrecord.startx))
                            gapy& = Val(cleanval(myrecord.starty))
                            'posizione di start ultimo taglio multiplo
                            gapx2& = Val(cleanval(myrecord.start2X))
                            gapy2& = Val(cleanval(myrecord.start2Y))
                            deltax& = Val(cleanval(myrecord.stopx)) - Val(cleanval(myrecord.startx))
                            deltay& = Val(cleanval(myrecord.stopy)) - Val(cleanval(myrecord.starty))
                            'delta relativo all'ultimo taglio multiplo
                            deltaMultipleX2& = Val(cleanval(myrecord.stop2X)) - Val(cleanval(myrecord.start2X))
                            deltaMultipleY2& = Val(cleanval(myrecord.stop2Y)) - Val(cleanval(myrecord.start2Y))
                            'delta step totale fra il primo taglio e l'ultimo
                            deltaStepTotX& = Val(cleanval(myrecord.start2X)) - Val(cleanval(myrecord.startx))
                            deltaStepTotY& = Val(cleanval(myrecord.start2Y)) - Val(cleanval(myrecord.starty))
     
                            If cutType% = 4 Then deltaz& = mm2step#(Val(cleanval(myrecord.serpstep)), stepxy!)
                            cutspeed& = mm2step#(Val(cleanval(myrecord.speed)), stepxy!)
                            
                            'first favourite for repeat cutting
                            
                            'test if repeats supported
                            If ((UBound(cuts$) - LBound(cuts$) + 1) = 1) Then
                            
                                'qui inserisco la parte di codice per gestire la marcatura quando
                                'si va selezionare il taglio di tipo 2 o 3; vado a salvare i parametri
                                'da assare alla Galil per eseguire correttamente i tagli di marcatura
                                'data for cutting
                                Select Case cutType%
                                Case "2"
                                    'marker circle
                                    rmc& = Sqr((deltax& * deltax&) + (deltay& * deltay&))
                                Case "3"
                                    'marker square
                                    deltax1& = deltax&
                                    deltay1& = 0
                                    deltax2& = 0
                                    deltay2& = deltay&
                                End Select
                                'fine parte relativa alla marcatura
                                'data for cutting
                                If (cutType% <> 2) And (cutType% <> 3) Then
                            
                                    If dcut% = 1 Then
                                        Select Case UCase$(cleanval(myrecord.cutDir))
                                        Case "0"
                                            deltax1& = deltax&
                                            deltay1& = 0
                                            deltax2& = 0
                                            deltay2& = deltay&
                                        Case Else
                                            deltax1& = 0
                                            deltay1& = deltay&
                                            deltax2& = deltax&
                                            deltay2& = 0
                                        End Select
                                    Else
                                        thecutdir$ = UCase$(cleanval(myrecord.cutDir))
                                        If cutType% <> 4 Then
                                            thecutdir$ = "0"
                                            If Abs(deltay&) > Abs(deltax&) Then thecutdir$ = "1"
                                        End If
                                        'favourite cut number 7
                                        If (cutType% = 7) Then
                                            deltax1& = deltax&
                                            deltay1& = deltay&
                                            deltax2& = 0
                                            deltay2& = 0
                                        Else
                                            Select Case thecutdir$
                                            Case "0"
                                                deltax1& = deltax&
                                                deltay1& = 0
                                                deltax2& = 0
                                                deltay2& = 0
                                            Case Else
                                                deltax1& = 0
                                                deltay1& = deltay&
                                                deltax2& = 0
                                                deltay2& = 0
                                            End Select
                                        End If
                                    End If
                                End If
                                If cutType% = 4 Then
                                    'serp
                                    Select Case UCase$(cleanval(myrecord.cutDir))
                                    Case "0"
                                        'serpx
                                        deltax1& = deltax&
                                        deltay1& = 0
                                        deltax2& = 0
                                        deltay2& = deltay&
                                        deltax3& = deltaz&
                                        deltay3& = 0
                                    Case Else
                                        'serpy
                                        deltax1& = 0
                                        deltay1& = deltay&
                                        deltax2& = deltax&
                                        deltay2& = 0
                                        deltax3& = 0
                                        deltay3& = deltaz&
                                    End Select
                                End If
                            
                                'taglio multiplo
                                If cutType% = 9 Then
                                    Select Case UCase$(cleanval(myrecord.cutDir))
                                    Case "0"
                                        'X
                                        deltax1& = deltax&
                                        deltay1& = 0
                                        deltax2& = 0
                                        deltay2& = deltaStepTotY&
                                        deltax3& = deltaMultipleX2&
                                        deltay3& = 0
                                    Case Else
                                        'Y
                                        deltax1& = 0
                                        deltay1& = deltay&
                                        deltax2& = deltaStepTotX&
                                        deltay2& = 0
                                        deltax3& = 0
                                        deltay3& = deltaMultipleY2&
                                    End Select
                                End If
                                   
                                'posizionamento su step e inizio taglio
                                guard% = 0
                                'thetable use for count laser move
                                If thetable% = 0 Then
                                    ccutrefx& = cutrefx& + gapx&
                                    ccutrefy& = cutrefy& + gapy&
                                    ccutrefz& = cutrefz& + gapZ&
                                    ccutrefw& = cutrefw& + gapW&
                                Else
                                    ccutrefx& = cutrefx1& + gapx&
                                    ccutrefy& = cutrefy1& + gapy&
                                    ccutrefz& = cutrefz& + gapZ&
                                    ccutrefw& = cutrefw& + gapW&
                                End If
                                Hasdone% = 0
                                                                                  
                                'set laser move
                                Call gotoposxyzw(Trim(Str(ccutrefx&)), Trim(Str(ccutrefy&)), Trim(Str(ccutrefz&)), Trim(Str(ccutrefw&)))
                                Call setoutput(0, d0.O_Rele_Work_Standby)
                                thefreq! = Val(cleanval(myrecord.freq)) * 1000
                                lastfreq$ = Trim$(Str(thefreq!))
                                'set frequency
                                Call setfreqda(1, thefreq!)
                                thecurr! = Val(cleanval(myrecord.current))
                                lastcurr$ = Trim$(Str(thecurr!))
                                'set laser lamp current and check max value
                                Call setda(1, thecurr!, ilasmax!)
                                'Delay (500)
                                'controllo puntali
                                Call readdevice(0, 0, 5)
                                'view lamp information
                                If FrmEuro.Imglampon.Visible = True Then
                                    If FrmEuro.Lblfreq.Caption <> lastfreq$ Then FrmEuro.Lblfreq.Caption = lastfreq$
                                    If FrmEuro.Lblcurr.Caption <> lastcurr$ Then FrmEuro.Lblcurr.Caption = lastcurr$
                                Else
                                    If FrmEuro.Lblfreq.Caption <> "0" Then FrmEuro.Lblfreq.Caption = "0"
                                    If FrmEuro.Lblcurr.Caption <> "0" Then FrmEuro.Lblcurr.Caption = "0"
                                End If
                                DoEvents
                                'wait for probe
                                Do While Not MovingProbe% = 0
                                    DoEvents
                                Loop
                                If cutType% = 6 Or cutType% = 7 Or cutType% = 2 Or cutType% = 3 Then
                                    Call ProbeUp
                                Else
                                    Call ProbeDown
                                End If
                            
                                percentage! = Val(cleanval(myrecord.tolaccept))
                                tcmd$ = "DX1=" + Str(deltax1&)
                                Debug.Print tcmd$
                                Call galilcmd(tcmd$, res$, ermsg$)
                                tcmd$ = "DY1=" + Str(deltay1&)
                                Debug.Print tcmd$
                                Call galilcmd(tcmd$, res$, ermsg$)
                                tcmd$ = "DX2=" + Str(deltax2&)
                                Debug.Print tcmd$
                                Call galilcmd(tcmd$, res$, ermsg$)
                                tcmd$ = "DY2=" + Str(deltay2&)
                                Debug.Print tcmd$
                                Call galilcmd(tcmd$, res$, ermsg$)
                                If cutType% = 4 Then
                                    'serp
                                    tcmd$ = "DX3=" + Str(deltax3&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "DY3=" + Str(deltay3&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                ElseIf cutType% = 8 Then
                                    'gestione taglio multiplo
                                    tcmd$ = "START2X=" + Str(cutrefx& + gapx2&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "START2Y=" + Str(cutrefy& + gapy2&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "DX3=" + Str(deltax3&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "DY3=" + Str(deltay3&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "NCUT=" + Str(intNCut)
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                End If
                                tcmd$ = "VSC=" + Str(cutspeed&)
                                Debug.Print tcmd$
                                Call galilcmd(tcmd$, res$, ermsg$)
                            
                                'invio i parametri necessari alla Galil per il marker circolare
                                If cutType% = 2 Then
                                    tcmd$ = "MRAGGIO=" + Trim$(Str(rmc&))
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                End If
                            
                            
                                theiset! = 0
                                donetest% = 0
                                Hasdone% = 0
                                Do
                                    DoEvents
                                    If Hasdone% = 2 Or workexit% > 0 Then
                                        cuterr% = 4
                                        GoTo cutfailed
                                    End If
                                    If donetest% = 0 Then
                                        donetest% = 1
                                        If funct% = 1 And ((cutType% <> 2 And cutType% <> 3)) Then
                                            'taglio funzionale
                                
                                            thevoltval! = ovnom1!
                                            'pretest volt
                                            If vnom2! > 0 Then thevoltval! = ovnom2!
                                            If uncond% = 0 Then
                                                perc1! = Val(cleanval(myrecord.tolcut))
                                                'perc1! = 0
                                                If intProgrammazione = 1 Then
                                                    Call rv2700(theval!, 0, 0, 6)
                                                    If slope% = 1 Then
                                                        Call testvolt2700(percentage!, perc1!, guard%, thevoltval!, 1, 1, ok%)
                                                        If theval! >= thevoltval! Then perc1! = 1
                                                    Else
                                                        Call testvolt2700(percentage!, perc1!, guard%, thevoltval!, 2, 1, ok%)
                                                        If theval! <= thevoltval! Then perc1! = 1
                                                    End If
                                        
                                                    'se la resistenza e' gia' in tolleranza finale setta perc1! a 1 e non tara
                                                    If perc1! = 1 Then
                                                        'taglio gia' scattato
                                                        cuterr% = 1
                                                        GoTo cutfailed
                                                    Else
                                                        ok% = 1
                                                    End If
                                        
                                                    If execute% = 0 Then
                                                        'setta comunque lo strumento
                                                        Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
                                                        Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
                                                        'Call setcut2700(1, slope%, 0, 0, 0, 0, dcut%, thevoltval!, 0)
                                                        Exit Sub
                                                        'End If
                                                    End If
                                                End If
                                            Else
                                                'incondizionato
                                                '0 marcatura
                                            End If
                                            If percentage! = 0 Then ok% = 1
                                
                                            If uncond% = 0 Then
                                                If (ok% = 1) And (intProgrammazione = 1) Then
                                                    Call setcut2700(1, slope%, 0, 0, (vnom1! * 1.1), 0, dcut%, vnom1!, vnom2!)
                                                    'Delay (300)
                                                End If
                                            End If
                                        Else 'else relativo al controllo su funct%=1
                                            If (cutType% <> 2) And (cutType% <> 3) And (cutType% <> 7) Then
                                                'Taglio Resistivo
                                                slope% = 1
                                
                                                theresval! = ornom1!
                                                'pretest volt
                                                If rnom2! > 0 Then theresval! = ornom2!
                                                If uncond% = 0 Then
                                                    perc1! = Val(cleanval(myrecord.tolcut))
                                                    'perc1! = 0
                                                    Call rr2700(theval!, 0, 0, 0, 6)
                                                    If slope% = 1 Then
                                                        Call testres2700(percentage!, perc1!, guard%, theresval!, 1, 1, ok%)
                                                        'If theval! >= theresval! Then perc1! = 1
                                                    End If
                                    
                                                    'se la resistenza e' gia' in tolleranza finale setta perc1! a 1 e non tara
                                                    If perc1! = 1 Then
                                                        'taglio gia' scattato
                                                        cuterr% = 1
                                                        GoTo cutfailed
                                                    Else
                                                        ok% = 1
                                                    End If
                                                    If execute% = 0 Then
                                                        'setta comunque lo strumento
                                                        If intProgrammazione = 1 Then
                                                            'Call setcut2700(0, slope%, 0, 0, 0, 0, dcut%, rnom1!, rnom2!)
                                                            Exit Sub
                                                        End If
                                                        Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
                                                        Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
                                                    End If
                                                Else
                                                    'incondizionato
                                                    'o marcatura
                                                End If
                                                If percentage! = 0 Then ok% = 1
                                                'If dcut% = 1 And ok% = 1 Then
                                                'il calcolo del valore del first pre-stop viene fatto precedentemente
                                                'calcolo del primo valore a cui fermarsi nel caso di taglio doppio o a L
                                                '1st cut su % diff valore di prestest e valore finale
                                                'Call readres(theresval!, guard%)
                                                ' new1! = theresval! + ((rnom2! - theresval!) * Val(cleanval(myrecord.cut1St)) / 100)
                                                ' If new1! > 0 Then rnom1! = new1!
                                                'End If
                                                If uncond% = 0 Then
                                                    If ok% = 1 And (intProgrammazione = 1) Then
                                                        Call setcut2700(0, slope%, 0, 0, 0, 0, dcut%, rnom1!, rnom2!)
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                Loop While moving% > 0
                            
                                If Hasdone% = 2 Or workexit% > 0 Then
                                    'interruzione operatore
                                    cuterr% = 4
                                    GoTo cutfailed
                                End If
                                'comando di taglio galil
                            
                                'qui va messo il correttore di visione, se abilitato
                            
                                gapstepx& = 0
                                gapstepy& = 0
                            
                                ffnn$ = lastfile$
                                If ffnn$ = "\" Then ffnn$ = ""
                                visionfilename$ = thepath1$ + ffnn$ + Trim$(Str(cutn%)) + ".vis"
                                exf$ = Dir$(visionfilename$)
                            
                                If exf$ <> "" And usevision% = 1 Then
                                    'il file di visione esiste
                                    Call loadvision(visionfilename$)
                                    thedimim& = 768& * 576&
                                    ReDim myimage(1 To thedimim&) As Byte
                                    FrmEuro.Digitizer.Halt
                                    FrmEuro.Digitizer.Grab
                                    FrmEuro.Buffer.Get myimage
                                    thedimim& = Len(copystring$)
                                    ReDim mytemplate(1 To thedimim&) As Byte
                                    CopyMemory2 mytemplate(1), copystring$, thedimim&
                                    Call findvision(found%, foundx&, foundy&)
                                    If found% = 1 Then
                                        vgapx& = (foundx& - TempLeft)
                                        vgapy& = (foundy& - TempTop)
                                        gapstepx& = vgapx& * steppixelx!
                                        gapstepy& = vgapy& * steppixely!
                                        'visualizza rettangolo verde di found
                                        FrmEuro.Picfx1.Left = foundx& * Screen.TwipsPerPixelX
                                        FrmEuro.Picfx1.Top = foundy& * Screen.TwipsPerPixelY
                                        FrmEuro.Picfx1.Width = (TempRight - TempLeft) * Screen.TwipsPerPixelX
                                        FrmEuro.Picfy1.Left = FrmEuro.Picfx1.Left
                                        FrmEuro.Picfy1.Top = FrmEuro.Picfx1.Top
                                        FrmEuro.Picfy1.Height = (TempBottom - TempTop) * Screen.TwipsPerPixelY
                                        FrmEuro.Picfx2.Left = FrmEuro.Picfx1.Left
                                        FrmEuro.Picfx2.Width = FrmEuro.Picfx1.Width
                                        FrmEuro.Picfx2.Top = FrmEuro.Picfx1.Top + FrmEuro.Picfy1.Height
                                        FrmEuro.Picfy2.Top = FrmEuro.Picfy1.Top
                                        FrmEuro.Picfy2.Height = FrmEuro.Picfy1.Height
                                        FrmEuro.Picfy2.Left = FrmEuro.Picfy1.Left + FrmEuro.Picfx1.Width
                                        FrmEuro.Picfx1.Visible = True
                                        FrmEuro.Picfx2.Visible = True
                                        FrmEuro.Picfy1.Visible = True
                                        FrmEuro.Picfy2.Visible = True
                                        dddttt& = timeGetTime&
                                        Do
                                            DoEvents
                                            If timeGetTime& - dddttt& < 0 Then Exit Do
                                        Loop While (timeGetTime& - dddttt&) < 200
                                    End If
                                    Erase myimage
                                    Erase mytemplate
                                    copystring$ = ""
                                    FrmEuro.Digitizer.GrabContinuous
                                    DoEvents
                                    FrmEuro.Picfx1.Visible = False
                                    FrmEuro.Picfx2.Visible = False
                                    FrmEuro.Picfy1.Visible = False
                                    FrmEuro.Picfy2.Visible = False

                                End If
                            
                                If gapstepx& <> 0 Or gapstepy& <> 0 Then
                                    'correzione spostamento
                                
                                    ccutrefx& = ccutrefx& + gapstepx&
                                    ccutrefy& = ccutrefy& - gapstepy&
                                
                                    Hasdone% = 0
                                    Call gotoposxyzw(Str(ccutrefx&), Str(ccutrefy&), Str(ccutrefz&), Str(ccutrefw&))
                                    Do
                                        DoEvents
                                        If Hasdone% = 2 Or workexit% > 0 Then
                                            cuterr% = 4
                                            GoTo cutfailed
                                        End If
                                    Loop While moving% > 0
                                End If
                            
                           
                                tcmd$ = "CDONE=0"
                                Debug.Print tcmd$
                                Call galilcmd(tcmd$, res$, ermsg$)
                                Call enablework
                                tcmd$ = "CDONE=0"
                                Debug.Print tcmd$
                                Call galilcmd(tcmd$, res$, ermsg$)
                                FrmEuro.Enabled = False
                                'command for laser
                                Select Case cutType%
                                Case 0
                                    tcmd$ = "XQ#DCUT"
                                Case 1
                                    tcmd$ = "XQ#LCUT"
                                Case 2
                                    tcmd$ = "XQ#MARKC"
                                Case 3
                                    tcmd$ = "XQ#MARKQ"
                                Case 4
                                    'serp
                                    tcmd$ = "XQ#SERCUT"
                                Case 5
                                    tcmd$ = "XQ#SCUT"
                                Case 6
                                    tcmd$ = "XQ#ICUT"
                                Case 7
                                    tcmd$ = "XQ#ICUT"
                                Case 8
                                    tcmd$ = "XQ#MCUT"
                                End Select
                                Debug.Print tcmd$
                            
                                'move laser lamp!!!!
                                Call galilcmd(tcmd$, res$, ermsg$)
                                'attesa primo stop
                                tout% = 0
                                numseconds& = 16000 / cutspeed&
                                If cutType% = 8 Then
                                numseconds& = numseconds& * intNCut
                                End If
                                Hasdone% = 0
                                If dcut% = 0 Then tout% = 5
                                If uncond% = 1 Then tout% = -1
                                'wait for cut complected
                                Call waitcut2700(600000, tout%)
                                If Hasdone% = 2 Or workexit% > 0 Then
                                    cuterr% = 4
                                    GoTo cutfailed
                                End If
                                'If tout% = 1 Then
                                    'MsgBox ("Cut Timeout")
                                    'cuterr% = 2
                                    'GoTo cutfailed
                                'End If
                            
                                'se taglio di marcatura esco senza test
                                'If (cutType% = 2) Or (cutType% = 3) Then
                                    'Call exitwork
                                    'Exit Sub
                                'End If
                            
                            
                            
                                'tcmd$ = "CDONE=0"
                                'Debug.Print tcmd$
                                'Call galilcmd(tcmd$, res$, ermsg$)
                                If dcut% = 1 Then
                                    'taglio di tipo doppio
                                    If funct% = 1 Then
                                        If slope% = 0 Then
                                            Call set2cut2700(0)
                                        Else
                                            Call set2cut2700(1)
                                        End If
                                    Else
                                        'taglio Resistivo
                                        Call set2cut2700(1)
                                    End If
                                    'tout% = -5
                                    tout% = 0
                                    numseconds& = 16000 / cutspeed&
                                    Hasdone% = 0
                                    Call waitcut2700(300000, tout%)
                                    If Hasdone% = 2 Or workexit% > 0 Then
                                        cuterr% = 4
                                        GoTo cutfailed
                                    End If
                                    If tout% = 1 Then
                                        cuterr% = 2
                                        GoTo cutfailed
                                    End If
                                End If
                            
                                'Tempo di ritardo dopo taratura
                                If DelayAfterTrim > 0 Then
                                    'Call Delay(CLng(DelayAfterTrim))
                                End If
                            
                                'test finale resistenza
                                percentage! = Val(cleanval(myrecord.tolcut))
                                If (blnStopGeometrico = False) Then
                                    If percentage! > 0 And uncond% = 0 And cutType% <> 6 Then
                                        If funct% = 0 And (cutType% <> 2 And cutType% <> 3) Then
                                            'Taglio Resistivo
                                    
                                            theresval! = ornom1!
                                            If rnom2! > 0 Then theresval! = ornom2!
                                            'Call ReadResOld(theval!, 0)
                                            Call rr2700(theval!, 0, 0, 0, 6)
                                            'Mette in E2 il valore della resistenza finale
                                            thematrix$(4002) = Trim$(Str(theValOhm!))
                                            ok% = 0
                                            If ((Abs(theValOhm! - theresval!) / theresval!) * 100) < percentage! Then ok% = 1
    
                                        Else
                                            'Taglio Funzionale
                                    
                                            thevoltval! = ovnom1!
                                            If vnom2! > 0 Then thevoltval! = ovnom2!
                                            Call rv2700(theval!, 0, 0, 6)
                                            ok% = 0
                                            If ((Abs(theval! - thevoltval!) / thevoltval!) * 100) < percentage! Then ok% = 1
                                        End If
                                        If ok% = 0 Then
                                            cuterr% = 5
                                            GoTo cutfailed
                                        End If
                                    End If
                                End If
                                Exit Sub
                            Else
                                'Cycle select cut, only cutType7 supported
                                
                                'probe only one use
                                DoEvents
                                'wait for probe
                                Do While Not MovingProbe% = 0
                                    DoEvents
                                Loop
                                'If cutType% = 6 Or cutType% = 7 Or cutType% = 2 Or cutType% = 3 Then
                                Call ProbeUp
                            
                                For n = LBound(cuts$) To UBound(cuts$) Step 1
                                    'cut's number
                                    cutn% = Val(cuts$(n))
                                    k% = cutn%
                                    
                                    tcmd$ = "Cut " + Str(cutn%)
                                    Debug.Print tcmd$
                                    'check cut's exists and load cut
                                    If k% <= UBound(TrimMatrix) Then
                                        myrecord = TrimMatrix(k%)
                                    Else
                                        cuterr% = 3
                                        GoTo cutfailed
                                    End If
                                    rnom2! = 0
                                    rnom1! = 0
                                    dcut% = 0
                                    uncond% = 0
                                    'only move values
                                    cutType% = Val(cleanval(myrecord.cutType))
                                    cutstep! = Val(cleanval(myrecord.serpstep))
                                    funct% = Val(cleanval(myrecord.func))
                                    thefunctional% = funct%
                                    
                                    'read start end stop positions
                                    gapx& = Val(cleanval(myrecord.startx))
                                    gapy& = Val(cleanval(myrecord.starty))
                                    'posizione di start ultimo taglio multiplo
                                    gapx2& = Val(cleanval(myrecord.start2X))
                                    gapy2& = Val(cleanval(myrecord.start2Y))
                                    deltax& = Val(cleanval(myrecord.stopx)) - Val(cleanval(myrecord.startx))
                                    deltay& = Val(cleanval(myrecord.stopy)) - Val(cleanval(myrecord.starty))
                                    'delta relativo all'ultimo taglio multiplo
                                    deltaMultipleX2& = Val(cleanval(myrecord.stop2X)) - Val(cleanval(myrecord.start2X))
                                    deltaMultipleY2& = Val(cleanval(myrecord.stop2Y)) - Val(cleanval(myrecord.start2Y))
                                    'delta step totale fra il primo taglio e l'ultimo
                                    deltaStepTotX& = Val(cleanval(myrecord.start2X)) - Val(cleanval(myrecord.startx))
                                    deltaStepTotY& = Val(cleanval(myrecord.start2Y)) - Val(cleanval(myrecord.starty))
     
                                    'If cutType% = 4 Then deltaz& = mm2step#(Val(cleanval(myrecord.serpstep)), stepxy!)
                                    cutspeed& = mm2step#(Val(cleanval(myrecord.speed)), stepxy!)
                                
                                'Next n
                                    'qui inserisco la parte di codice per gestire la marcatura quando
                                    'si va selezionare il taglio di tipo 2 o 3; vado a salvare i parametri
                                    'da assare alla Galil per eseguire correttamente i tagli di marcatura
                                    'data for cutting
                                
                                    'fine parte relativa alla marcatura
                                    'data for cutting
                                    If dcut% = 1 Then
                                        Select Case UCase$(cleanval(myrecord.cutDir))
                                        Case "0"
                                            deltax1& = deltax&
                                            deltay1& = 0
                                            deltax2& = 0
                                            deltay2& = deltay&
                                        Case Else
                                            deltax1& = 0
                                            deltay1& = deltay&
                                            deltax2& = deltax&
                                            deltay2& = 0
                                        End Select
                                    Else
                                        thecutdir$ = UCase$(cleanval(myrecord.cutDir))
                                        If cutType% <> 4 Then
                                            thecutdir$ = "0"
                                            If Abs(deltay&) > Abs(deltax&) Then thecutdir$ = "1"
                                        End If
                                        'favourite cut number 7
                                        If (cutType% = 7) Then
                                            deltax1& = deltax&
                                            deltay1& = deltay&
                                            deltax2& = 0
                                            deltay2& = 0
                                        End If
                                    End If
                                
                                    'posizionamento su step e inizio taglio
                                    guard% = 0
                                    'thetable use for count laser move
                                    If thetable% = 0 Then
                                        ccutrefx& = cutrefx& + gapx&
                                        ccutrefy& = cutrefy& + gapy&
                                        ccutrefz& = cutrefz& + gapZ&
                                        ccutrefw& = cutrefw& + gapW&
                                    Else
                                        ccutrefx& = cutrefx1& + gapx&
                                        ccutrefy& = cutrefy1& + gapy&
                                        ccutrefz& = cutrefz& + gapZ&
                                        ccutrefw& = cutrefw& + gapW&
                                    End If
                                    Hasdone% = 0
                                                                                  
                                    'set laser move
                                    Call gotoposxyzw(Trim(Str(ccutrefx&)), Trim(Str(ccutrefy&)), Trim(Str(ccutrefz&)), Trim(Str(ccutrefw&)))
                                    Call setoutput(0, d0.O_Rele_Work_Standby)
                                    thefreq! = Val(cleanval(myrecord.freq)) * 1000
                                    lastfreq$ = Trim$(Str(thefreq!))
                                    'set frequency
                                    Call setfreqda(1, thefreq!)
                                    thecurr! = Val(cleanval(myrecord.current))
                                    lastcurr$ = Trim$(Str(thecurr!))
                                    'set laser lamp current and check max value
                                    Call setda(1, thecurr!, ilasmax!)
                                    'Delay (500)
                                    'controllo puntali
                                    Call readdevice(0, 0, 5)
                                    'view lamp information
                                    If FrmEuro.Imglampon.Visible = True Then
                                        If FrmEuro.Lblfreq.Caption <> lastfreq$ Then FrmEuro.Lblfreq.Caption = lastfreq$
                                        If FrmEuro.Lblcurr.Caption <> lastcurr$ Then FrmEuro.Lblcurr.Caption = lastcurr$
                                    Else
                                        If FrmEuro.Lblfreq.Caption <> "0" Then FrmEuro.Lblfreq.Caption = "0"
                                        If FrmEuro.Lblcurr.Caption <> "0" Then FrmEuro.Lblcurr.Caption = "0"
                                    End If
                                    DoEvents
                                    'wait for probe
                                    'Do While Not MovingProbe% = 0
                                        'DoEvents
                                    'Loop
                                    ''If cutType% = 6 Or cutType% = 7 Or cutType% = 2 Or cutType% = 3 Then
                                    'Call ProbeUp
                            
                                    percentage! = Val(cleanval(myrecord.tolaccept))
                                    tcmd$ = "DX1=" + Str(deltax1&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "DY1=" + Str(deltay1&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "DX2=" + Str(deltax2&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    tcmd$ = "DY2=" + Str(deltay2&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                
                                    tcmd$ = "VSC=" + Str(cutspeed&)
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                            
                                    theiset! = 0
                                    donetest% = 0
                                    Hasdone% = 0
                                    Do
                                        DoEvents
                                        If Hasdone% = 2 Or workexit% > 0 Then
                                            cuterr% = 4
                                            GoTo cutfailed
                                        End If
                                        If donetest% = 0 Then
                                            donetest% = 1
                                            If funct% = 1 And ((cutType% <> 2 And cutType% <> 3)) Then
                                                'taglio funzionale
                                
                                                thevoltval! = ovnom1!
                                                'pretest volt
                                                If vnom2! > 0 Then thevoltval! = ovnom2!
                                                If uncond% = 0 Then
                                                    perc1! = Val(cleanval(myrecord.tolcut))
                                                    'perc1! = 0
                                                    If intProgrammazione = 1 Then
                                                        Call rv2700(theval!, 0, 0, 6)
                                                        If slope% = 1 Then
                                                            Call testvolt2700(percentage!, perc1!, guard%, thevoltval!, 1, 1, ok%)
                                                            If theval! >= thevoltval! Then perc1! = 1
                                                        Else
                                                            Call testvolt2700(percentage!, perc1!, guard%, thevoltval!, 2, 1, ok%)
                                                            If theval! <= thevoltval! Then perc1! = 1
                                                        End If
                                        
                                                        'se la resistenza e' gia' in tolleranza finale setta perc1! a 1 e non tara
                                                        If perc1! = 1 Then
                                                            'taglio gia' scattato
                                                            cuterr% = 1
                                                            GoTo cutfailed
                                                        Else
                                                            ok% = 1
                                                        End If
                                        
                                                        If execute% = 0 Then
                                                            'setta comunque lo strumento
                                                            Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
                                                            Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
                                                            'Call setcut2700(1, slope%, 0, 0, 0, 0, dcut%, thevoltval!, 0)
                                                            Exit Sub
                                                            'End If
                                                        End If
                                                    End If
                                                Else
                                                    'incondizionato
                                                    '0 marcatura
                                                End If
                                                If percentage! = 0 Then ok% = 1
                                
                                                If uncond% = 0 Then
                                                    If (ok% = 1) And (intProgrammazione = 1) Then
                                                        Call setcut2700(1, slope%, 0, 0, (vnom1! * 1.1), 0, dcut%, vnom1!, vnom2!)
                                                        'Delay (300)
                                                    End If
                                                End If
                                            End If
                                        End If
                                    Loop While moving% > 0
                            
                                    If Hasdone% = 2 Or workexit% > 0 Then
                                        'interruzione operatore
                                        cuterr% = 4
                                        GoTo cutfailed
                                    End If
                                    'comando di taglio galil
                            
                                    'qui va messo il correttore di visione, se abilitato
                            
                                    gapstepx& = 0
                                    gapstepy& = 0
                            
                                    ffnn$ = lastfile$
                                    If ffnn$ = "\" Then ffnn$ = ""
                                    visionfilename$ = thepath1$ + ffnn$ + Trim$(Str(cutn%)) + ".vis"
                                    exf$ = Dir$(visionfilename$)
                            
                                    If exf$ <> "" And usevision% = 1 Then
                                        'il file di visione esiste
                                        Call loadvision(visionfilename$)
                                        thedimim& = 768& * 576&
                                        ReDim myimage(1 To thedimim&) As Byte
                                        FrmEuro.Digitizer.Halt
                                        FrmEuro.Digitizer.Grab
                                        FrmEuro.Buffer.Get myimage
                                        thedimim& = Len(copystring$)
                                        ReDim mytemplate(1 To thedimim&) As Byte
                                        CopyMemory2 mytemplate(1), copystring$, thedimim&
                                        Call findvision(found%, foundx&, foundy&)
                                        If found% = 1 Then
                                            vgapx& = (foundx& - TempLeft)
                                            vgapy& = (foundy& - TempTop)
                                            gapstepx& = vgapx& * steppixelx!
                                            gapstepy& = vgapy& * steppixely!
                                            'visualizza rettangolo verde di found
                                            FrmEuro.Picfx1.Left = foundx& * Screen.TwipsPerPixelX
                                            FrmEuro.Picfx1.Top = foundy& * Screen.TwipsPerPixelY
                                            FrmEuro.Picfx1.Width = (TempRight - TempLeft) * Screen.TwipsPerPixelX
                                            FrmEuro.Picfy1.Left = FrmEuro.Picfx1.Left
                                            FrmEuro.Picfy1.Top = FrmEuro.Picfx1.Top
                                            FrmEuro.Picfy1.Height = (TempBottom - TempTop) * Screen.TwipsPerPixelY
                                            FrmEuro.Picfx2.Left = FrmEuro.Picfx1.Left
                                            FrmEuro.Picfx2.Width = FrmEuro.Picfx1.Width
                                            FrmEuro.Picfx2.Top = FrmEuro.Picfx1.Top + FrmEuro.Picfy1.Height
                                            FrmEuro.Picfy2.Top = FrmEuro.Picfy1.Top
                                            FrmEuro.Picfy2.Height = FrmEuro.Picfy1.Height
                                            FrmEuro.Picfy2.Left = FrmEuro.Picfy1.Left + FrmEuro.Picfx1.Width
                                            FrmEuro.Picfx1.Visible = True
                                            FrmEuro.Picfx2.Visible = True
                                            FrmEuro.Picfy1.Visible = True
                                            FrmEuro.Picfy2.Visible = True
                                            dddttt& = timeGetTime&
                                            Do
                                                DoEvents
                                                If timeGetTime& - dddttt& < 0 Then Exit Do
                                            Loop While (timeGetTime& - dddttt&) < 200
                                        End If
                                        Erase myimage
                                        Erase mytemplate
                                        copystring$ = ""
                                        FrmEuro.Digitizer.GrabContinuous
                                        DoEvents
                                        FrmEuro.Picfx1.Visible = False
                                        FrmEuro.Picfx2.Visible = False
                                        FrmEuro.Picfy1.Visible = False
                                        FrmEuro.Picfy2.Visible = False

                                    End If
                            
                                    If gapstepx& <> 0 Or gapstepy& <> 0 Then
                                        'correzione spostamento
                                
                                        ccutrefx& = ccutrefx& + gapstepx&
                                        ccutrefy& = ccutrefy& - gapstepy&
                                
                                        Hasdone% = 0
                                        Call gotoposxyzw(Str(ccutrefx&), Str(ccutrefy&), Str(ccutrefz&), Str(ccutrefw&))
                                        Do
                                            DoEvents
                                            If Hasdone% = 2 Or workexit% > 0 Then
                                                cuterr% = 4
                                                GoTo cutfailed
                                            End If
                                        Loop While moving% > 0
                                    End If
                            
                           
                                    tcmd$ = "CDONE=0"
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    Call enablework
                                    tcmd$ = "CDONE=0"
                                    Debug.Print tcmd$
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    FrmEuro.Enabled = False
                                    'command for laser
                                
                                    'Case 7
                                        tcmd$ = "XQ#ICUT"
                                    Debug.Print tcmd$
                            
                                    'move laser lamp!!!!
                                    Call galilcmd(tcmd$, res$, ermsg$)
                                    'attesa primo stop
                                    tout% = 0
                                    numseconds& = 16000 / cutspeed&
                                    If cutType% = 8 Then
                                        numseconds& = numseconds& * intNCut
                                    End If
                                    Hasdone% = 0
                                    If dcut% = 0 Then tout% = 5
                                    If uncond% = 1 Then tout% = -1
                                    'wait for cut complected
                                    Call waitcut2700(600000, tout%)
                                    If Hasdone% = 2 Or workexit% > 0 Then
                                        cuterr% = 4
                                        GoTo cutfailed
                                    End If
                                    'If tout% = 1 Then
                                        'MsgBox ("Cut Timeout")
                                        'cuterr% = 2
                                        'GoTo cutfailed
                                    'End If
                            
                                    'se taglio di marcatura esco senza test
                                    'If (cutType% = 2) Or (cutType% = 3) Then
                                        'Call exitwork
                                        'Exit Sub
                                    'End If
                            
                            
                            
                                    'tcmd$ = "CDONE=0"
                                    'Debug.Print tcmd$
                                    'Call galilcmd(tcmd$, res$, ermsg$)
                                    If dcut% = 1 Then
                                        'taglio di tipo doppio
                                        If funct% = 1 Then
                                            If slope% = 0 Then
                                                Call set2cut2700(0)
                                            Else
                                                Call set2cut2700(1)
                                            End If
                                        Else
                                            'taglio Resistivo
                                            Call set2cut2700(1)
                                        End If
                                        'tout% = -5
                                        tout% = 0
                                        numseconds& = 16000 / cutspeed&
                                        Hasdone% = 0
                                        Call waitcut2700(300000, tout%)
                                        If Hasdone% = 2 Or workexit% > 0 Then
                                            cuterr% = 4
                                            GoTo cutfailed
                                        End If
                                        If tout% = 1 Then
                                            cuterr% = 2
                                            GoTo cutfailed
                                        End If
                                    End If
                            
                                    'Tempo di ritardo dopo taratura
                                    If DelayAfterTrim > 0 Then
                                        'Call Delay(CLng(DelayAfterTrim))
                                    End If
                            
                                    'test finale resistenza
                                    percentage! = Val(cleanval(myrecord.tolcut))
                                    If (blnStopGeometrico = False) Then
                                        If percentage! > 0 And uncond% = 0 And cutType% <> 6 Then
                                            If funct% = 0 And (cutType% <> 2 And cutType% <> 3) Then
                                                'Taglio Resistivo
                                    
                                                theresval! = ornom1!
                                                If rnom2! > 0 Then theresval! = ornom2!
                                                'Call ReadResOld(theval!, 0)
                                                Call rr2700(theval!, 0, 0, 0, 6)
                                                'Mette in E2 il valore della resistenza finale
                                                thematrix$(4002) = Trim$(Str(theValOhm!))
                                                ok% = 0
                                                If ((Abs(theValOhm! - theresval!) / theresval!) * 100) < percentage! Then ok% = 1
    
                                            Else
                                                'Taglio Funzionale
                                    
                                                thevoltval! = ovnom1!
                                                If vnom2! > 0 Then thevoltval! = ovnom2!
                                                Call rv2700(theval!, 0, 0, 6)
                                                ok% = 0
                                                If ((Abs(theval! - thevoltval!) / thevoltval!) * 100) < percentage! Then ok% = 1
                                            End If
                                            If ok% = 0 Then
                                                cuterr% = 5
                                                GoTo cutfailed
                                            End If
                                        End If
                                    End If
                                Next n
                                Exit Sub
                            End If
cutfailed:
'E1=cuterr%
thematrix$(4001) = Trim$(Str(cuterr%))
FrmEuro.Enabled = True
FrmEuro.Visible = True
'flagwork% = 0

End Sub
Sub waitcut2700(numseconds&, tout%)
   dt! = Timer
   ok% = 0
   a27% = 16
    Call spoll(a27%, poll%, stat%)
    Call spoll(a27%, poll%, stat%)
   tout% = 1
   Do
        DoEvents
        dt1! = Timer
        Do
            DoEvents
            If Timer - dt1! < 0 Then dt1! = Timer
        Loop While Timer - dt1! < 0.1
        Call spoll(a27%, poll%, stat%)
        If (poll% And 64) = 64 Then
            tout% = 0
            Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
            Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
            Exit Do
        End If
        tcmd$ = "CDONE="
        Call galilcmd(tcmd$, res$, ermsg$)
        If (res$ <> 0) Then
           tout% = 0
           Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
           Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
           Exit Sub
        End If
       ' If Hasdone% = 1 Then Hasdone% = 2
        If Hasdone% > 0 Then
             tout% = 0
             Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
             Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
             Exit Do
        End If
        If Timer - dt! < 0 Then dt! = Timer
        
        'controllo CDONE
        'tcmd$ = "CDONE="
        'Call galilcmd(tcmd$, res$, ermsg$)
        'If (res$ <> 0) Then
        '   tout% = 0
        '   Exit Sub
        'End If
   Loop While Timer - dt! < numseconds&
   'If tout% = 0 Then
   '         If mode% = 1 Then
   '             Call rv2700(theval!, 1, 24, 5)
   '         Else
   '             Call rr2700(theval!, 0, 0, 0, 5)
   '         End If
   '         'MsgBox$ (theval!)
   ' Else
   '         'MsgBox ("Cut timeout")
   ' End If
End Sub
Sub setcut2700(mode%, up%, fourwires%, filter%, therange!, dig!, dcut%, nom1!, nom2!)
'imposta taglio con stop resistivo o in tensione, singolo o doppio
'se in tensione, in salita o discesa
'filtro impostabile
'range impostabile
'modalita' 4 fili impostabile
'risoluzione impostabile

'mode: =1 taglio con stop in volt
If a27% = 0 Then a27% = 16
funcname$ = "RES"
If fourwires% = 1 Then funcname$ = "FRES"
If mode% = 1 Then
    'tensione
    fourwires% = 0
    funcname$ = "VOLT:DC"
Else
    'resistivo
    up% = 1
End If
Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%)
'Call send(a27%, ":SENS:FUNC '" + funcname$ + "'", stat%) '
'If fourwires% = 0 Then Call send(a27%, ":SENS:" + funcname$ + ":NPLC 1", stat%) ' Integration rate Influenza la velocita'
If therange! = 0 Then
    Call send(a27%, ":SENS:" + funcname$ + ":RANGE:AUTO 1", stat%)
Else
    'Call send(a27%, ":SENS:" + funcname$ + ":RANGE:AUTO 0", stat%)
    If mode% = 1 Then
        Select Case therange!
        Case Is < 10
            Call send(a27%, ":SENS:" + funcname$ + ":RANGE 10", stat%)
        Case Is < 100
            Call send(a27%, ":SENS:" + funcname$ + ":RANGE 100", stat%)
        End Select
    Else
        Select Case therange!
        Case Is < 1000
            Call send(a27%, ":SENS:" + funcname$ + ":RANGE 1000", stat%)
        Case Is < 10000
            Call send(a27%, ":SENS:" + funcname$ + ":RANGE 10000", stat%)
        Case Is < 100000
            Call send(a27%, ":SENS:" + funcname$ + ":RANGE 100000", stat%)
        Case Is < 1000000
            Call send(a27%, ":SENS:" + funcname$ + ":RANGE 1000000", stat%)
        Case Is < 10000000
            Call send(a27%, ":SENS:" + funcname$ + ":RANGE 10000000", stat%)
        End Select
    End If
End If

'If filter% = 1 Then
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:TCON REP", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:COUN 10", stat%)
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:STAT ON", stat%)
'Else
'    Call send(a27%, ":SENS:" + funcname$ + ":AVER:STAT OFF", stat%)
'End If
'If fourwires% = 1 Then
'    Call send(a27%, ":SENS:" + funcname$ + ":OCOM ON", stat%)
'End If
If dig! = 0 Then dig! = 5
'Call send(a27%, ":SENS:" + funcname$ + ":DIG " + Trim$(Str(dig!)), stat%)
Call send(a27%, ":TRIG:COUNT 1", stat%)
Call send(a27%, ":TRIG:SOURCE IMM", stat%)
Call send(a27%, ":TRIG:DELAY 0", stat%)
Call send(a27%, ":STAT:QUE:CLEAR", stat%)    '
Call send(a27%, ":STAT:PRES", stat%)    ' Status preset
Call send(a27%, "*SRE 1", stat%)    ' SRQ
Call send(a27%, "*CLS", stat%)       ' reset Status Byte Register  per SRQ
If dcut% = 0 Then
        'taglio singolo
        Select Case mode%
        Case 1
            'taglio in tensione
            Select Case up%
                Case 1
                    Call send(a27%, ":CALC3:LIM2:UPP " + Trim$(Str(nom1!)), stat%)
                    Call send(a27%, ":CALC3:LIM2:LOW -100", stat%)
                    'Call send(a27%, ":CALC3:LIM2:STAT 1", stat%)
                    'Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
                    Call send(a27%, ":STAT:MEAS:ENAB 16", stat%)    ' Abilita Limit 2  UP fail
                    'Call send(a27%, ":CALC3:OUTP:LSEN ALOW", stat%) 'output active low
                Case Else
                    'Call send(a27%, ":CALC3:LIM2:UPP " + Trim$(Str(nom1!)), stat%)
                    'Call send(a27%, ":CALC3:LIM2:LOW -100", stat%)
                    Call send(a27%, ":CALC3:LIM2:UPP 100", stat%)
                    Call send(a27%, ":CALC3:LIM2:LOW " + Trim$(Str(nom1!)), stat%)
                    'Call send(a27%, ":CALC3:LIM2:STAT 1", stat%)
                    'Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
                    Call send(a27%, ":STAT:MEAS:ENAB 8", stat%)    ' Abilita Limit 2 down fail
                    'Call send(a27%, ":STAT:MEAS:ENAB 16", stat%)    ' Abilita Limit 2  UP fail
                    'Call send(a27%, ":CALC3:OUTP:LSEN AHIGH", stat%) 'output active low
                End Select
                Call send(a27%, ":CALC3:LIM2:STAT 1", stat%)
                Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
        Case Else
            'taglio in resistenza
            Call send(a27%, ":CALC3:LIM2:UPP " + Trim$(Str(nom1!)), stat%)
            Call send(a27%, ":CALC3:LIM2:LOW -100", stat%)
            Call send(a27%, ":CALC3:LIM2:STAT 1", stat%)
            Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
            Call send(a27%, ":STAT:MEAS:ENAB 16", stat%)    ' Abilita Limit 2  UP fail
        End Select
Else
        'taglio doppio
        Select Case mode%
        Case 1
            'taglio in volt
            Select Case up%
            Case 1
                Call send(a27%, ":CALC3:LIM1:UPP " + Trim$(Str(nom1!)), stat%)
                Call send(a27%, ":CALC3:LIM1:LOW -100", stat%)
                Call send(a27%, ":CALC3:LIM2:UPP " + Trim$(Str(nom2!)), stat%)
                Call send(a27%, ":CALC3:LIM2:LOW -100", stat%)
                'Call send(a27%, ":CALC3:LIM1:STAT 1", stat%)
                'Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
                Call send(a27%, ":STAT:MEAS:ENAB 4", stat%)    ' Abilita Limit 1  UP fail
            Case Else
                Call send(a27%, ":CALC3:LIM1:UPP 100", stat%)
                Call send(a27%, ":CALC3:LIM1:LOW " + Trim$(Str(nom1!)), stat%)
                Call send(a27%, ":CALC3:LIM2:UPP 100", stat%)
                Call send(a27%, ":CALC3:LIM2:LOW " + Trim$(Str(nom2!)), stat%)
                'Call send(a27%, ":CALC3:LIM1:STAT 1", stat%)
                'Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
                Call send(a27%, ":STAT:MEAS:ENAB 2", stat%)    ' Abilita Limit 1  down fail
            End Select
        Case Else
            'taglio in resistenza
            Call send(a27%, ":CALC3:LIM1:UPP " + Trim$(Str(nom1!)), stat%)
            Call send(a27%, ":CALC3:LIM1:LOW 0", stat%)
            Call send(a27%, ":CALC3:LIM2:UPP " + Trim$(Str(nom2!)), stat%)
            Call send(a27%, ":CALC3:LIM2:LOW 0", stat%)
            Call send(a27%, ":CALC3:LIM1:STAT 1", stat%)
            Call send(a27%, ":CALC3:LIM2:STAT 0", stat%)
            Call send(a27%, ":STAT:MEAS:ENAB 4", stat%)    ' Abilita Limit 1  UP fail
        End Select
End If
Call send(a27%, ":CALC3:OUTP:LSEN ALOW", stat%) 'output active low
Call send(a27%, ":CALC3:OUTP:PULS:TIME 0.1", stat%)
Call send(a27%, ":CALC3:OUTP:PULS:STAT ON", stat%)
Call send(a27%, ":CALC3:OUTP:STAT ON", stat%) 'enable outputs
Call send(a27%, ":INIT:CONT ON", stat%)
End Sub

Sub set2cut2700(up%)
If a27% = 0 Then a27% = 16
Call send(a27%, ":INIT:CONT OFF", stat%)
Call send(a27%, ":ABORT", stat%)     ' reset trigger buffer
Call send(a27%, ":STAT:QUE:CLEAR", stat%)    '
Call send(a27%, ":STAT:PRES", stat%)    ' Status preset
Call send(a27%, "*SRE 1", stat%)    ' SRQ
Call send(a27%, "*CLS", stat%)       ' reset Status Byte Register  per SRQ
Call send(a27%, ":CALC3:LIM1:STAT 0", stat%)
Call send(a27%, ":CALC3:LIM2:STAT 1", stat%)
If up% = 1 Then
    Call send(a27%, ":STAT:MEAS:ENAB 16", stat%)    ' Limit 2  FAIL up
Else
    Call send(a27%, ":STAT:MEAS:ENAB 8", stat%)    ' Limit 2  FAIL down
End If
Call send(a27%, ":INIT:CONT ON", stat%)
End Sub

Sub testres2700(percentage!, perc1!, guard%, theresval!, level%, prt%, ok%)
ok% = 1
theValOhm! = thefinalval!
If thefinalval! = 0 Then Call rr2700(theValOhm!, 0, 0, 0, 6)
thematrix$(4003) = Trim(Str(theValOhm!))
realper! = Int(1000 * (theresval! - theValOhm!) / theresval!)
'Exit Sub
realper! = realper! / 10
If perc1! <> 0 Then
    If Abs(realper!) <= Abs(perc1!) Then
            If prt% = 1 Then
                mr$ = "Pretest OK:" + Str$(theValOhm!) + " (" + Trim$(Str$(-realper!)) + "%)"
                Call updatereport(mr$, 2)
            End If
            perc1! = 1
            Exit Sub
    Else
       prt% = 0
    End If
Else
    prt% = 0
End If
perc1! = 0
If level% = 1 Then
    'puo' stare entro valore - percentuale
    If realper! < 0 Then
        tooh% = 1
    End If
    If realper! > percentage! Then
        tool% = 1
    End If
End If
If level% = 2 Then
    'puo' stare entro valore + percentuale
    If realper! > 0 Then
        tool% = 1
    End If
    If realper! < -percentage! Then
        tooh% = 1
    End If
End If
If level% = 3 Then
    'puo' stare entro valore +/- percentuale
    If (realper! > percentage!) Then
        tool% = 1
    End If
    If (realper! < -percentage!) Then
        tooh% = 1
    End If
End If
If tooh% = 1 Then
    ok% = 0
End If
If tool% = 1 Then
    ok% = 0
End If
If prt% = 1 Then
    If ok% = 1 Then
        Select Case level%
        Case 1, 2
            clr% = 0
            mr$ = "Pretest OK:"
        Case 3
            clr% = 2
            mr$ = "Test OK:"
        End Select
    Else
        clr% = 1
        Select Case level%
        Case 1, 2
            mr$ = "Pretest Fail:"
        Case 3
            mr$ = "Test Fail:"
        End Select
    End If
    mr$ = mr$ + Str$(theValOhm!) + " (" + Trim$(Str$(-realper!)) + "%)"
    nok% = ok% Xor 1
    Call updatereport(mr$, clr%)
    theresval! = theValOhm!
Else
   If ok% = 0 Then
      Select Case level%
        Case 1, 2
            mr$ = "Pretest Fail:"
        Case 3
            mr$ = "Test Fail:"
      End Select
      mr$ = mr$ + Str$(theValOhm!) + " (" + Trim$(Str$(-realper!)) + "%)"
      nok% = ok% Xor 1
      Call updatereport(mr$, clr%)
   End If
End If

End Sub

Sub trimr2700(rnom1!, rnom2!, percentage!, timout%, intDigit As Integer)
   'taglio a stop singolo o doppio
   'se singolo su OUT 0
   'se doppio prima Out 1 poi Out 0
   dcut% = 0
   PowLimit! = PowerTrim '0.01
   If rnom1! = 0 Then
        MsgBox ("Impossible with null resistor values!")
        Exit Sub
   End If
   If (rnom2! <> 0) And (rnom2! > rnom1!) Then
        dcut% = 1
   Else
        rnom2! = 0
   End If
   
   thenom! = rnom1!
   If rnom2! > 0 Then thenom! = rnom2!
   
   If intDigit = 0 Then
      Call send(a27%, ":DISP:DIG 6", stat%)
   Else
      Call send(a27%, ":DISP:DIG " + Str(intDigit), stat%)
   End If
   Call send(a27%, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
   Call send(a27%, ":SENS:VOLT:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
   
   If dcut% = 0 Then
        Call send(a27%, ":CALC2:LIM3:UPP" + Str(vset1!), stat%)
        Call send(a27%, ":CALC2:LIM3:LOW -20", stat%)
        Call send(a27%, ":CALC2:LIM3:UPP:SOUR2 13", stat%)
        Select Case vset1!
        Case Is < 2
             Call send(a27%, ":SENS:VOLT:PROT:LEV 4", stat%)
             Call send(a27%, ":SENS:VOLT:DC:RANGE 2", stat%)
        Case 2 To 20
             Call send(a27%, ":SENS:VOLT:PROT:LEV 30", stat%)
             Call send(a27%, ":SENS:VOLT:DC:RANGE 20", stat%)
        Case 20 To 60
             Call send(a27%, ":SENS:VOLT:PROT:LEV 70", stat%)
             Call send(a27%, ":SENS:VOLT:DC:RANGE 60", stat%)
        Case Is > 60
             Call send(a27%, ":SENS:VOLT:PROT:LEV 200", stat%)
             Call send(a27%, ":SENS:VOLT:DC:RANGE 200", stat%)
        End Select
   Else
        Call send(a27%, ":CALC2:LIM2:UPP" + Str(vset1!), stat%)
        Call send(a27%, ":CALC2:LIM2:LOW -20", stat%)
        Call send(a27%, ":CALC2:LIM2:UPP:SOUR2 14", stat%)
        Call send(a27%, ":CALC2:LIM3:UPP" + Str(vset2!), stat%)
        Call send(a27%, ":CALC2:LIM3:LOW -20", stat%)
        Call send(a27%, ":CALC2:LIM3:UPP:SOUR2 13", stat%)
        Select Case vset2!
        Case Is < 2
             Call send(a27%, ":SENS:VOLT:DC:RANGE 2", stat%)
        Case 2 To 20
             Call send(a27%, ":SENS:VOLT:DC:RANGE 20", stat%)
        Case Is > 20
             Call send(a27%, ":SENS:VOLT:DC:RANGE 200", stat%)
        End Select
   End If
   Call send(a27%, ":SOUR:CURR:IMM" + Str(iset!), stat%) ' Source Current IMMediate 1 mA
   Call send(a27%, ":SOUR:DEL 0.001", stat%)   ' Source Delay 1 mS
   Call send(a27%, ":CALC2:FEED VOLT", stat%)
   Call send(a27%, ":CALC2:CLIM:PASS:SOUR2 15", stat%)
   Call send(a27%, ":CALC2:CLIM:BCON IMM", stat%)
   Call send(a27%, ":TRIG:DELAY 0.001", stat%)
   Call send(a27%, ":TRIG:COUNT 1", stat%)
   Call send(a27%, ":ARM:COUN INF", stat%)   ' ARM
   Call send(a27%, ":STAT:QUE:CLEAR", stat%)    '
   Call send(a27%, ":STAT:PRES", stat%)    ' Status preset
   Call send(a27%, "*SRE 1", stat%)    ' SRQ
   Call send(a27%, "*CLS", stat%)       ' reset Status Byte Register  per SRQ
   Call send(a27%, ":CALC2:LIM1:STAT 0", stat%)
   If dcut% = 1 Then
        Call send(a27%, ":CALC2:LIM2:STAT 1", stat%)
        Call send(a27%, ":CALC2:LIM3:STAT 0", stat%)
        Call send(a27%, ":STAT:MEAS:ENAB 4", stat%)    ' Limit 2  FAIL
   Else
        Call send(a27%, ":CALC2:LIM2:STAT 0", stat%)
        Call send(a27%, ":CALC2:LIM3:STAT 1", stat%)
        Call send(a27%, ":STAT:MEAS:ENAB 16", stat%)    ' Limit 3  FAIL
   End If
   Call send(a27%, ":OUTPUT ON", stat%)
   Call send(a27%, ":INIT", stat%)
   
End Sub

