Attribute VB_Name = "Advantek"
'***************************************************************************
' Module Name: DRIVER.BAS
' Purpose: the declaration of functions, data structures, status codes,
'          constants, and messages
' Version: 3.01
' Date: 04/16/1998
' Copyright (c) 1996 Advantech Corp. Ltd.
' All rights reserved.
'****************************************************************************

'****************************************************************************
'    Constant Definition
'****************************************************************************
Global Const MaxEntries = 255
Global DeviceHandle As Long
Global devicelist(0 To MaxEntries) As PT_DEVLIST
Global SubDevicelist(0 To MaxEntries) As PT_DEVLIST
Global errcde As Long
Global szErrMsg As String * 80
Global ioportmatrix(0 To 9) As Long 'contiene gli indirizzi I/O delle schede di IO presenti
Global dwDeviceNum As Long
Global hiomatrix(0 To 9) As Long 'contiene gli handler controller io PCL 722

Global Const MaxDev = 255 ' max. # of devices
Global Const MaxDevNameLen = 49           ' original is 64; max lenght of device name
Global Const MaxGroup = 6
Global Const MaxPort = 3
Global Const MaxszErrMsgLen = 80
Global Const MAX_DEVICE_NAME_LEN = 64
Global Const MAX_DRIVER_NAME_LEN = 16
Global Const MAX_DAUGHTER_NUM = 16
Global Const MAX_DIO_PORT = 48
Global Const MAX_AO_RANGE = 16
Global nOutEntries As Integer

Type PT_DEVLIST
    dwDeviceNum  As Long
    szDeviceName(0 To 49) As Byte
    nNumOfSubdevices As Integer
End Type

'**************************************************************************
'    Function Declaration for ADSAPI32
'**************************************************************************
Declare Function DRV_DeviceGetNumOfList Lib "adsapi32.dll" (NumOfDevices As Integer) As Long
Declare Function DRV_DeviceGetList Lib "adsapi32.dll" (ByVal devicelist As Long, ByVal MaxEntries As Integer, nOutEntries As Integer) As Long
Declare Function DRV_DeviceGetSubList Lib "adsapi32.dll" (ByVal DeviceNum As Long, ByVal SubDevList As Long, ByVal MaxEntries As Integer, nOutEntries As Integer) As Long
Declare Function DRV_DeviceOpen Lib "adsapi32.dll" (ByVal DeviceNum As Long, DriverHandle As Long) As Long
Declare Function DRV_DeviceClose Lib "adsapi32.dll" (DriverHandle As Long) As Long
Declare Function DRV_GetAddress Lib "adsapi32.dll" (lpVoid As Any) As Long
Declare Sub DRV_GetErrorMessage Lib "adsapi32.dll" (ByVal lError As Long, ByVal lpszszErrMsg As String)
    
' Direct I/O Functions List
Declare Function DRV_outp Lib "adsapi32.dll" (ByVal DeviceNum As Long, ByVal port As Integer, ByVal ByteData As Long) As Long
Declare Function DRV_outpw Lib "adsapi32.dll" (ByVal DeviceNum As Long, ByVal port As Integer, ByVal ByteData As Long) As Long
Declare Function DRV_inp Lib "adsapi32.dll" (ByVal DeviceNum As Long, ByVal port As Integer, ByteData As Long) As Long
Declare Function DRV_inpw Lib "adsapi32.dll" (ByVal DeviceNum As Long, ByVal port As Integer, ByteData As Long) As Long
Declare Function DRV_DioReadPortByte Lib "adsapi32.dll" (ByVal DriverHandle As Long, DioReadPortByte As PT_DioReadPortByte) As Long
Declare Function DRV_DioWritePortByte Lib "adsapi32.dll" (ByVal DriverHandle As Long, DioWritePortByte As PT_DioWritePortByte) As Long
Declare Function DRV_WritePortByte Lib "adsapi32.dll" (ByVal DriverHandle As Long, WritePortByte As PT_WritePortByte) As Long
Declare Function DRV_ReadPortByte Lib "adsapi32.dll" (ByVal DriverHandle As Long, ReadPortByte As PT_ReadPortByte) As Long
Declare Function DRV_AOVoltageOut Lib "adsapi32.dll" (ByVal DriverHandle As Long, AOVoltageOut As PT_AOVoltageOut) As Long
Declare Function DRV_AOConfig Lib "adsapi32.dll" (ByVal DriverHandle As Long, AOConfig As PT_AOConfig) As Long
Declare Function DRV_AIConfig Lib "adsapi32.dll" (ByVal DriverHandle As Long, AOConfig As PT_AOConfig) As Long
Declare Function DRV_AIVoltageIn Lib "adsapi32.dll" (ByVal DriverHandle As Long, AIVoltageIn As PT_AIVoltageIn) As Long


Type PT_AIConfig
    DasChan As Integer
    DasGain As Integer
End Type

Type PT_AIVoltageIn
    chan     As Integer
    gain     As Integer
    TrigMode As Integer
    voltage  As Long      ' FLOAT far *voltage
End Type

Type PT_DioWritePortByte
    port  As Integer
    Mask  As Integer
    state As Integer
End Type

Type PT_DioReadPortByte
    port As Integer
    value As Long         ' USHORT far *value
End Type

Type PT_WritePortByte
    port     As Integer
    ByteData As Integer
End Type

Type PT_ReadPortByte
    port     As Integer
    ByteData As Long      ' USHORT far *ByteData
End Type

Type PT_AOConfig
    chan     As Integer
    RefSrc   As Integer
    MaxValue As Single
    MinValue As Single
End Type

Type PT_AOVoltageOut
    chan        As Integer
    OutputValue As Single
End Type


Global ptiow As PT_WritePortByte
Global ptior As PT_ReadPortByte
Global ptdiow As PT_DioWritePortByte
Global ptdior As PT_DioReadPortByte

'record generico di porta: e' un byte espanso a 8 byte:
'bit a 0 -> Chr$(0)
'bit a 1 -> Chr$(1)

Type prt
    tp As String * 8
