Attribute VB_Name = "Mod2400"
'Keithley 2400 multimeter addresss
Global intAddress2400dx As Integer
Global intAddress2400sx As Integer
Public bytTipoLetturaCorrente As Integer  'tipo di lettura corrente: =0 in tensione
'=1 in corrente; =2 in resistenza


Sub initsystem(autozero%, guard%, back%, fourwires%, disp%)
   'reset sistema
   Call send(intAddress2400dx, "*RST", stat%)                ' reset DVM
   'Auto Zero > precisione < velocita'
   If autozero% = 0 Then Call send(intAddress2400dx, ":SYST:AZERO:STAT OFF", stat%)     ' Autozero OFF
   'Guard Ohms
   If guard% = 1 Then Call send(intAddress2400dx, ":SYST:GUAR OHMS", stat%)     ' Guard ohmico
   'Puntali posteriori
   If back% = 1 Then Call send(intAddress2400dx, ":SYST:FRSW REAR", stat%) ' puntali posteriori
   'Misura a 4 fili
   If fourwires% = 1 Then Call send(intAddress2400dx, ":SYST:RSEN ON", stat%)
   'Display
    Call send(intAddress2400dx, ":SYST:BEEP:STAT 0", stat%) 'Beep spento
    If disp% = 0 Then Call send(intAddress2400dx, ":DISP:ENAB OFF", stat%)
   Call send(intAddress2400dx, ":DISP:DIG 7", stat%)          'Risoluzione 4 cifre e 1/2
   'Funzioni concorrenti
   Call send(intAddress2400dx, ":SENS:FUNC:CONC OFF", stat%)
   bytInit = 1
End Sub

Sub setsecondcut(up%)
    Call send(intAddress2400dx, ":ABOR", stat%)     ' reset trigger buffer
    Call send(intAddress2400dx, "*SRE 1", stat%)    ' SRQ
    Call send(intAddress2400dx, "*CLS", stat%)       ' reset Status Byte Register  per SRQ
    Call send(intAddress2400dx, ":CALC2:LIM1:STAT 0", stat%)
    Call send(intAddress2400dx, ":CALC2:LIM2:STAT 0", stat%)
    Call send(intAddress2400dx, ":CALC2:LIM3:STAT 1", stat%)
    If up% = 1 Then
        Call send(intAddress2400dx, ":STAT:MEAS:ENAB 16", stat%)    ' Limit 3  FAIL up
    Else
        Call send(intAddress2400dx, ":STAT:MEAS:ENAB 8", stat%)    ' Limit 3  FAIL down
    End If
    Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
    Call send(intAddress2400dx, ":INIT", stat%)
End Sub

Sub testvolt(percentage!, perc1!, guard%, theresval!, level%, prt%, ok%)
    ok% = 1
    Call ReadVolt(theval!)
    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 waitcut(numseconds&, tout%)
   If debugging% = 1 Then
        Call updatedebug("Waiting for cut stop")
   End If
   If tout% = -5 Then tout% = -1: readit% = 1
   If tout% = -1 Then nopoll% = 1
   If tout% = 5 Then tout% = 0: readit% = 1
   readit% = 0 'non misuro piu' qui
   tout% = 1
   dt! = Timer
   ok% = 0
   'nopoll% = 1
    Call spoll(intAddress2400dx, poll%, stat%)
    Call spoll(intAddress2400dx, poll%, stat%)
   Do
        DoEvents
        If nopoll% = 0 Then
            Call spoll(intAddress2400dx, poll%, stat%)
            If (poll% And 64) = 64 Then
                tout% = 0
                If debugging% = 1 Then Call updatedebug("Cut Stop detected" + Str(poll%))
                Exit Do
            End If
        End If
        If Hasdone% = 1 Then Hasdone% = 2
        If Hasdone% > 0 Then: tout% = 0: Exit Do
        If Timer - dt! < 0 Then dt! = Timer
   Loop While Timer - dt! < numseconds&
    If tout% = 0 And readit% = 1 Then
            Call send(intAddress2400dx, ":ABORT", stat%)     ' legge solo la resistenza
            Call send(intAddress2400dx, ":FORM:ELEM VOLT", stat%)     ' legge solo la resistenza
            Call send(intAddress2400dx, ":ARM:COUNT 1", stat%)
            Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)
            Call send(intAddress2400dx, ":TRIG:SOURCE IMM", stat%)
            Call send(intAddress2400dx, ":TRIG:DELAY 0", stat%)
            Call send(intAddress2400dx, ":OUTPUT ON", stat%)
            Call send(intAddress2400dx, ":READ?", stat%)
            rx$ = Space$(255)
            Call enter(rx$, 255, Rl%, intAddress2400dx, stat%)
            If theiset! = 0 Then
                thefinalval! = Val(rx$)
            Else
                thefinalval! = Val(rx$) / theiset!
            End If
    End If
End Sub