End Type
Type dv0                           'Struttura relativa alla scheda PCL-722 per descrivere i vari segnali utilizzati

    I_Alimentazione_24 As Byte                           'P 0.0 Ch 0
    I_Rele_Emergenza As Byte                             'P 0.1 Ch 0
    I_Chiave_Abilitazione_Laser As Byte                  'P 0.2 Ch 0
    I_Work_Standby_Man As Byte                           'P 0.3 Ch 0
    I_Rele_Comm_Pompa_Refrigerante  As Byte              'P 0.4 Ch 0
    I_Puntali_Alti_Probe2 As Byte                        'P 0.5 Ch 0
    I_Comparatore_1_Micro_ As Byte                       'P 0.6 Ch 0
    I_Comparatore_2_Micro_ As Byte                       'P 0.7 Ch 0
    I_Livello_Refrigerante As Byte                       'P 1.0 Ch 0
    I_Flusso_Refrigerante As Byte                        'P 1.1 Ch 0
    I_Clickson_Laser As Byte                             'P 1.2 Ch 0
    I_Interlock_Qswitch As Byte                          'P 1.3 Ch 0
    I_Termico_Pompa_Refrigerante As Byte                 'P 1.4 Ch 0
    I_Rele_Rf_25W As Byte                                'P 1.5 Ch 0
    I_Temperatura_Mod_RF As Byte                         'P 1.6 Ch 0
    I_Puntali_Rosa_Alti As Byte                          'P 1.7 Ch 0
    I_Blocco_Chiller As Byte                             'P 2.0 Ch 0
    I_Puntali_Alti_Probe1 As Byte                        'P 2.1 Ch 0
    I_Switch_Portello_Ant_Open As Byte                   'P 2.2 Ch 0
    I_Switch_Portello_Ant_Mot_Open As Byte               'P 2.3 Ch 0
    I_Switch_Portello_Sup_Open As Byte                   'P 2.4 Ch 0
    I_Inibizione_Azionamenti As Byte                     'P 2.5 Ch 0
    I_Contaore_Laser_On As Byte                          'P 2.6 Ch 0
    I_Sensore_Vuoto_Tavola As Byte                       'P 2.7 Ch 0
    O_System_On As Byte                                  'P 0.0 Ch 1
    O_Abilitazione_Chiller As Byte                       'P 0.1 Ch 1
    O_RF_On_12V As Byte                                  'P 0.2 Ch 1
    O_24V_Cond_A_O As Byte                               'P 0.3 Ch 1
    O_24V_Cond_B_O As Byte                               'P 0.4 Ch 1
    O_Alim_Sist_Misura As Byte                           'P 0.5 Ch 1
    O_24V_Cond_C_I As Byte                               'P 0.6 Ch 1
    O_PasswordOK As Byte                                 'P 0.7 Ch 1
    O_Rele_Laser_HeNe As Byte                            'P 1.0 Ch 1
    O_Rele_Illuminazione As Byte                         'P 1.1 Ch 1
    O_Rele_Comm_Pompa_Ref As Byte                        'P 1.2 Ch 1
    O_Lamp_On As Byte                                    'P 1.3 Ch 1
    O_Rele_Work_Standby As Byte                          'P 1.4 Ch 1
    O_Pressione_Atmosferica_Pressione_Regolata As Byte   'P 1.5 Ch 1
    O_Chiamata_Operatore As Byte                         'P 1.6 Ch 1
    O_Allarme_Macchina As Byte                           'P 1.7 Ch 1
    O_Puntali_Contattazione As Byte                               'P 2.0 Ch 1
    O_AbbProbe_1 As Byte                                 'P 2.1 Ch 1
    O_AbbProbe_2 As Byte                                 'P 2.2 Ch 1
    O_Filtro_1 As Byte                                   'P 2.3 Ch 1
    O_Filtro_2 As Byte                                   'P 2.4 Ch 1
    O_Filtro_3 As Byte                                   'P 2.5 Ch 1
    O_Filtro_4 As Byte                                   'P 2.6 Ch 1
    O_Misura_Power_Laser As Byte                         'P 2.7 Ch 1
    v49 As Byte                          'P 0.0 Ch 2
    v50 As Byte                          'P 0.1 Ch 2
    v51 As Byte                          'P 0.2 Ch 2
    v52 As Byte                          'P 0.3 Ch 2
    v53 As Byte                 'P 0.4 Ch 2
    v54 As Byte                 'P 0.5 Ch 2
    v55 As Byte                 'P 0.6 Ch 2
    v56 As Byte                 'P 0.7 Ch 2
    v57 As Byte                                          'P 1.0 Ch 2
    v58 As Byte                                          'P 1.1 Ch 2
    v59 As Byte                                          'P 1.2 Ch 2
    v60 As Byte  'P 1.3 Ch 2
    v61 As Byte  'P 1.4 Ch 2
    v62 As Byte  'P 1.5 Ch 2
    v63 As Byte  'P 1.6 Ch 2
    v64 As Byte  'P 1.7 Ch 2
    v65 As Byte  'P 2.0 Ch 2
    v66 As Byte  'P 2.1 Ch 2
    v67 As Byte  'P 2.2 Ch 2
    v68 As Byte  'P 2.3 Ch 2
    v69 As Byte  'P 2.4 Ch 2
    v70 As Byte  'P 2.5 Ch 2
    v71 As Byte  'P 2.6 Ch 2
    v72 As Byte  'P 2.7 Ch 2
    v73 As Byte                      'P 0.0 Ch 3
    v74 As Byte                      'P 0.1 Ch 3
    v75 As Byte                      'P 0.2 Ch 3
    v76 As Byte                      'P 0.3 Ch 3
    v77 As Byte                      'P 0.4 Ch 3
    v78 As Byte                      'P 0.5 Ch 3
    v79 As Byte                      'P 0.6 Ch 3
    v80 As Byte                      'P 0.7 Ch 3
    v81 As Byte                      'P 1.0 Ch 3
    v82 As Byte                      'P 1.1 Ch 3
    v83 As Byte                      'P 1.2 Ch 3
    v84 As Byte                      'P 1.3 Ch 3
    v85 As Byte  'P 1.4 Ch 3
    v86 As Byte  'P 1.5 Ch 3
    v87 As Byte  'P 1.6 Ch 3
    v88 As Byte  'P 1.7 Ch 3
    v89 As Byte  'P 2.0 Ch 3
    v90 As Byte  'P 2.1 Ch 3
    v91 As Byte  'P 2.2 Ch 3
    v92 As Byte  'P 2.3 Ch 3
    v93 As Byte  'P 2.4 Ch 3
    v94 As Byte  'P 2.5 Ch 3
    v95 As Byte  'P 2.6 Ch 3
    v96 As Byte  'P 2.7 Ch 3
    v97 As Byte  'P 0.0 Ch 4
    v98 As Byte  'P 0.1 Ch 4
    v99 As Byte  'P 0.2 Ch 4
    v100 As Byte  'P 0.3 Ch 4
    v101 As Byte 'P 0.4 Ch 4
    v102 As Byte 'P 0.5 Ch 4
    v103 As Byte 'P 0.6 Ch 4
    v104 As Byte 'P 0.7 Ch 4
    v105 As Byte 'P 1.0 Ch 4
    v106 As Byte 'P 1.1 Ch 4
    v107 As Byte 'P 1.2 Ch 4
    v108 As Byte 'P 1.3 Ch 4
    v109 As Byte 'P 1.4 Ch 4
    v110 As Byte 'P 1.5 Ch 4
    v111 As Byte 'P 1.6 Ch 4
    v112 As Byte 'P 1.7 Ch 4
    v113 As Byte 'P 2.0 Ch 4
    v114 As Byte 'P 2.1 Ch 4
    v115 As Byte 'P 2.2 Ch 4
    v116 As Byte 'P 2.3 Ch 4
    v117 As Byte 'P 2.4 Ch 4
    v118 As Byte 'P 2.5 Ch 4
    v119 As Byte 'P 2.6 Ch 4
    v120 As Byte 'P 2.7 Ch 4
    v121 As Byte 'P 0.0 Ch 5
    v122 As Byte 'P 0.1 Ch 5
    v123 As Byte 'P 0.2 Ch 5
    v124 As Byte 'P 0.3 Ch 5
    v125 As Byte 'P 0.4 Ch 5
    v126 As Byte 'P 0.5 Ch 5
    v127 As Byte 'P 0.6 Ch 5
    v128 As Byte 'P 0.7 Ch 5
    v129 As Byte 'P 1.0 Ch 5
    v130 As Byte 'P 1.1 Ch 5
    v131 As Byte 'P 1.2 Ch 5
    v132 As Byte 'P 1.3 Ch 5
    v133 As Byte 'P 1.4 Ch 5
    v134 As Byte 'P 1.5 Ch 5
    v135 As Byte 'P 1.6 Ch 5
    v136 As Byte 'P 1.7 Ch 5
    v137 As Byte 'P 2.0 Ch 5
    v138 As Byte 'P 2.1 Ch 5
    v139 As Byte 'P 2.2 Ch 5
    I_Tavola_A_NReady  As Byte  'P 2.3 Ch 5
    I_Tavola_B_NReady As Byte   'P 2.4 Ch 5
    I_PLC_NReady As Byte        'P 2.5 Ch 5
    I_NScarto_Pezzo As Byte     'P 2.6 Ch 5
    I_NScarto_Completo As Byte  'P 2.7 Ch 5


End Type
     
Public d0 As dv0                   'Variabile per riferirsi ai segnali della PCL-722
Public lpDioWritePortByte As PT_DioWritePortByte 'Struttura contenente i valori della porta su cui scrivo,della maschera e il valore che scrivo
Public lpWritePortByte As PT_WritePortByte       'struttura contenente i riferimenti alla porta su cui scrivo
Public lpAOConfig As PT_AOConfig
Public lpAOVoltageOut As PT_AOVoltageOut
Public DeviceInstalled() As DeviceInUseOnPC
Public DriverHandle As Long
Public NumOfDevices As Integer
Public intAddressDAC As Integer
Public sngPressioneMax As Single
Public strRisposta As Integer
Public sngValueMax As Single     'Valore massimo di una variabile che deve essere poi inviata in uscita alla scheda PCL818L
Public NumOfDeviceInUse As Long
Public IOPortAddress() As Integer       'Contiene gli indirizzi base delle device installate

Public dblTensioneLetta As Double 'valore di tensione letto
Public lpTensioneInIngresso As PT_AIVoltageIn
Public lpByteInIngresso As PT_ReadPortByte
Public lngByteLow As Long
Public lngByteHigh As Long
Public lngValoreLetto As Long

Type DeviceInUseOnPC               'Struttura che descrive le devices in uso
     strName As String
     intnum As Integer
End Type

'device generico
Type gendev
    prt(0 To 17) As String * 8
End Type

Public gd As gendev
Sub expander(bv%, st$, pn%)
theindex% = pn% * 8
st$ = ""
For i% = 0 To 7
    'lo zero logico e' un valore di tensione alto
    If Frmports.Chkbit(theindex% + i%).Enabled = False Then Frmports.Chkbit(theindex% + i%).Enabled = True
    If (bv% And (2 ^ i%)) > 0 Then
        st$ = st$ + Chr$(1)
        If Frmports.Visible = True Then
            Frmports.Chkbit(theindex% + i%).value = 1
        End If
    Else
        st$ = st$ + Chr$(0)
        If Frmports.Visible = True Then
            Frmports.Chkbit(theindex% + i%).value = 0
        End If
    End If