Sub functional(thv!, up%)
   'esegue un taglio funzionale con superamento di soglia in crescendo o calando (up%)
   timout% = 15
   Call ReadVolt!(theval!)
   If up% = 1 Then
        If theval! > thv! Then
            MsgBox ("Voltage value too high")
            Call send(intAddress2400dx, "*RST", stat%)                ' reset DVM
            Exit Sub
        End If
   End If
   If up% = 0 Then
        If theval! < thv! Then
            MsgBox ("Voltage value too low")
            Call send(intAddress2400dx, "*RST", stat%)                ' reset DVM
            Exit Sub
        End If
   End If
   Call initsystem(1, 0, 1, 0, 1)
   Call send(intAddress2400dx, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
   Call send(intAddress2400dx, ":SENS:VOLT:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
   Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
   Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 150", stat%) ' Voltage Compliance
   Call send(intAddress2400dx, ":SOUR:FUNC CURR", stat%)   ' Source Current
   Call send(intAddress2400dx, ":SOUR:CURR:IMM 0", stat%) ' Source Current IMMediate 1 mA
   Call send(intAddress2400dx, ":SOUR:DEL 0.001", stat%)   ' Source Delay 1 mS
   Call send(intAddress2400dx, ":STAT:QUE:CLEAR", stat%)    '
   Call send(intAddress2400dx, ":STAT:PRES", stat%)    ' Status preset
   Call send(intAddress2400dx, "*SRE 1", stat%)    ' SRQ
   Call send(intAddress2400dx, "*CLS", stat%)       ' reset Status Byte Register  per SRQ
   If up% = 1 Then
        Call send(intAddress2400dx, ":CALC2:LIM3:UPP" + Str(thv!), stat%)
        Call send(intAddress2400dx, ":CALC2:LIM3:LOW -100", stat%)
        Call send(intAddress2400dx, ":CALC2:LIM3:UPP:SOUR2 13", stat%)
        Call send(intAddress2400dx, ":STAT:MEAS:ENAB 16", stat%)    ' Limit 3 up  FAIL
   Else
        Call send(intAddress2400dx, ":CALC2:LIM3:UPP 100", stat%)
        Call send(intAddress2400dx, ":CALC2:LIM3:LOW" + Str(thv!), stat%)
        Call send(intAddress2400dx, ":CALC2:LIM3:LOW:SOUR2 13", stat%)
        Call send(intAddress2400dx, ":STAT:MEAS:ENAB 8", stat%)    ' Limit 3 down
   End If
   Call send(intAddress2400dx, ":CALC2:FEED VOLT", stat%)
   Call send(intAddress2400dx, ":CALC2:LIM1:STAT 0", stat%)
   Call send(intAddress2400dx, ":CALC2:LIM2:STAT 0", stat%)
   Call send(intAddress2400dx, ":CALC2:LIM3:STAT 1", stat%)
   Call send(intAddress2400dx, ":CALC2:CLIM:PASS:SOUR2 14", stat%)
   Call send(intAddress2400dx, ":CALC2:CLIM:BCON IMM", stat%)
   Call send(intAddress2400dx, ":TRIG:DELAY 0.001", stat%)
   Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)
   Call send(intAddress2400dx, ":ARM:COUN INF", stat%)   ' ARM
   Call send(intAddress2400dx, ":OUTPUT ON", stat%)
   Call send(intAddress2400dx, ":INIT", stat%)
   dt! = Timer
   ok% = 0
   Do
        DoEvents
        Call spoll(intAddress2400dx, poll%, stat%)
        If (poll% And 64) = 64 Then
            ok% = 1
            Exit Do
        End If
        If Timer - dt! < 0 Then dt! = Timer
   Loop While Timer - dt! < timout%
   Call ReadVolt!(theval!)
  ' Form1.lblres = theval!
   Call send(intAddress2400dx, "*RST", stat%)                ' reset DVM
   If ok% <= 0 Then
        Call MsgBox("Trim timeout")
   Else
        MsgBox ("Done")
   End If
End Sub

Sub ReadVoltOld(theval!, guard%)
    'legge una tensione con campionamento singolo
    'e Isource = 0
    Call initsystem(0, guard%, 1, 0, 1)
    Call send(intAddress2400dx, ":FORM:ELEM VOLT", stat%)     ' legge solo la tensione
    Call send(intAddress2400dx, ":DISP:DIG 6", stat%)
    Call send(intAddress2400dx, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
    Call send(intAddress2400dx, ":SENS:VOLT:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
    Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
    Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 150", stat%) ' Voltage Compliance
    Call send(intAddress2400dx, ":SENS:AVER OFF", stat%)
    Call send(intAddress2400dx, ":SOUR:FUNC CURR", stat%)   ' Source Current
    Call send(intAddress2400dx, ":SOUR:CURR:IMM 0", stat%) ' Source Current IMMediate 1 mA
    Call send(intAddress2400dx, ":SOUR:DEL 0.001", stat%)   ' Source Delay 1 mS
    Call send(intAddress2400dx, ":ARM:COUNT 1", stat%)
    Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)
    Call send(intAddress2400dx, ":TRIG:SOURCE IMM", stat%)
    Call send(intAddress2400dx, ":TRIG:DELAY 0", stat%)
    Call send(intAddress2400dx, ":OUTPUT ON", stat%)
    Call send(intAddress2400dx, ":READ?", stat%)
    rx$ = Space$(255)
    Call enter(rx$, 255, Rl%, intAddress2400dx, stat%)
    theval! = Val(rx$)
    Call rescontinue(theval! * 2, 6, 1)
End Sub
Sub Inizializza2400PerLetturaTensione()
'inizializza il 2400 per eseguire delle letture in tensione

bytTipoLetturaCorrente = 0
'''mario''' Call send(intAddress2400dx, "*RST", stat%)
Call send(intAddress2400dx, "*CLS", stat%)
Call send(intAddress2400dx, ":SYST:FRSW REAR", stat%) ' puntali posteriori
Call send(intAddress2400dx, ":SYST:RSEN ON", stat%) 'abilita la misura a 4 puntali
Call send(intAddress2400dx, ":FORM:ELEM VOLT", stat%) 'legge solo tensioni
Call send(intAddress2400dx, ":DISP:DIG 6", stat%)
Call send(intAddress2400dx, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
Call send(intAddress2400dx, ":SENS:VOLT:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
Call send(intAddress2400dx, ":SENS:AVER:TCON REP", stat%)
Call send(intAddress2400dx, ":SENS:AVER:COUNT 10", stat%)
'Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 150", stat%) ' Voltage Compliance
Call send(intAddress2400dx, ":SOUR:FUNC CURR", stat%)   ' Source Current
Call send(intAddress2400dx, ":SOUR:CURR:IMM 0", stat%) ' Source Current IMMediate 1 mA



Call send(intAddress2400dx, ":ARM:COUNT 1", stat%)
Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)

End Sub

Sub ReadVolt(theval!)
'legge una tensione dal 2400 e lo pone in lettura in continua
 If bytTipoLetturaCorrente <> 0 Then Call Inizializza2400PerLetturaTensione
    'Call send(intAddress2400dx, "OUTP OFF", stat%)
    Call send(intAddress2400dx, ":ABOR", stat%)
    Call send(intAddress2400dx, ":ARM:COUNT 1", stat%)
    Call send(intAddress2400dx, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
    Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
'    Call send(intAddress2400dx, ":SENS:AVER:STAT OFF", stat%)
    Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 200", stat%)
    Call send(intAddress2400dx, ":MEAS?", stat%)
    rx$ = Space$(255)
    Call enter(rx$, 255, Rl%, intAddress2400dx, stat%)
    theval! = Val(rx$)
    
    'setto lo strumento in lettura continua
    ' Select Case theval!
     '        Case Is < 2
      '          Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 4", stat%)
       '         Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 2", stat%)
        '     Case 2 To 20
         '       Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 24", stat%)
          '      Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 20", stat%)
           '  Case Is > 20
            '    Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 200", stat%)
             '   Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 200", stat%)
        'End Select
    Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
    Call send(intAddress2400dx, ":OUTPUT ON", stat%)
    Call send(intAddress2400dx, ":INIT", stat%)

End Sub

Sub ReadConTrigger(theval!)
'esegue un numero programmato di letture, quindi ne fa la media

If bytTipoLetturaCorrente <> 0 Then Call Inizializza2400PerLetturaTensione
Call send(intAddress2400dx, ":ABOR", stat%)
Call send(intAddress2400dx, ":ARM:COUN 1", stat%)
Call send(intAddress2400dx, ":TRIG:COUNT 3", stat%)
Call send(intAddress2400dx, ":TRIG:DELAY 0.1", stat%)
Call send(intAddress2400dx, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
Call send(intAddress2400dx, ":READ?", stat%)
Call send(intAddress2400dx, ":CALC:MATH ((volt[0]+volt[1]+volt[2])/3)", stat%)
Call send(intAddress2400dx, ":CALC:STAT ON", stat%)
Call send(intAddress2400dx, ":CALC:DATA?", stat%)

rx$ = Space$(255)
Call enter(rx$, 255, Rl%, intAddress2400dx, stat%)
theval! = Val(rx$)
Select Case theval!
       Case Is < 2
            Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 2", stat%)
            Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 2", stat%)
       Case 2 To 20
            Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 20", stat%)
            Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 20", stat%)
       Case Is > 20
            Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 200", stat%)
            Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 200", stat%)
End Select

Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
Call send(intAddress2400dx, ":OUTPUT ON", stat%)
Call send(intAddress2400dx, ":INIT", stat%)

End Sub

Sub ReadAverage(theval!)

If bytTipoLetturaCorrente <> 0 Then Call Inizializza2400PerLetturaTensione
Call send(intAddress2400dx, ":ABOR", stat%)
Call send(intAddress2400dx, ":ARM:COUN 1", stat%)
Call send(intAddress2400dx, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 200", stat%)
Call send(intAddress2400dx, ":SENS:AVER:STAT ON", stat%)
Call send(intAddress2400dx, ":MEAS:VOLT:DC?", stat%)

rx$ = Space$(255)
Call enter(rx$, 255, Rl%, intAddress2400dx, stat%)
theval! = Val(rx$)
'Select Case theval!
 '      Case Is < 2
  '          Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 4", stat%)
   '         Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 2", stat%)
    '   Case 2 To 20
     '       Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 24", stat%)
      '      Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 20", stat%)
       'Case Is > 20
        '    Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 200", stat%)
         '   Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 200", stat%)
'End Select

Call send(intAddress2400dx, ":SENS:AVER:STAT OFF", stat%)
Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
Call send(intAddress2400dx, ":OUTPUT ON", stat%)
Call send(intAddress2400dx, ":INIT", stat%)

End Sub

Sub executd2400(cutn%, thetable%, execute%, cuterr%, intProgrammazione As Integer)
                            'attende che la tavola sia in posizione
                            Hasdone% = 0
                            flagwork% = 1
                            cuterr% = 0
                            cutrefx& = Val(TLasReferenceX$)
                            cutrefy& = Val(TLasReferenceY$)
                            cutrefx1& = Val(TLasReferenceX1$)
                            cutrefy1& = Val(TLasReferenceY1$)
                            cutrefz& = PositionTableIn
                            cutrefw& = Val(FocusPos)
                            thematrix$(4002) = "0"
                            thematrix$(4003) = "0"
                            If doubletable% = 1 Then
                            Do
                                Call readdevice(0, 0, 5)
                                If execute% = 1 Then
                                    'If doubletable% = 1 Then
                                        'If thetable% = 0 Then
                                        '    If d0.I_Tavola_A_NReady = 0 Then Exit Do
                                        'Else
                                        '    If d0.I_Tavola_B_NReady = 0 Then Exit Do
                                        'End If
                                    'End If
                                Else
                                    Exit Do
                                End If
                                DoEvents
                                ek% = GetAsyncKeyState%(27) 'esc key
                                If Hasdone% = 2 Or workexit% > 0 Or ek% <> 0 Then
                                    'interruzione operatore
                                    cuterr% = 4
                                    'If ek% <> 0 Then SendKeys "{ESC}", 1
                                     If ek% <> 0 Then pressesc% = 1
                                    GoTo cutfailed
                                End If
                            
                            Loop
                            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
                            k% = cutn%
                            If k% <= UBound(TrimMatrix) Then
                                myrecord = TrimMatrix(k%)
                            Else
                                cuterr% = 3
                                GoTo cutfailed
                            End If
                            rnom2! = 0
                            rnom1! = 0
                            dcut% = 0
                            uncond% = 0
                            cutType% = Val(cleanval(myrecord.cutType))
                            cutstep! = Val(cleanval(myrecord.serpstep))
                            funct% = Val(cleanval(myrecord.func))
                            thefunctional% = funct%
                                                        
                            '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)
                            
                            Select Case cutType%
                                Case 2, 3, 6
                                    Call MoveProbe("0", "0", "0", "0", 1)
                                Case Else
                                    Call MoveProbe(Str(TmpExecCAX), Str(TmpExecCAY), Str(TmpExecCBX), Str(TmpExecCBY), 1)
                            End Select
                            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))
                            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 ReadResInTensione(theval!, rnom2!, theValOhm!)
                                    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 ReadVolt(theval!)
                                    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, 7
                            
                                '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
                                Call gotoposw(FocusPos)
                                Do While Not moving% = 0
                                    DoEvents
                                Loop
                                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 ReadResInTensione(theval!, rnom1!, theValOhm!)
                                    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) 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
                            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!)
                            
                            '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
                            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
                            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
                                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
                            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% = 7 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
                            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
                            
                            If (AutoFocusType = 1) And (Val(cleanval(myrecord.AutoFEnable)) = 1) Then
                                StartCutXPP = step2mm(Val(cleanval(myrecord.startx)), stepxy!)
                                StartCutYPP = step2mm(Val(cleanval(myrecord.starty)), stepxy!)
                            
                                AdjWFocus = -(DwFocus + (VarWXmm * (StartCutXPP - Ref1FocusXmm)) + (VarWYmm * (StartCutYPP - Ref1FocusYmm)))
                                AdjWFocusPP = mm2step(CLng(AdjWFocus), CLng(StepFocus))
                            
                                If AdjWFocusPP < 0 Then
                                    AdjWFocusPP = FocusPos + AdjWFocusPP
                                Else
                                    AdjWFocusPP = FocusPos - AdjWFocusPP
                                End If
                            End If
                            
                            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!))
                            Call setfreqda(1, thefreq!)
                            thecurr! = Val(cleanval(myrecord.current))
                            lastcurr$ = Trim$(Str(thecurr!))
                            Call setda(1, thecurr!, ilasmax!)
                            Delay (500)
                            'controllo puntali
                            Call readdevice(0, 0, 5)
                            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
                            Do While Not MovingProbe% = 0
                                DoEvents
                            Loop
                            If cutType% = 6 Or cutType% = 2 Or cutType% = 3 Then
                                If (d0.I_Puntali_Alti_Probe1 = 0 Or d0.I_Puntali_Alti_Probe2 = 0) Then
                                    Call ProbeUp
                                End If
                            Else
                                Call ProbeDown
                            End If
                            'If d0.I_Puntali_Rosa_Alti = 0 Then
                             '   puntali% = 0
                            'Else
                                'puntali% = 1
                            'End If
                            'If flagpuntali% = 0 Then puntali% = 0
                            'Do
                                'DoEvents
                                'If Hasdone% = 2 Or workexit% > 0 Then Exit Do
                            'Loop While puntali% = 1
                           ' If Hasdone% = 2 Or workexit% > 0 Then
                            '    cuterr% = 4
                             '   GoTo cutfailed
                            '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% = 7 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 ReadVolt(theval!)
                                    If slope% = 1 Then
                                        Call testvolt(percentage!, perc1!, guard%, thevoltval!, 1, 1, ok%)
                                        If theval! >= thevoltval! Then perc1! = 1
                                    Else
                                        Call testvolt(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 setcut2400(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
                                'il calcolo del first pre-stop viene fatto precedentemente, non lo ripeto
                                'If dcut% = 1 And ok% = 1 Then
                                 '  Call readvolt(thevoltval!)
                                '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
                                '   new1! = thevoltval! + ((vnom2! - thevoltval!) * Val(cleanval(myrecord.cut1St)) / 100)
                                 '  If new1! > 0 Then vnom1! = new1!
                                'End If
                                If uncond% = 0 Then
                                    If (ok% = 1) And (intProgrammazione = 1) Then
                                        Call setcut2400(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) 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 ReadResOld(theval!, 0)
                                     If slope% = 1 Then
                                        Call testres(percentage!, perc1!, guard%, theresval!, 1, 1, ok%)
'                                        If theValOhm! >= 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 trimr(theresval!, 0, percentage!, 100, 5)
                                        Exit Sub
                                        End If
                                     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 trimr(rnom1!, rnom2!, percentage!, 100, 7)
                                     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
                            
                            If AutoFocusType = 1 And cutType% <> 8 And Val(cleanval(myrecord.AutoFEnable)) = 1 Then
                                Call gotoposw(Str(AdjWFocusPP))
                                Do While Not moving% = 0
                                    DoEvents
                                Loop
                            End If
                            
                            If AutoFocusType = 2 And cutType% <> 8 And Val(cleanval(myrecord.AutoFEnable)) = 1 Then
                                Dim TmpFocusPosX As Long
                                Dim TmpFocusPosY As Long
                                
                                Dim TmpStartX1 As Long
                                Dim TmpStartY1 As Long
                                Dim TmpStartX2 As Long
                                Dim TmpStartY2 As Long
                                
                                Dim TmpDiagX As Long
                                Dim TmpDiagY As Long
                                
                                Dim TmpOffSetFocusX As Long
                                Dim TmpOffSetFocusY As Long
                                
                                Dim TmpDistance As Single
                                
                                'TmpStartX1 = Val(cleanval(myrecord.startx))
                                'TmpStartY1 = Val(cleanval(myrecord.starty))
                                'TmpStartX2 = Val(cleanval(myrecord.stopx))
                                'TmpStartY2 = Val(cleanval(myrecord.stopy))
                                
                                'TmpDiagX = ((TmpStartX2 - TmpStartX1) / 2) '+ TmpStartX1
                                'TmpDiagY = ((TmpStartY2 - TmpStartY1) / 2) '+ TmpStartY1
                                
                                TmpOffSetFocusX = OffSetFocusX '+ TmpDiagX
                                TmpOffSetFocusY = OffSetFocusY '+ TmpDiagY
                                
                                TmpFocusPosX = ccutrefx& + TmpOffSetFocusX
                                TmpFocusPosY = ccutrefy& + TmpOffSetFocusY
                                Call gotoposxy(Str(TmpFocusPosX), Str(TmpFocusPosY))
                                Do While Not moving% = 0
                                    DoEvents
                                Loop
                                
                                TmpDistance = CalFocusANG - ReadMetro
                                AdjWFocus = TmpDistance
                                AdjWFocusPP = mm2stepS(AdjWFocus, CLng(StepFocus))
                                Call updDst("Distanza: " & cleanval(myrecord.reslabel) & " : " & Str(AdjWFocus))
                                Call gotoposw(Str(FocusPos - AdjWFocusPP))
                                Do While Not moving% = 0
                                    DoEvents
                                Loop
                                Call gotoposxy(Str(ccutrefx&), Str(ccutrefy&))
                                Do While Not moving% = 0
                                    DoEvents
                                Loop
                            End If
                            
                            Call SetFilter(Val(cleanval(myrecord.recFilter1)), Val(cleanval(myrecord.recFilter2)), Val(cleanval(myrecord.recFilter3)), Val(cleanval(myrecord.recFilter4)))
                            
                            tcmd$ = "CDONE=0"
                            Debug.Print tcmd$
                            Call galilcmd(tcmd$, res$, ermsg$)
                            Call enablework
                            FrmEuro.Enabled = False
                            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#MCUT"
                            End Select
                            Debug.Print tcmd$
                            
                            Call galilcmd(tcmd$, res$, ermsg$)
                            'attesa primo stop
                            tout% = 0
                            numseconds& = 16000 / cutspeed&
                            If cutType% = 7 Then
                               numseconds& = numseconds& * intNCut
                            End If
                            Hasdone% = 0
                            If dcut% = 0 Then tout% = 5
                            If uncond% = 1 Then tout% = -1
                            Call waitcut2400(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
                            
                            
                            If dcut% = 1 Then
                                'taglio di tipo doppio
                              If funct% = 1 Then
                                 If slope% = 0 Then
                                    Call setsecondcut(0)
                                 Else
                                    Call setsecondcut(1)
                                 End If
                              Else
                              'taglio Resistivo
                                 Call trimrSecondCut
                              End If
                                'tout% = -5
                                 tout% = 0
                                 numseconds& = 16000 / cutspeed&
                                'Hasdone% = 0
                                 Call waitcut2400(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 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 ReadResInTensione(theval!, theresval!, theValOhm!)
                                    '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 ReadVolt(theval!)
                                    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
                            '''mario''' Call send(intAddress2400dx, ":OUTPUT OFF", stat%)
                            Exit Sub
cutfailed:
'E1=cuterr%
thematrix$(4001) = Trim$(Str(cuterr%))
FrmEuro.Enabled = True
FrmEuro.Visible = True
flagwork% = 0
'''mario''' Call send(intAddress2400dx, ":OUTPUT OFF", stat%)

End Sub

Sub rescontinue(sngRange As Single, intDigit As Integer, bytAbilitazioneUscita As Byte)
'   If FrmEuro.Imgmacon.Visible = False Then Exit Sub
'   If thefunctional% = 1 Then
        'setta lo strumento per misura continua di volt a corrente nulla
        Call initsystem(1, 0, 1, 0, 1)
        If intDigit = 0 Then
            Call send(intAddress2400dx, ":DISP:DIG 5", stat%)
        Else
            Call send(intAddress2400dx, ":DISP:DIG " + Str(intDigit), stat%)
        End If
        Call send(intAddress2400dx, ":SENS:FUNC 'VOLT:DC'", stat%) ' Sense Voltage
'        Call send(intAddress2400dx, ":CONF:VOLT", stat%)
        Call send(intAddress2400dx, ":SOUR:FUNC CURR", stat%) ' Source Current IMMediate 0
        Call send(intAddress2400dx, ":SOUR:CURR:IMM 0", stat%) ' Source Current IMMediate 0
        Select Case sngRange
             Case Is < 2
                Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 2", stat%)
                Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 2", stat%)
             Case 2 To 20
                Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 20", stat%)
                Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 20", stat%)
             Case Is > 20
                Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 200", stat%)
                Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 200", stat%)
        End Select
        Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
        If bytAbilitazioneUscita = 1 Then
           Call send(intAddress2400dx, ":OUTPUT ON", stat%)
           Call send(intAddress2400dx, ":INIT", stat%)
        Else
           Call send(intAddress2400dx, ":OUTPUT OFF", stat%)
        End If
  
End Sub

Sub setcut2400Old(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

'NB:IL TAGLIO RESISTIVO VIENE GESTITO DA UN'ALTR FUNZIONE

'mode: =1 taglio con stop in volt
Call initsystem(1, 0, 1, fourwires%, 1)
funcname$ = "RES"
' If fourwires% = 1 Then funcname$ = "RES"
If mode% = 1 Then
    'tensione
    fourwires% = 0
    funcname$ = "VOLT:DC"
Else
    'resistivo
    up% = 1
End If
Call send(intAddress2400dx, ":ABORT", stat%)
Call send(intAddress2400dx, ":SENS:FUNC '" + funcname$ + "'", stat%) '
Call send(intAddress2400dx, ":SENS:" + funcname$ + ":NPLC 1", stat%) ' Integration rate Influenza la velocita'


If mode% = 1 Then
    ' Taglio In Tensione
    Call send(intAddress2400dx, ":SOUR:FUNC CURR", stat%)   ' Source Current
    Call send(intAddress2400dx, ":SOUR:CURR:IMM 0", stat%) ' Source Current IMMediate 1 mA
    Call send(intAddress2400dx, ":SOUR:DEL 0", stat%)   ' Source Delay 1 mS
'Else
    ' Taglio Resistenza
If therange! = 0 Then
    Call send(intAddress2400dx, ":SENS:VOLT:PROT 100", stat%)
    Call send(intAddress2400dx, ":SENS:" + funcname$ + ":RANGE:AUTO 1", stat%)
Else
    Call send(intAddress2400dx, ":SENS:VOLT:PROT " + Str(therange! * 2), stat%)
    Call send(intAddress2400dx, ":SENS:" + funcname$ + ":RANGE:AUTO 0", stat%)
    Call send(intAddress2400dx, ":SENS:" + funcname$ + ":RANGE " + Trim$(Str(therange!)), stat%)
End If

End If

If filter% = 1 Then
    Call send(intAddress2400dx, ":SENS:AVER:TCON REP", stat%)
    Call send(intAddress2400dx, ":SENS:AVER:COUN 10", stat%)
    Call send(intAddress2400dx, ":SENS:AVER:STAT ON", stat%)
Else
    Call send(intAddress2400dx, ":SENS:AVER:STAT OFF", stat%)
End If
If funcname$ = "RES" Then
    Call send(intAddress2400dx, ":SENS:" + funcname$ + ":OCOM ON", stat%)
End If
If dig! = 0 Then dig! = 5
Call send(intAddress2400dx, ":DISP:DIG " + Trim$(Str(dig!)), stat%)
Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)
Call send(intAddress2400dx, ":TRIG:SOURCE IMM", stat%)
Call send(intAddress2400dx, ":TRIG:DELAY 0", stat%)
Call send(intAddress2400dx, ":STAT:QUE:CLEAR", stat%)    '
Call send(intAddress2400dx, ":STAT:PRES", stat%)    ' Status preset
Call send(intAddress2400dx, "*SRE 1", stat%)    ' SRQ
Call send(intAddress2400dx, "*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(intAddress2400dx, ":CALC2:LIM3:UPP " + Trim$(Str(nom1!)), stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:LOW -100", stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:UPP:SOUR2 13", stat%)
                    Call send(intAddress2400dx, ":STAT:MEAS:ENAB 16", stat%)    ' Abilita Limit 2  UP fail
                Case Else
                    Call send(intAddress2400dx, ":CALC2:LIM3:UPP 100", stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:LOW " + Trim$(Str(nom1!)), stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:LOW:SOUR2 13", stat%)
                    Call send(intAddress2400dx, ":STAT:MEAS:ENAB 8", stat%)    ' Abilita Limit 2 down fail
                End Select
                Call send(intAddress2400dx, ":CALC2:LIM3:STAT 1", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM2:STAT 0", stat%)
        Case Else
        
            'taglio in resistenza
       '     Call send(intAddress2400dx, ":CALC2:LIM2:UPP " + Trim$(Str(nom1!)), stat%)
        '    Call send(intAddress2400dx, ":CALC2:LIM2:LOW -100", stat%)
         '   Call send(intAddress2400dx, ":CALC2:LIM2:STAT 1", stat%)
          '  Call send(intAddress2400dx, ":CALC2:LIM1:STAT 0", stat%)
           ' Call send(intAddress2400dx, ":STAT:MEAS:ENAB 4", 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(intAddress2400dx, ":CALC2:LIM2:UPP " + Trim$(Str(nom1!)), stat%)
                Call send(intAddress2400dx, ":CALC2:LIM2:LOW -100", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:UPP " + Trim$(Str(nom2!)), stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:LOW -100", stat%)
                Call send(intAddress2400dx, ":STAT:MEAS:ENAB 4", stat%)    ' Abilita Limit 1  UP fail
                Call send(intAddress2400dx, ":CALC2:LIM2:UPP:SOUR2 14", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:UPP:SOUR2 13", stat%)
            Case Else
                Call send(intAddress2400dx, ":CALC2:LIM2:UPP 100", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM2:LOW " + Trim$(Str(nom1!)), stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:UPP 100", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:LOW " + Trim$(Str(nom2!)), stat%)
                Call send(intAddress2400dx, ":STAT:MEAS:ENAB 2", stat%)    ' Abilita Limit 1  down fail
                Call send(intAddress2400dx, ":CALC2:LIM2:LOW:SOUR2 14", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:LOW:SOUR2 13", stat%)
            End Select
            
            Call send(intAddress2400dx, ":CALC2:LIM2:STAT 1", stat%)
            Call send(intAddress2400dx, ":CALC2:LIM3:STAT 0", stat%)
        Case Else
            'taglio in resistenza
       '     Call send(intAddress2400dx, ":CALC2:LIM1:UPP " + Trim$(Str(nom1!)), stat%)
        '    Call send(intAddress2400dx, ":CALC2:LIM1:LOW 0", stat%)
         '   Call send(intAddress2400dx, ":CALC2:LIM2:UPP " + Trim$(Str(nom2!)), stat%)
          '  Call send(intAddress2400dx, ":CALC2:LIM2:LOW 0", stat%)
           ' Call send(intAddress2400dx, ":CALC2:LIM1:STAT 1", stat%)
            'Call send(intAddress2400dx, ":CALC2:LIM2:STAT 0", stat%)
           ' Call send(intAddress2400dx, ":STAT:MEAS:ENAB 4", stat%)    ' Abilita Limit 1  UP fail
        End Select
End If
Call send(intAddress2400dx, ":CALC2:FEED VOLT", stat%)
Call send(intAddress2400dx, ":CALC2:CLIM:PASS:SOUR2 15", stat%)
Call send(intAddress2400dx, ":CALC2:CLIM:BCON IMM", stat%)
Call send(intAddress2400dx, ":TRIG:DELAY 0.001", stat%)
Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)
Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
Call send(intAddress2400dx, ":OUTPUT ON", stat%) 'enable outputs
Call send(intAddress2400dx, ":INIT", stat%)

End Sub

Sub setcut2400(mode%, up%, fourwires%, filter%, therange!, dig!, dcut%, nom1!, nom2!)
'imposta taglio in tensione, singolo o doppio
'se in tensione, in salita o discesa
'filtro impostabile
'range impostabile
'modalita' 4 fili impostabile
'risoluzione impostabile

mode% = 1
bytTipoLetturaCorrente = 3
Call Inizializza2400PerLetturaTensione

If therange! = 0 Then
    Call send(intAddress2400dx, ":SENS:VOLT:PROT 100", stat%)
    Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE:AUTO 1", stat%)
Else
    Select Case therange!
       Case Is < 2
            Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 2", stat%)
            Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 2", stat%)
       Case 2 To 20
            Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 20", stat%)
            Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 20", stat%)
       Case Is > 20
            Call send(intAddress2400dx, ":SENS:VOLT:PROT:LEV 200", stat%)
            Call send(intAddress2400dx, ":SENS:VOLT:DC:RANGE 200", stat%)
    End Select
End If


If filter% = 1 Then
    Call send(intAddress2400dx, ":SENS:AVER:TCON REP", stat%)
    Call send(intAddress2400dx, ":SENS:AVER:COUN 10", stat%)
    Call send(intAddress2400dx, ":SENS:AVER:STAT ON", stat%)
Else
    Call send(intAddress2400dx, ":SENS:AVER:STAT OFF", stat%)
End If
If dig! = 0 Then dig! = 5
Call send(intAddress2400dx, ":DISP:DIG " + Trim$(Str(dig!)), stat%)
Call send(intAddress2400dx, "*SRE 1", stat%)    ' SRQ
Call send(intAddress2400dx, "*CLS", stat%)       ' reset Status Byte Register  per SRQ
If dcut% = 0 Then
        'taglio singolo
            'taglio in tensione
            Select Case up%
                Case 1
                    Call send(intAddress2400dx, ":CALC2:LIM3:UPP " + Trim$(Str(nom1!)), stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:LOW -100", stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:UPP:SOUR2 13", stat%)
                    Call send(intAddress2400dx, ":STAT:MEAS:ENAB 16", stat%)    ' Abilita Limit 2  UP fail
                Case Else
                    Call send(intAddress2400dx, ":CALC2:LIM3:UPP 100", stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:LOW " + Trim$(Str(nom1!)), stat%)
                    Call send(intAddress2400dx, ":CALC2:LIM3:LOW:SOUR2 13", stat%)
                    Call send(intAddress2400dx, ":STAT:MEAS:ENAB 8", stat%)    ' Abilita Limit 2 down fail
                End Select
                Call send(intAddress2400dx, ":CALC2:LIM3:STAT 1", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM2:STAT 0", stat%)
Else
        'taglio doppio
            'taglio in volt
            Select Case up%
            Case 1
                Call send(intAddress2400dx, ":CALC2:LIM2:UPP " + Trim$(Str(nom1!)), stat%)
                Call send(intAddress2400dx, ":CALC2:LIM2:LOW -100", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:UPP " + Trim$(Str(nom2!)), stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:LOW -100", stat%)
                Call send(intAddress2400dx, ":STAT:MEAS:ENAB 4", stat%)    ' Abilita Limit 1  UP fail
                Call send(intAddress2400dx, ":CALC2:LIM2:UPP:SOUR2 14", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:UPP:SOUR2 13", stat%)
            Case Else
                Call send(intAddress2400dx, ":CALC2:LIM2:UPP 100", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM2:LOW " + Trim$(Str(nom1!)), stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:UPP 100", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:LOW " + Trim$(Str(nom2!)), stat%)
                Call send(intAddress2400dx, ":STAT:MEAS:ENAB 2", stat%)    ' Abilita Limit 1  down fail
                Call send(intAddress2400dx, ":CALC2:LIM2:LOW:SOUR2 14", stat%)
                Call send(intAddress2400dx, ":CALC2:LIM3:LOW:SOUR2 13", stat%)
            End Select
            
            Call send(intAddress2400dx, ":CALC2:LIM2:STAT 1", stat%)
            Call send(intAddress2400dx, ":CALC2:LIM3:STAT 0", stat%)
End If
Call send(intAddress2400dx, ":CALC2:FEED VOLT", stat%)
Call send(intAddress2400dx, ":CALC2:CLIM:PASS:SOUR2 15", stat%)
Call send(intAddress2400dx, ":CALC2:CLIM:BCON IMM", stat%)
Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
Call send(intAddress2400dx, ":OUTPUT ON", stat%) 'enable outputs
Call send(intAddress2400dx, ":INIT", stat%)

End Sub



Sub waitcut2400(numseconds As Long, tout%)
'attende l 'esecuzione del taglio

   dt! = Timer
   ok% = 0
   blnStopGeometrico = False
   Call spoll(intAddress2400dx, poll%, stat%)
   Call spoll(intAddress2400dx, poll%, stat%)
   tout% = 1
   Do
        Call spoll(intAddress2400dx, poll%, stat%)
        If (poll% And 64) = 64 Then
            tout% = 0
            Exit Do
        End If
        
        Call galilcmd("CDONE=", res$, ermsg$)
        'Exit Do
        If (Val(res$) <> 0) Then
           tout% = 0
           blnStopGeometrico = True
           Exit Do
        End If
        Delay (200)
        
      '  If Hasdone% = 1 Then Hasdone% = 2
        If Hasdone% > 0 Then
            tout% = 0
            Exit Do
        End If
        If Timer - dt! < 0 Then dt! = Timer
   Loop While Timer - dt! < numseconds
   
End Sub


Sub ReadCurrold(theval!, guard%)
'lettura della corrente single shot

    Call initsystem(1, guard%, 1, 0, 1)
    Call send(intAddress2400dx, ":FORM:ELEM CURR", stat%)     ' legge solo la resistenza
    Call send(intAddress2400dx, ":DISP:DIG 6", stat%)
    Call send(intAddress2400dx, ":SENS:FUNC 'CURR:DC'", stat%) ' Sense Voltage
    Call send(intAddress2400dx, ":SENS:CURR:NPLC 1", stat%)    ' Integration rate Influrnza la velocita'
    Call send(intAddress2400dx, ":SENS:CURR:DC:RANGE:AUTO 1", stat%)
    Call send(intAddress2400dx, ":SENS:CURR:PROT:LEV 0.2", stat%) ' Voltage Compliance
    Call send(intAddress2400dx, ":SENS:AVER OFF", stat%)
    Call send(intAddress2400dx, ":SOUR:FUNC VOLT", stat%)   ' Source Current
    Call send(intAddress2400dx, ":SOUR:VOLT:IMM 0", stat%) ' Source Current IMMediate 1 mA
    Call send(intAddress2400dx, ":SOUR:DEL 0.001", stat%)   ' Source Delay 1 mS
    Call send(intAddress2400dx, ":ARM:COUNT 1", stat%)
    Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)
    Call send(intAddress2400dx, ":TRIG:SOURCE IMM", stat%)
    Call send(intAddress2400dx, ":TRIG:DELAY 0", stat%)
    Call send(intAddress2400dx, ":OUTPUT ON", stat%)
    Call send(intAddress2400dx, ":READ?", stat%)
    rx$ = Space$(255)
    Call enter(rx$, 255, Rl%, intAddress2400dx, stat%)
    theval! = Val(rx$)
    Call ReadCurrCont(theval! * 2, 6, 1)

End Sub
Sub Inizializza2400PerLetturaCorrente()
'inizializza lo strumento per la lettura in tensione

bytTipoLetturaCorrente = 1
'''mario''' Call send(intAddress2400dx, "*RST", stat%)
Call send(intAddress2400dx, "*CLS", stat%)
Call send(intAddress2400dx, ":SYST:FRSW REAR", stat%) ' puntali posteriori
'Call send(intAddress2400dx, ":SYST:RSEN ON", stat%) 'abilita la misura a 4 puntali
Call send(intAddress2400dx, ":FORM:ELEM CURR", stat%) 'legge solo tensioni
Call send(intAddress2400dx, ":DISP:DIG 6", stat%)
Call send(intAddress2400dx, ":SENS:FUNC 'CURR:DC'", stat%) ' Sense Voltage
Call send(intAddress2400dx, ":SENS:CURR:NPLC 0.01", stat%)    ' Integration rate Influrnza la velocita'
Call send(intAddress2400dx, ":SENS:CURR:DC:RANGE:AUTO 1", stat%)
'Call send(intAddress2400dx, ":SENS:CURR:PROT:LEV 0.2", stat%) ' Voltage Compliance
Call send(intAddress2400dx, ":SOUR:FUNC VOLT", stat%)   ' Source Current
Call send(intAddress2400dx, ":SOUR:VOLT:IMM 0", stat%) ' Source Current IMMediate 1 mA

Call send(intAddress2400dx, ":ARM:COUNT 1", stat%)
Call send(intAddress2400dx, ":TRIG:COUNT 1", stat%)

End Sub

Sub ReadCurr(theval!)
'lettura della corrente single shot

If bytTipoLetturaCorrente <> 1 Then Call Inizializza2400PerLetturaCorrente
Call send(intAddress2400dx, ":ABOR", stat%)
Call send(intAddress2400dx, ":SENS:CURR:DC:RANGE:AUTO 1", stat%)
Call send(intAddress2400dx, ":SENS:AVER:STAT OFF", stat%)
Call send(intAddress2400dx, ":MEAS:CURR:DC?", stat%)
rx$ = Space$(255)
Call enter(rx$, 255, Rl%, intAddress2400dx, stat%)
theval! = Val(rx$)
'setto lo strumento in lettura continua
Select Case theval!
       Case Is < 0.001
            Call send(intAddress2400dx, ":SENS:CURR:PROT:LEV 0.00105", stat%)
            Call send(intAddress2400dx, ":SENS:CURR:DC:RANGE 0.001", stat%)
       Case 0.001 To 0.01
            Call send(intAddress2400dx, ":SENS:CURR:PROT:LEV 0.0105", stat%)
            Call send(intAddress2400dx, ":SENS:CURR:DC:RANGE 0.01", stat%)
       Case 0.01 To 0.1
            Call send(intAddress2400dx, ":SENS:CURR:PROT:LEV 0.105", stat%)
            Call send(intAddress2400dx, ":SENS:CURR:DC:RANGE 0.1", stat%)
       Case 0.1 To 1
            Call send(intAddress2400dx, ":SENS:CURR:PROT:LEV 1.05", stat%)
            Call send(intAddress2400dx, ":SENS:CURR:DC:RANGE 1.05", stat%)
End Select
       
Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
'Call send(intAddress2400dx, ":OUTPUT ON", stat%)
Call send(intAddress2400dx, ":INIT", stat%)

End Sub


Sub ReadCurrCont(sngRangeCurr As Single, intDigit As Integer, bytAbilitazioneUscita As Byte)
'lettura della corrente in continua


        'setta lo strumento per misura continua di volt a corrente nulla
        Call initsystem(1, 0, 1, 0, 1)
        Call send(intAddress2400dx, ":SENS:FUNC 'CURR:DC'", stat%) ' Sense Voltage
        'Call send(intAddress2400dx, ":SENS:CURR:PROT " + Str(sngRangeCurr * 2), stat%)
        Call send(intAddress2400dx, ":SOUR:FUNC VOLT", stat%) ' Source Current IMMediate 0
        Call send(intAddress2400dx, ":SOUR:VOLT:IMM 0", stat%) ' Source Current IMMediate 0
        If sngRangeCurr = 0 Then
            Call send(intAddress2400dx, ":SENS:CURR:RANGE:AUTO 1", stat%)
        Else
            Call send(intAddress2400dx, ":SENS:CURR:PROT:LEV " + Str(sngRangeCurr * 2), stat%)
            Call send(intAddress2400dx, ":SENS:CURR:RANGE " + Str(sngRangeCurr), stat%)
        End If
        If intDigit = 0 Then
            Call send(intAddress2400dx, ":DISP:DIG 5", stat%)
        Else
            Call send(intAddress2400dx, ":DISP:DIG " + Str(intDigit), stat%)
        End If
        Call send(intAddress2400dx, ":ARM:COUN INF", stat%)
        If bytAbilitazioneUscita = 1 Then
           Call send(intAddress2400dx, ":OUTPUT ON", stat%)
           Call send(intAddress2400dx, ":INIT", stat%)
        Else
           Call send(intAddress2400dx, ":OUTPUT OFF", stat%)
        End If
       
End Sub


Sub initSource2400(intFrontRear As Integer)
'questa funzione ha il compito di inizializzare il 2400 che deve essere utilizzato come sorgente

Call send(intAddress2400sx, "*RST", stat%)
'selezione il pannello posteriore
If intFrontRear = 1 Then
   Call send(intAddress2400sx, ":SYST:FRSW REAR", stat%)
End If
'disabilito l 'opzione a 4 cavi
Call send(intAddress2400sx, ":SYST:RSEN 1", stat%)

End Sub

Sub SetSource2400(sngValoreDaSettare As Single, strTipoSource As String, strAbilitazioneUscita As String, sngCompliance As Single)
'Setto lo strumento per fornire una certa tensione o corrente in uscita
'se strTipoSource=V setto una tensione, se = I setto una corrente
'strAbilitazioneUscita  serve per sapere se abilito l'uscita immediatamente (C)o
'solo successivamente (O) con un altro comando
'strCompliance contiene il limite da passare allo strumento
Dim sngValoreSettato As Single
Dim strCompliance As String

'inizializzo
Call initSource2400(1)
strCompliance = Str(CSng(sngCompliance))
If strTipoSource = "V" Then
'alimento in tensione e limito in corrente
   Call send(intAddress2400sx, ":SENS:FUNC 'CURR:DC'", stat%)
    'Call send(intAddress2400sx, ":CONF:CURR", stat%)
   Call send(intAddress2400sx, ":SOUR:FUNC VOLT", stat%)
   Select Case sngValoreDaSettare
        Case Is < 2
             Call send(intAddress2400sx, ":SOUR:VOLT:RANGE 2", stat%)
        Case 2 To 20
             Call send(intAddress2400sx, ":SOUR:VOLT:RANGE 20", stat%)
        Case 20 To 200
             Call send(intAddress2400sx, ":SOUR:VOLT:RANGE 200", stat%)
        Case Is >= 200
             strRisposta = MsgBox("Errore:è stata programmata una tensione troppo alta", vbExclamation)
             Exit Sub
   End Select
   If (sngCompliance = 0) Or (sngCompliance < 0) Then
   'setto come default la corrente a 200 mA
      Call send(intAddress2400sx, ":SENS:CURR:RANGE:AUTO ON", stat%)
      Call send(intAddress2400sx, ":SENS:CURR:PROT:LEV 0.2", stat%)
   Else
       Call send(intAddress2400sx, ":SENS:CURR:PROT:LEV " + Str(sngCompliance), stat%)
       Call send(intAddress2400sx, ":SENS:CURR:RANGE " + Str(sngCompliance), stat%)
   End If
   Call send(intAddress2400sx, ":SOUR:VOLT:IMM" + Str(sngValoreDaSettare), stat%) ' Source Current IMMediate 1 mA
   Call send(intAddress2400sx, ":SOUR:DEL 0.001", stat%)
ElseIf (strTipoSource = "I") Then
  ' Call send(intAddress2400sx, ":SENS:FUNC 'VOLT:DC'", stat%)
   Call send(intAddress2400sx, ":SOUR:FUNC CURR", stat%)
   Call send(intAddress2400sx, ":SENS:FUNC 'VOLT:DC'", stat%)
   'Call send(intAddress2400sx, ":CONF:VOLT", stat%)
   Select Case sngValoreDaSettare
        Case Is < 0.001
             Call send(intAddress2400sx, ":SOUR:CURR:RANGE 0.01", stat%)
        Case 0.001 To 0.1
             Call send(intAddress2400sx, ":SOUR:CURR:RANGE 0.1", stat%)
        Case 0.1 To 1
             Call send(intAddress2400sx, ":SOUR:CURR:RANGE 1", stat%)
        Case Is > 1
             strRisposta = MsgBox("Errore: è stata programmata una corrente troppo alta", vbExclamation)
             Exit Sub
   End Select
   If (strCompliance = "0") Or (strCompliance = "") Then
   'setto come default la corrente a 200 mA
      Call send(intAddress2400sx, ":SENS:VOLT:RANGE:AUTO ON", stat%)
      Call send(intAddress2400sx, ":SENS:VOLT:PROT 50", stat%)
   Else
      Call send(intAddress2400sx, ":SENS:VOLT:PROT " + strCompliance, stat%)
      Call send(intAddress2400sx, ":SENS:VOLT:RANGE " + strCompliance, stat%)
   End If
   Call send(intAddress2400sx, ":SOUR:CURR:IMM " + Str(sngValoreDaSettare), stat%) ' Source Current IMMediate 1 mA
   Call send(intAddress2400sx, ":SOUR:DEL 0.001", stat%)
   
Else
   strRisposta = MsgBox("Errore nel settaggio dello strumento: che tipo di sorgente deve essere?")
   Exit Sub
End If
If strAbilitazioneUscita = "C" Then
   'abilito l'uscita solo se richiesto
      Call send(intAddress2400sx, ":ARM:COUN INF", stat%)
      Call send(intAddress2400sx, ":OUTP ON", stat%)
      Call send(intAddress2400sx, ":INIT", stat%)
End If
   
End Sub

Sub AbilitaUscitaSource()
'permette di abilitare l'uscita quando lo si desidera

Call send(intAddress2400sx, ":ARM:COUN INF", stat%)
Call send(intAddress2400sx, ":OUTP ON", stat%)
Call send(intAddress2400sx, ":INIT", stat%)

End Sub

Sub DisabilitaUscitaSource()

Call send(intAddress2400sx, ":OUTP OFF", stat%)

End Sub