Next i%
End Sub
Sub openIO(findstr$, nn%, dn%)
  'cerca e apre la schede di IO specificata in findstr$
  
  Dim gnNumOfDevices As Integer
  Dim nOutEntries As Integer
  Dim i, ii As Integer
  Dim tempStr As String

    ' Add type of PC Laboratory Card
    tt& = DRV_GetAddress(devicelist(0))
    errcde = DRV_DeviceGetList(tt&, MaxEntries, nOutEntries)
    If (errcde <> 0) Then
        DRV_GetErrorMessage errcde, szErrMsg
        Call updatelog(szErrMsg, 1)
        Exit Sub
    End If

    errcde = DRV_DeviceGetNumOfList(gnNumOfDevices)
    If (errcde <> 0) Then
        DRV_GetErrorMessage errcde, szErrMsg
        Call updatelog(szErrMsg, 1)
        Exit Sub
    End If
    ok% = 0
    For i = 0 To (gnNumOfDevices - 1)
        tempStr = ""
        For ii = 0 To MaxDevNameLen
            tempStr = tempStr + Chr(devicelist(i).szDeviceName(ii))
        Next ii
        tempStr$ = UCase$(tempStr$)
        fd% = InStr(tempStr$, findstr$)
        If fd% > 0 Then
            fn% = fn% + 1
        End If
        If fn% = nn% And fd% > 0 Then
            ok% = 1
            inou% = InStr(tempStr$, "I/O=")
            If inou% > 0 Then
                tempStr$ = Mid$(tempStr$, inou% + 4)
                theport& = Val("&H" + tempStr$)
                ioportmatrix(dn%) = theport&
                iii& = dn%
                errcde = DRV_DeviceOpen(iii&, dh&)
                If (errcde <> 0) Then
                    DRV_GetErrorMessage errcde, szErrMsg
                    hiomatrix(dn%) = -1
                    ioportmatrix(dn%) = -1
                    c0% = InStr(szErrMsg, Chr$(0))
                    If c0% > 0 Then errst$ = Left$(szErrMsg, c0% - 1)
                    Call updatelog(mss$(123) + Str(theport&) + ":" + errst$, 1)
                Else
                    Call updatelog("I/O " + findstr$ + Str$(fn% - 1) + mss$(124) + Str(theport&), 0)
                    hiomatrix(dn%) = dh&
                    od$ = Trim$(Str(fn% - 1))
                    ex$ = Dir(thepath$ + findstr$ + od$ + ".ini")
                    'inizializzazione IO controller
                    If ex$ <> "" Then Call initio(dn%, findstr$, od$)
                End If
            End If
        End If
    Next i
    If ok% = 0 Then Call updatelog("I/O (" + findstr$ + ")" + mss$(125), 1)
End Sub
Sub getoffset(dn%, ByRef bt2 As Byte, ByVal off&)
Select Case dn%
Case 0
    ad1& = DRV_GetAddress(d0)
End Select
ad2& = DRV_GetAddress(bt2)
off& = ad2& - ad1&
End Sub
Sub setoutput(dn%, ByRef bt As Byte)
Select Case dn%
Case 0
    ad1& = DRV_GetAddress(d0)
End Select
ad2& = DRV_GetAddress(bt)
off& = ad2& - ad1&
pn% = Int(off& / 8)
bm% = off& Mod 8
bm% = 2 ^ bm%
Call setbit(dn%, pn%, bm%)
End Sub
Sub resetoutput(dn%, ByRef bt As Byte)
Select Case dn%
Case 0
    ad1& = DRV_GetAddress(d0)
End Select
ad2& = DRV_GetAddress(bt)
off& = ad2& - ad1&
pn% = Int(off& / 8)
bm% = off& Mod 8
bm% = 2 ^ bm%
Call resetbit(dn%, pn%, bm%)
End Sub

Sub scanio()
iiodev1$ = Trim$(UCase$(iodev$))
If Right$(iodev1$, 1) <> "," Then iodev1$ = iodev1$ + ","
iiodev2$ = ""
dn% = 0
Do
    cm% = InStr(iiodev1$, ",")
    If cm% > 0 Then
        dev$ = Trim$(Left$(iiodev1$, cm% - 1))
        iiodev1$ = Mid$(iiodev1$, cm% + 1)
        iiodev2$ = iiodev2$ + dev$ + " "
        If dev$ <> "X" Then
            fd% = 0
            cn% = 0
            Do
                fd% = InStr(fd% + 1, iiodev2$, dev$ + " ")
                If fd% > 0 Then cn% = cn% + 1
            Loop While fd% > 0
            If hiomatrix(dn%) = -1 Then
                Call openIO(dev$, cn%, dn%)
            End If
            dn% = dn% + 1
        End If
    End If
Loop While cm% > 0
Call setfreqda(1, 1000)
Call setda(1, 7, ilasmax!)
End Sub

Sub ReadAD(dn%, ch%, readval%)
Dim vp%
If ioportmatrix(dn%) = -1 Then Exit Sub
If hiomatrix(dn%) = -1 Then Exit Sub
ptior.port = ioportmatrix(dn%) + 8
ptior.ByteData = DRV_GetAddress(vp%)
errcde = DRV_ReadPortByte(hiomatrix(dn%), ptior)
If errcde <> 0 Then GoTo errorcode
If (vp% And 128) = 0 Then
    If ch% >= 0 Then
        ptior.port = ioportmatrix(dn%)
        ptior.ByteData = DRV_GetAddress(vp%)
    End If
    ptior.port = ioportmatrix(dn%)
    ptior.ByteData = DRV_GetAddress(vp%)
    errcde = DRV_ReadPortByte(hiomatrix(dn%), ptior)
    If errcde <> 0 Then GoTo errorcode
    thech% = vp% And 15
    ch% = thech%
    val1% = Int(vp% / 16)
    ptior.port = ioportmatrix(dn%) + 1
    ptior.ByteData = DRV_GetAddress(vp%)
    errcde = DRV_ReadPortByte(hiomatrix(dn%), ptior)
    If errcde <> 0 Then GoTo errorcode
    val2% = vp%
    val3% = val1% + (16 * val2%)
    readval% = val3%
    tval3$ = Trim(Str$(val3%))
    Call dav(10, 20, val3%, VT$)
    Select Case thech%
    Case 0
        If Frmports.Visible = True Then
            If Frmports.Lblch0.Caption <> tval3$ Then
                Frmports.Lblch0.Caption = tval3$
            End If
        End If
        'If FrmEuro.Lblc0.Caption <> vt$ Then FrmEuro.Lblc0.Caption = vt$
    Case 1
        If Frmports.Visible = True Then
            If Frmports.Lblch1.Caption <> tval3$ Then
                Frmports.Lblch1.Caption = tval3$
            End If
        End If
        'If FrmEuro.Lblc1.Caption <> vt$ Then FrmEuro.Lblc1.Caption = vt$
    Case 2
        If Frmports.Visible = True Then
            If Frmports.Lblch2.Caption <> tval3$ Then
                Frmports.Lblch2.Caption = tval3$
            End If
        End If
        'If FrmEuro.Lblc1.Caption <> vt$ Then FrmEuro.Lblc1.Caption = vt$
    Case 3
        If Frmports.Visible = True Then
            If Frmports.Lblch3.Caption <> tval3$ Then
                Frmports.Lblch3.Caption = tval3$
            End If
        End If
        
    End Select
End If
'sw trigger
ptiow.port = ioportmatrix(dn%)
ptiow.ByteData = 0
errcde = DRV_WritePortByte(hiomatrix(dn%), ptiow)
If errcde <> 0 Then GoTo errorcode
Exit Sub
errorcode:
Call updatelog(mss$(126), 1)
End Sub
Sub dav(offset!, range!, advalue%, VT$)
voltage! = ((range! / 4095) * advalue%) - offset!
voltint% = CInt(voltage! * 10)
voltage! = voltint% / 10
VT$ = Trim$(Str$(voltage!))
If InStr(VT$, ".") <= 0 Then VT$ = VT$ + ".0"
End Sub
Sub setda(dn%, Vval!, Vmax!)
'Questa funziona va ad inviare in uscita alla porta analogica della PCL818L
'il valore desiderato; convertirà quella desiderato nel corrispondente range
'della porta di uscita: Vmax!= valore massimo dell'uscita

Vval! = Abs(Vval!)
If Vval! > Vmax! Then Vval! = Vmax!
dtcurr& = timeGetTime&()
If ioportmatrix(dn%) = -1 Then Exit Sub
If hiomatrix(dn%) = -1 Then Exit Sub
fac! = Vval! / Vmax!
If fac! > 1 Then fac! = 1
adval3% = Int(4095 * fac!)
If adval3% > 4095 Then adval3% = 4095
adval1% = (adval3% And 15) * 16
adval2% = (adval3% And 4080) / 16
ptiow.port = ioportmatrix(dn%) + 4
ptiow.ByteData = adval1%
errcde = DRV_WritePortByte(hiomatrix(dn%), ptiow)
ptiow.port = ioportmatrix(dn%) + 5
ptiow.ByteData = adval2%
errcde = DRV_WritePortByte(hiomatrix(dn%), ptiow)
dif! = Abs(Vval! - Val(lastcurr$))
If (dn% = 1) Then lastcurr$ = Trim$(Str(Vval!))    'devo salvare il valore dell'ultima corrente settata solo quando una dn%=1 cioe'
If (dif! = 0) Or (Vval! = 7) Then Exit Sub         'la scheda PCL818L che gestisce la corrente del laser; quando uso quella del DPI
If dif! > 3 Then                                   'non devo aggiornare questo valore per non andare poi a modificare la label della corrente laser
    Call Delay(3000)
    Exit Sub
End If
Call Delay(500)
End Sub
Sub readdevice(bindex%, portmin%, portmax%)
    'legge il device indicato da bindex% da portmin% a portmax% e mette le letture in un record
    If hiomatrix(bindex%) = -1 Then Exit Sub
    Dim vp%
    For i% = portmin% To portmax%
        ptdior.port = i%
        ptdior.value = DRV_GetAddress(vp%)
        errcde = DRV_DioReadPortByte(hiomatrix(bindex%), ptdior)
        If (errcde <> 0) Then
            DRV_GetErrorMessage errcde, szErrMsg
            c0% = InStr(szErrMsg, Chr$(0))
            If c0% > 0 Then errst$ = Left$(szErrMsg, c0% - 1)
            Call updatelog(mss$(127) + Str(bindex%) + ":" + errst$, 1)
            vp% = -1
        End If
        Call expander(vp%, st$, i%)
        gd.prt(i%) = st$
    Next i%
    Select Case bindex%
    Case 0
        'PCL 722
        ll& = Len(gd)
        Call CopyMemory(d0, gd, ll&)
    End Select
End Sub
Sub initio(dn%, findstr$, od$)
un% = FreeFile
Open thepath$ + findstr$ + od$ + ".ini" For Input As #un%
er% = 0
Do While Not EOF(un%)
        Line Input #un%, cl$
        cl$ = Trim$(cl$)
        dio% = 0
        Select Case UCase$(Left$(cl$, 1))
        Case Is = "D"
            cl$ = Trim$(Mid$(cl$, 2))
            dio% = 1
        Case Is = "P"
            dio% = 0
            cl$ = Trim$(Mid$(cl$, 2))
        End Select
        If Left$(cl$, 1) <> "'" And cl$ <> "" Then
            bl% = InStr(cl$, " ")
            If bl% > 0 Then
                pn% = Val(Left$(cl$, bl%))
                bn% = Val(Mid$(cl$, bl%))
                If dio% = 1 Then
                    ptdiow.port = pn%
                    ptdiow.Mask = 255
                    ptdiow.state = bn%
                    errcde = DRV_DioWritePortByte(hiomatrix(dn%), ptdiow)
                Else
                    If pn% < 32 Then
                        ptiow.port = pn% + ioportmatrix(dn%)
                    Else
                        ptiow.port = pn%
                    End If
                    ptiow.ByteData = bn%
                    errcde = DRV_WritePortByte(hiomatrix(dn%), ptiow)
                End If
                If (errcde <> 0) Then
                    DRV_GetErrorMessage errcde, szErrMsg
                    c0% = InStr(szErrMsg, Chr$(0))
                    If c0% > 0 Then errst$ = Left$(szErrMsg, c0% - 1)
                    Call updatelog(mss$(128) + findstr$ + " " + od$ + ":" + errst$, 1)
                End If
            End If
        End If
Loop
Close #un%
If er% = 0 Then Call updatelog(findstr$ + " " + od$ + mss$(129), 0)
End Sub
Sub biton(devn%, thebit As Byte)
Select Case DevNum%
Case 0
    ad1& = DRV_GetAddress(d0)
    ad2& = DRV_GetAddress(thebit)
    theoffset& = ad2& - ad1&
    pn% = Int(theoffset& / 8)
    bn% = theoffset& Mod 8
    bm% = 2 ^ bn%
    Call setbit(0, pn%, bm%)
End Select
End Sub
Sub bitoff(devn%, thebit As Byte)
Select Case DevNum%
Case 0
    ad1& = DRV_GetAddress(d0)
    ad2& = DRV_GetAddress(thebit)
    theoffset& = ad2& - ad1&
    pn% = Int(theoffset& / 8)
    bn% = theoffset& Mod 8
    bm% = 2 ^ bn%
    Call resetbit(0, pn%, bm%)
End Select
End Sub
Sub setbit(dn%, pn%, bm%)
'resetta il o i bit corrispondenti a bm% sulla porta pn% del device dn%
If hiomatrix(dn%) = -1 Then Exit Sub
ptdiow.port = pn%
ptdiow.Mask = bm%
ptdiow.state = 255
errcde = DRV_DioWritePortByte(hiomatrix(dn%), ptdiow)
If (errcde <> 0) Then
        DRV_GetErrorMessage errcde, szErrMsg
        c0% = InStr(szErrMsg, Chr$(0))
        If c0% > 0 Then errst$ = Left$(szErrMsg, c0% - 1)
        Call updatelog(mss$(130) + errst$, 1)
End If
End Sub
Sub resetbit(dn%, pn%, bm%)
'resetta il o i bit corrispondenti a bm% sulla porta pn% del device dn%
If hiomatrix(dn%) = -1 Then Exit Sub
ptdiow.port = pn%
ptdiow.Mask = bm%
ptdiow.state = 0
errcde = DRV_DioWritePortByte(hiomatrix(dn%), ptdiow)
If (errcde <> 0) Then
        DRV_GetErrorMessage errcde, szErrMsg
        c0% = InStr(szErrMsg, Chr$(0))
        If c0% > 0 Then errst$ = Left$(szErrMsg, c0% - 1)
        Call updatelog(mss$(130) + errst$, 1)
End If
End Sub
Sub setprt(dn%, pn%, bm%, pw%)
'scrive il byte pw% combinato alla maschera bm% sulla porta pn% del device dn%
'serve per settare/resettare piu' bit contemporaneamente
If hiomatrix(dn%) = -1 Then Exit Sub
ptdiow.port = pn%
ptdiow.Mask = bm%
ptdiow.state = pw%
errcde = DRV_DioWritePortByte(hiomatrix(dn%), ptdiow)
If (errcde <> 0) Then
        DRV_GetErrorMessage errcde, szErrMsg
        c0% = InStr(szErrMsg, Chr$(0))
        If c0% > 0 Then errst$ = Left$(szErrMsg, c0% - 1)
        Call updatelog(mss$(130) + errst$, 1)
End If
End Sub

Function LockBoa(StatoLock As Integer)
    'Call readdevice(0, 0, 5)
    'Call Delay(1000)
    'If AirFOk = True Then
        'If StatoLock = 0 Then
            'Call resetoutput(0, d0.O_Rele_Tavola_A_Fwd)
'            Call Delay(1000)
 '           Call galilcmd("MOZ", res$, ermsg$)
            '''mario''' FrmEuro.ImgStBoaL.Visible = True
            '''mario''' FrmEuro.ImgStBoaU.Visible = False
  '          Call Delay(1000)
   '         BoaSTLO = True
    '    End If
     '   If StatoLock = 1 Then
      '      Call galilcmd("SHZ", res$, ermsg$)
       '     Call Delay(1000)
        '    Call setoutput(0, d0.O_Rele_Tavola_A_Fwd)
            '''mario''' FrmEuro.ImgStBoaL.Visible = False
            '''mario''' FrmEuro.ImgStBoaU.Visible = True
         '   Call Delay(1000)
          '  BoaSTLO = False
'        End If
 '   End If
  '      If StatoLock = 2 Then
   '         Call resetoutput(0, d0.O_Rele_Tavola_A_Fwd)
    '        Call galilcmd("MOZ", res$, ermsg$)
     '   End If
End Function

Function SelectDevice(strDeviceName As String) As Long
'Questa funzione ha il compito di selezionare la device desiderata; prende in
'ingresso il nome della device e restituisce il suo numero come riferimento per
'l'apertura
Dim intCont As Integer
Dim lngDeviceInUse As Long

For intCont = 0 To NumOfDevices                'Vado a selezionare la device su cui voglio lavorare
    If (InStr(DeviceInstalled(intCont).strName, strNameDevice) <> 0) Then
       lngDeviceInUse = DeviceInstalled(intCont).intnum
    End If
Next intCont
SelectDevice = lngDeviceInUse
NumOfDeviceInUse = lngDeviceInUse

End Function

Function OpenDevice(lngNumDeviceInUse As Long) As Long
'Prende in ingresso il numero di una device e la va ad aprire; restituisce
'Handle della device stessa

  
errcde = DRV_DeviceOpen(lngNumDeviceInUse, DriverHandle)     'Apro la device desiderata
If (errcde <> 0) Then
   DRV_GetErrorMessage errcde, szErrMsg
   strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
   Exit Function
End If
OpenDevice = DriverHandle

End Function

Sub CloseDevice(lngDriverHandle As Long)
'Provvede a chiudere la Device desiderata

errcde = DRV_DeviceClose(lngDriverHandle)
If (errcde <> 0) Then
   DRV_GetErrorMessage errcde, szErrMsg
   strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
   Exit Sub
End If

End Sub

Function HiByte(intValueToHi As Integer) As Integer
'Prende in ingresso un intero e in uscita restituisce il corrispondente byte alto
'se è negativo ricavo i 7 bit più alti con una maschera, shifto verso destra di
' 8 posizioni ed aggiungo 128 che mi rappresenta il meno del numero
Dim intTemp As Integer

If (intValueToHi < 0) Then
   intTemp = (intValueToHi And 32512)
   intTemp = intTemp \ 256
   intTemp = intTemp + 128
Else
   intTemp = (intValueToHi And 32512)
   intTemp = intTemp \ 256
End If
HiByte = intTemp

End Function

Function LoByte(intValueToLo As Integer) As Integer
'Prende in ingresso un intero e restituisce il corrispondente byte basso
Dim intTemp As Integer

intTemp = (intValueToLo And 255)
LoByte = intTemp

End Function

Function WriteAnalogOutSon(sngValueOutPut As Single, AddressDAC As Integer, intDeviceNum As Integer) As Integer
'Prende in ingresso il valore a cui portare l'uscita analogica della scheda espansione
'e l'indirizzo del DAC da usare; in uscita restituisce il valore inviata all'output
'digitale; questa funzione scrive sulla porta di uscite digitali
Dim intTemp As Integer
Dim sngTemp As Single

DriverHandle = hiomatrix(intDeviceNum)
sngTemp = -sngValueOutPut
intTemp = Round((sngTemp + 10) * 2047 / 10)
intTemp = intTemp * 16

lpDioWritePortByte.Mask = 255         'Seleziono i bit da settare

lpDioWritePortByte.port = 0           'Prima scrivo il byte alti
lpDioWritePortByte.state = HiByte(intTemp)
errcde = DRV_DioWritePortByte(DriverHandle, lpDioWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
End If

lpDioWritePortByte.port = 1          'Devo abilitare i latch del DAC; scrivo nei bit alti
lpDioWritePortByte.state = 40 + AddressDAC
errcde = DRV_DioWritePortByte(DriverHandle, lpDioWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
  End If
  
lpDioWritePortByte.state = 56 + AddressDAC    'Disabilito i latch e congelo i valori memorizzati
errcde = DRV_DioWritePortByte(DriverHandle, lpDioWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
  End If
  
lpDioWritePortByte.port = 0          'Vado a scrivere il byte basso
lpDioWritePortByte.state = LoByte(intTemp)
errcde = DRV_DioWritePortByte(DriverHandle, lpDioWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
  End If
  
lpDioWritePortByte.port = 1          'Devo abilitare i latch del DAC; scrivo nei bit bassi
lpDioWritePortByte.state = 16 + AddressDAC
errcde = DRV_DioWritePortByte(DriverHandle, lpDioWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
  End If
  
lpDioWritePortByte.state = 40       'Vado a disabilitare il DAC
errcde = DRV_DioWritePortByte(DriverHandle, lpDioWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
  End If

WriteAnalogOutSon = intTemp

End Function

Function DeviceInstalledOnPc() As Integer
'Restituisce il numero di devices installate sul PC; nella struttura DeviceInstalled
'memorizza il nome e il numero delle schede
Dim Address As Long
Dim intCont1 As Integer
Dim intCont2 As Integer
Dim nOutEntries As Integer

Address = DRV_GetAddress(devicelist(0))          'Ricavo l'indirizzo della prima struttura relativa ad una device installata
  errcde = DRV_DeviceGetList(Address, MaxEntries, nOutEntries)        'Ricavo il puntatore alla prima struttura installata
  If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
  End If

errcde = DRV_DeviceGetNumOfList(NumOfDevices)             'Serve per conoscere il numero di devices installate
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
End If
ReDim DeviceInstalled(0 To NumOfDevices)                 'Ridimensiono la variabile relativa alle devices installate
ReDim IOPortAddress(0 To NumOfDevices)                   'Ridimensiono la variabile che contiene gli indirizzi base delle devices

For intCont1 = 0 To (NumOfDevices - 1)                    'Ricavo il nome della struttura in questione
    strNameDevice = ""
    For intCont2 = 0 To MaxDevNameLen
        strNameDevice = strNameDevice + Chr(devicelist(intCont1).szDeviceName(intCont2))
    Next intCont2
    DeviceInstalled(intCont1).strName = strNameDevice
    DeviceInstalled(intCont1).intnum = devicelist(intCont1).dwDeviceNum
    If (InStr(strNameDevice, "PCL-722") <> 0) Then
       IOPortAddress(intCont1) = &H2C0
    ElseIf (InStr(strNameDevice, "PCL-818L") <> 0) Then
       IOPortAddress(intCont1) = &H300
    End If
Next intCont1

DeviceInstalledOnPc = NumOfDevices

End Function


Sub WriteOnIOPort(sngValueOutPut As Single, AddressDAC As Integer, intDeviceNum As Integer)
'Prende in ingresso il valore a cui portare l'uscita analogica della scheda espansione
'e l'indirizzo del DAC da usare; questa funzione scrive sulla porta di uscita I/O
Dim intTemp As Integer
Dim sngTemp As Single

If ioportmatrix(intDeviceNum) = -1 Then Exit Sub
If hiomatrix(intDeviceNum) = -1 Then Exit Sub
DriverHandle = hiomatrix(intDeviceNum)
If sngValueOutPut > 10 Then sngValueOutPut = 10          'Setto il range massimo in cui accettare i valori
If sngValueOutPut < -10 Then sngValueOutPut = -10        'di uscita
If (sngValueOutPut >= 0) Then
    sngTemp = -sngValueOutPut
    intTemp = Round((sngTemp + 10) * 2047 / 10)
Else
   sngTemp = -sngValueOutPut
   intTemp = Round((sngTemp - 10.1) * 2047 / 10)
End If

intTemp = intTemp * 16

lpWritePortByte.port = ioportmatrix(intDeviceNum) + 3         'Prima scrivo il byte alti
lpWritePortByte.ByteData = HiByte(intTemp)
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
End If

lpWritePortByte.port = ioportmatrix(intDeviceNum) + 11        'Devo abilitare i latch del DAC; scrivo nei bit alti
lpWritePortByte.ByteData = 40 + AddressDAC
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.ByteData = 56 + AddressDAC    'Disabilito i latch e congelo i valori memorizzati
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.port = ioportmatrix(intDeviceNum) + 3        'Vado a scrivere il byte basso
lpWritePortByte.ByteData = LoByte(intTemp)
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.port = ioportmatrix(intDeviceNum) + 11        'Devo abilitare i latch del DAC; scrivo nei bit bassi
lpWritePortByte.ByteData = 16 + AddressDAC
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.ByteData = 40       'Vado a disabilitare il DAC
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
End If

End Sub


Sub WriteAnalogOutMother(sngValueOutPut As Single, sngValueMax As Single, intDeviceNum As Integer)
'Prende in ingresso un valore single e lo porta sull'uscita analogica della PCL818L

DriverHandle = hiomatrix(intDeviceNum)
lpAOVoltageOut.chan = 0
lpAOVoltageOut.OutputValue = sngValueOutPut
errcde = DRV_AOVoltageOut(DriverHandle, lpAOVoltageOut)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
End If

End Sub

Sub SetConfigAO(MaxValueOut As Single, MinValueOut As Single, intDeviceNum As Long)
'Prende in ingresso i valori max e min accettabili dalla scheda e li va a settare
DriverHandle = hiomatrix(lngDeviceNum)
lpAOConfig.chan = 0
If (MaxValueOut > 10) Then MaxValueOut = 10
If (MinValueOut < 0) Then MinValueOut = 0
lpAOConfig.MaxValue = MaxValueOut
lpAOConfig.MinValue = MinValueOut
lpAOConfig.RefSrc = 0
errcde = DRV_AOConfig(DriverHandle, lpAOConfig)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
End If

End Sub

Sub SetPressioneSon(sngPressione As Single, AddressDAC As Integer, intDeviceNum As Integer)
'inserire il controllo sulla pressione massima accettabile
Dim intTemp As Integer
Dim sngTemp As Single
Dim sngTemp2 As Single
Dim sngPressInVolt As Single
Dim sngByteHi As Single
Dim sngByteLo As Single

sngPressioneSettata = sngPressione
DriverHandle = hiomatrix(intDeviceNum)
'converto la pressione in KPa in una tensione fra -10 e 10
sngPressInVolt = ((20 * sngPressione) / 500) - 10

'invio la tensione fra -10 e 10 alla espansione della PLC-818L
sngTemp = -sngPressInVolt
sngTemp2 = ((sngTemp + 10) * 4095 / 20) * 16 'converto e shifto a sx di 4 bits
'eseguo la maschera per ottenere la parte alta e quella bassa
sngByteHi = sngTemp2 And 65280
sngByteHi = sngByteHi / 256
sngByteLo = sngTemp2 And 240

If ioportmatrix(intDeviceNum) = -1 Then Exit Sub
If hiomatrix(intDeviceNum) = -1 Then Exit Sub
DriverHandle = hiomatrix(intDeviceNum)

lpWritePortByte.port = ioportmatrix(intDeviceNum) + 3         'Prima scrivo il byte alti
lpWritePortByte.ByteData = CInt(sngByteHi)
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
End If

lpWritePortByte.port = ioportmatrix(intDeviceNum) + 11        'Devo abilitare i latch del DAC; scrivo nei bit alti
lpWritePortByte.ByteData = 40 + AddressDAC
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.ByteData = 56 + AddressDAC    'Disabilito i latch e congelo i valori memorizzati
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.port = ioportmatrix(intDeviceNum) + 3        'Vado a scrivere il byte basso
lpWritePortByte.ByteData = CInt(sngByteLo)
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.port = ioportmatrix(intDeviceNum) + 11        'Devo abilitare i latch del DAC; scrivo nei bit bassi
lpWritePortByte.ByteData = 16 + AddressDAC
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
  End If
  
lpWritePortByte.ByteData = 40       'Vado a disabilitare il DAC
errcde = DRV_WritePortByte(DriverHandle, lpWritePortByte)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Sub
End If

End Sub


Function ReadPressione(intChannel As Integer, intDeviceNum As Integer) As Single
'Questa funzione legge in ingresso analogico
'Dim sngVoltage As Single
'Dim AiConfig As PT_AIConfig
'Dim AiVoltageIn As PT_AIVoltageIn


'DriverHandle = hiomatrix(intDeviceNum)
'AiConfig.DasChan = intChannel ' channel: 0
'AiConfig.DasGain = 0 ' gain code: 0
'errcde = DRV_AIConfig(DriverHandle, AiConfig)
'If (errcde <> 0) Then
   'DRV_GetErrorMessage errcde, szErrMsg
   'Response = MsgBox(szErrMsg, vbOKOnly, “Error!!”)
   'Exit Function
'End If
'
'AiVoltageIn.chan = AiConfig.DasChan
'AiVoltageIn.gain = AiConfig.DasGain
'AiVoltageIn.TrigMode = 0
'AiVoltageIn.voltage = DRV_GetAddress(sngVoltage)
'errcde = DRV_AIVoltageIn(DriverHandle, AiVoltageIn)
'If (errcde <> 0) Then
   'DRV_GetErrorMessage errcde, szErrMsg
   'Response = MsgBox(szErrMsg, vbOKOnly, “Error!!”)
   'Exit Function
'End If
'Select Case intChannel
    'Case 0
        'If Frmports.Visible = True Then
            'If Frmports.Lblch0.Caption <> Str(sngVoltage) Then
                'Frmports.Lblch0.Caption = Str(sngVoltage)
            'End If
        'End If
        'If FrmEuro.Lblc0.Caption <> vt$ Then FrmEuro.Lblc0.Caption = vt$
    'Case 1
        'If Frmports.Visible = True Then
         '   If Frmports.Lblch1.Caption <> Str(sngVoltage) Then
          '      Frmports.Lblch1.Caption = Str(sngVoltage)
           ' End If
        'End If
        'If FrmEuro.Lblc1.Caption <> vt$ Then FrmEuro.Lblc1.Caption = vt$
'    Case 2
 '       If Frmports.Visible = True Then
  '          If Frmports.Lblch2.Caption <> Str(sngVoltage) Then
   '             Frmports.Lblch2.Caption = Str(sngVoltage)
    '        End If
     '   End If
        'If FrmEuro.Lblc1.Caption <> vt$ Then FrmEuro.Lblc1.Caption = vt$
'    End Select

End Function

Function ConvVoltPressione(sngVoltage As Single) As Single
'questa funzione prende in ingresso un numero che rappresenta dei volt e restituisce in
'uscita il corrispondente valore in kPa
Dim sngTemp As Single
Dim sngTemp2 As Single

sngTemp = (500 * sngVoltage) / 10
sngTemp2 = ((105 * sngVoltage) / 4750) * 1000
sngTemp2 = (500 * (sngVoltage + 1)) / 20
'sngPressInVolt = ((20 * sngPressione) / 500) - 10
'ConvVoltPressione = sngTemp2
ConvVoltPressione = sngTemp

End Function


Function ConvPressioneToVolt(sngPressKPa As Single) As Single
'questa funzione converte una pressione in KPa in una tensione
Dim sngVolt As Single

sngVolt = (10 * sngPressKPa) / 500
ConvPressioneToVolt = sngVolt

End Function

Function LetturaTensioneDiretta(lngDevice As Long, intCanale As Integer) As Double
'legge direttamente un valoer da un canale e restituisce la tensione corrispondente
Dim dblValoreInIngressoTemp As Double



If intDevice < 0 Then: strRisposta = MsgBox("Errore: Device non selezionata correttamente", vbExclamation): Exit Function
If intCanale < 0 Then strRisposta = MsgBox("Errore: Canale non selezionato correttamente", vbExclamation)

lpTensioneInIngresso.chan = intCanale
lpTensioneInIngresso.voltage = DRV_GetAddress(dblTensioneLetta)
errcde = DRV_AIVoltageIn(lngDevice, lpTensioneInIngresso)
If (errcde <> 0) Then
    DRV_GetErrorMessage errcde, szErrMsg
    strRisposta = MsgBox(szErrMsg, vbOKOnly, "Error!!")
    Exit Function
End If
LetturaTensioneDiretta = dblTensioneLetta


End Function
