'
' libreria  : VBlib
' file      : ComCNOsaiOpen_Series.vb 
'
' funzioni  : connessione con CN OSAI open  come classe - override metodi per nuovo CN
'
' copyright 2008-2016 C.Viviani
'
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class Open_Series
    Inherits ComCNOsai

    Dim OsaiServer As OpenControl.OPENcontrolPortTypeClient

    Private Const NO_ERROR As UShort = 1
    Private PhaseCN As Integer
    Private ProcessCN As Integer


#Region "COSTRUTTORE"

    Public Sub New(ByVal szAddress As String, ByVal bIsDebug As Boolean, Optional ByVal sVersion As Short = 2)

      MyBase.New(szAddress, bIsDebug, sVersion)

   End Sub


#End Region

#Region "METODI"


#Region "METODI - Open/close connection"

    ' ------------------------------
    ' Apertura sessione con CN Osai
    ' ------------------------------
    '    Public Overrides Function OpenSession(ByVal szNomeCn As String) As ULong
    Public Overrides Function OpenSession() As Boolean

        Connected = False


        OsaiServer = New OpenControl.OPENcontrolPortTypeClient("OPENcontrol", "http://" & NomeCn & ":8080")

        If OsaiServer.State = ServiceModel.CommunicationState.Created Then
            Try
                O_BootPhaseEnquiry(PhaseCN, ErrClass, ErrNum)

            Catch ex As Exception
                MsgBox("Controllo non connesso !!!" & vbCrLf & ex.Message)  '!!!!!!!!!!!!!!!!!!!!!!!!!!
                Me.ErrMsg = "Open NC not connected  !!!" & vbCrLf & ex.Message
            End Try
            ' Lettura processo CN
            If PhaseCN = SYSTEM_UP_PHASE Then
                Connected = True
            Else
                MsgBox("Boot phase " & ErrNum)
                Me.ErrMsg = "Open NC Boot phase error : " & ErrNum
            End If

        End If

        Return Connected


    End Function

    Private Function O_BootPhaseEnquiry(ByRef Phase As Integer, ByRef nErrClass As UShort, ByRef nErrNum As UShort) As Integer
        Dim oBootPhaseInquiry As New OpenControl.BootPhaseEnquiry
        Dim retBootPhaseInquiry As OpenControl.BootPhaseEnquiryR

        retBootPhaseInquiry = OsaiServer.BootPhaseEnquiry(oBootPhaseInquiry)

        nErrClass = retBootPhaseInquiry.ErrClass
        nErrNum = retBootPhaseInquiry.ErrNum
        Phase = retBootPhaseInquiry.Phase

        O_BootPhaseEnquiry = retBootPhaseInquiry.retval

    End Function

    Public Overrides Function CloseSession() As Boolean

        Try

            OsaiServer.Close()

        Catch ex As Exception

        End Try

    End Function


#End Region

#Region "METODI - Lettura/scrittura variabili CN"

    '------------------------------
    ' Lettura / scittura variabili SC su Cn
    '------------------------------
    Public Overrides Function ReadSCVar(ByVal nIndex As Integer, ByVal nNumChar As Integer, Optional ByVal nProcess As Integer = 1) As String

      Dim InputStruct As New OpenControl.ReadVarText
      Dim RetVal As OpenControl.ReadVarTextR

      If (Not (Debug)) Then       ' se macchina collegata e non in debug

         InputStruct.Code = CndexLinkDotNet.Cndex.SC_CODE ' 50 
         InputStruct.Index = nIndex
         InputStruct.Size = nNumChar
         InputStruct.Process = nProcess

         RetVal = OsaiServer.ReadVarText(InputStruct)
         If (RetVal.retval > 0) Then
            Return RetVal.Text
         Else
            ErrClass = RetVal.ErrClass
            ErrNum = RetVal.ErrNum
            ErrMsg = "Error on reading string variable" & vbCrLf & what_happened()
            Return "?????????????????????"
         End If
         ' se sono in debug
      Else
         Return "OK"
      End If                                  ' Endif gestione macchina collegata

   End Function

   Public Overrides Function WriteSCVar(ByVal nIndex As Integer, ByVal nNumChar As Integer, ByVal szText As String, Optional ByVal nProcess As Integer = 1) As Boolean

      Dim InputStruct As New OpenControl.WriteVarText
      Dim RetVal As OpenControl.WriteVarTextR


      If (Not (Debug)) Then       ' se macchina collegata e non in debug

         InputStruct.Code = CndexLinkDotNet.Cndex.SC_CODE ' 50 
         InputStruct.Index = nIndex
         InputStruct.Len = nNumChar
         InputStruct.Process = nProcess
         InputStruct.Text = szText

         RetVal = OsaiServer.WriteVarText(InputStruct)

         If (RetVal.retval > 0) Then
            Return True
         Else
            ErrClass = RetVal.ErrClass
            ErrNum = RetVal.ErrNum
            ErrMsg = "Error on writing string variable" & vbCrLf & what_happened()
            Return False
         End If
         ' se sono in debug
      Else
         Return True
      End If                                  ' Endif gestione macchina collegata

   End Function

   '------------------------------
   ' Lettura variabile Bool su Cn
   '------------------------------
   Public Overrides Function ReadBoolVar(ByVal szVar As stVAR, Optional ByVal nProcess As Integer = 1) As Short
      Dim InputStruct As New OpenControl.ReadVarWord
      Dim RetVal As OpenControl.ReadVarWordR
      Dim nValues(0) As Short               ' Array (lo vuole la libreria Osai) che conterrà il valore letto

      InputStruct.NumVar = 1
      InputStruct.Process = nProcess
      InputStruct.Code = CndexLinkDotNet.Cndex.GW_CODE  ' 21
      InputStruct.Index = szVar.nAddress

      RetVal = OsaiServer.ReadVarWord(InputStruct)
      If (RetVal.retval > 0) Then
         nValues(0) = RetVal.Value(0)
         If ((nValues(0) And (2 ^ szVar.nBit)) <> 0) Then     ' Maschera per controllare solo il bit che mi interessa
            nValues(0) = 1
         Else
            nValues(0) = 0
         End If

         ' return con il valore letto
         Return nValues(0)

      Else
         'MsgBox("Error on reading bool variable" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return CShort(ERRORVALUE)    ' In uscita setto variabile a ERRORVALUE errore 

      End If

   End Function

   '------------------------------
   ' Lettura variabile Short su Cn
   '------------------------------
   Public Overrides Function ReadShortVar(ByVal szVar As stVAR, Optional ByVal nProcess As Integer = 1) As Short
      Dim nValue(0) As Short

      Dim nNumVar As UShort = 1
      Dim nIndex As UShort = 1
      Dim sValue() As UShort = {}
      Dim InputStruct As New OpenControl.ReadVarWord
      Dim RetVal As OpenControl.ReadVarWordR

      InputStruct.Code = CndexLinkDotNet.Cndex.GW_CODE ' 21
      InputStruct.Index = szVar.nAddress
      InputStruct.NumVar = 1
      InputStruct.Process = nProcess

        Try

            RetVal = OsaiServer.ReadVarWord(InputStruct)
            If (RetVal.retval > 0) Then
                Return UShortToShort(RetVal.Value(0))
            Else
                'MsgBox("Error on reading Short variable" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
                Me.ErrMsg = "Error on reading Short variable" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum)
                Return CShort(ERRORVALUE)    ' In uscita setto variabile a ERRORVALUE errore 
            End If
        Catch ex As Exception

            Me.ErrMsg = ex.Message
            Return CShort(CRASHVALUE)    ' In uscita setto variabile a CRASHVALUE errore 

        End Try

    End Function

   '------------------------------
   ' Lettura variabile Double su Cn
   '------------------------------
   Public Overrides Function ReadDoubleVar(ByVal szVar As stVAR, Optional ByVal nProcess As Integer = 1) As Double
      'Dim nValue(0) As Short

      Dim nNumVar As UShort = 1
      Dim nIndex As UShort = 1
      Dim dResult As Double = 0.0
      Dim InputStruct As New OpenControl.ReadVarDouble
      Dim RetVal As OpenControl.ReadVarDoubleR

      InputStruct.Code = CndexLinkDotNet.Cndex.GD_CODE ' 41
      InputStruct.Index = szVar.nAddress
      InputStruct.NumVar = 1
      InputStruct.Process = nProcess

        RetVal = OsaiServer.ReadVarDouble(InputStruct)
        If (RetVal.retval > 0) Then
            dResult = RetVal.Value(0)
            Return dResult
        Else
            'MsgBox("Error on reading Short variable" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
            Return CDbl(ERRORVALUE)    ' In uscita setto variabile a ERRORVALUE errore 
        End If

    End Function

   '------------------------------
   ' Lettura variabile E su Cn
   '------------------------------
   Public Overrides Function ReadVarE(ByVal szVar As stVAR, Optional ByVal nProcess As Integer = 1) As Double

      Dim nValue(0) As Short
      Dim nNumVar As UShort = 1
      Dim InputStruct As New OpenControl.ReadVarDouble
      Dim RetVal As OpenControl.ReadVarDoubleR

      InputStruct.Code = CndexLinkDotNet.Cndex.E_CODE ' 46

      InputStruct.Index = szVar.nAddress
      InputStruct.NumVar = 1
      InputStruct.Process = nProcess

      RetVal = OsaiServer.ReadVarDouble(InputStruct)
      If (RetVal.retval > 0) Then
         Return RetVal.Value(0)
      Else
         'MsgBox("Error on reading E variable" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return CDbl(ERRORVALUE)    ' In uscita setto variabile a ERRORVALUE errore 
      End If

   End Function

   '------------------------------
   ' Lettura variabile MW su Cn
   '------------------------------
   Public Overrides Function ReadVarMW(ByVal szVar As stVAR, Optional ByVal nProcess As Integer = 1) As UShort

      Dim nNumVar As UShort = 1
      Dim InputStruct As New OpenControl.ReadVarWord
      Dim RetVal As OpenControl.ReadVarWordR

      InputStruct.Code = CndexLinkDotNet.Cndex.MW_CODE ' 20
      InputStruct.Index = szVar.nAddress
      InputStruct.NumVar = 1
      InputStruct.Process = nProcess

      RetVal = OsaiServer.ReadVarWord(InputStruct)
      If (RetVal.retval > 0) Then

         Return (RetVal.Value(0))
      Else
         'MsgBox("Error on reading MW variable" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return CUShort(-ERRORVALUE)    ' In uscita setto variabile a ERRORVALUE errore 
      End If

   End Function

    ' -------------------------------
    ' Scrittura variabile Short su Cn
    '---------------------------------
    Public Overrides Function WriteShortVar(ByVal szVar As stVAR, ByVal nValue As Short, Optional ByVal nProcess As Integer = 1) As Boolean

      Dim nValues(0) As Short                 ' Array (lo vuole la libreria Osai) che conterrà il valore letto
      Dim nNumVarToBeWrited As Integer = 1  ' Numero di variabili da leggere (fisso a 1)
      Dim nResult As Integer = -1
      Dim InputStruct As New OpenControl.WriteVarWord
      Dim RetVal As OpenControl.WriteVarWordR

      nValues(0) = nValue

      ' se macchina collegata e non in debug
      If (Not (Debug)) Then

         InputStruct.Code = CndexLinkDotNet.Cndex.GW_CODE ' 21
         InputStruct.NumVar = 1
         InputStruct.Process = 1
         InputStruct.Index = szVar.nAddress
         ReDim InputStruct.Value(0)
         InputStruct.Value(0) = ShortToUShort(nValues(0))

         RetVal = OsaiServer.WriteVarWord(InputStruct)

         If (RetVal.retval <= 0) Then
            ErrClass = RetVal.ErrClass
            ErrNum = RetVal.ErrNum
            ErrMsg = "Error on writing short variable" & vbCrLf & what_happened()
            Return False      ' In uscita setto variabile a ERRORVALUE errore 
         Else

            Return True

         End If

         ' se sono in debug
      Else

         Return True

      End If                                  ' Endif gestione macchina collegata

   End Function

   ' -------------------------------
   ' Scrittura variabile bool su Cn
   '---------------------------------
   Public Overrides Function WriteBoolVar(ByVal szVar As stVAR, ByVal nValue As Short, Optional ByVal nProcess As Integer = 1) As Boolean

      Dim nValues(0) As Short                 ' Array (lo vuole la libreria Osai) che conterrà il valore letto
      Dim nNumVarToBeWrited As Integer = 1  ' Numero di variabili da leggere (fisso a 1)
      Dim nResult As Integer = -1
      Dim InputStruct As New OpenControl.WriteVarWordBit
      Dim RetVal As OpenControl.WriteVarWordBitR

      nValues(0) = nValue

      ' se macchina collegata e non in debug
      If (Not (Debug)) Then

         InputStruct.Code = CndexLinkDotNet.Cndex.GW_CODE ' 21
         InputStruct.Process = 1
         InputStruct.Index = szVar.nAddress
         InputStruct.BitIndex = szVar.nBit
         InputStruct.BitValue = nValue
         RetVal = OsaiServer.WriteVarWordBit(InputStruct)

         If (RetVal.retval <= 0) Then
            ErrClass = RetVal.ErrClass
            ErrNum = RetVal.ErrNum
            ErrMsg = "Error on writing bool variable" & vbCrLf & what_happened()
            Return False      ' 
         Else
            Return True
         End If

         ' se sono in debug
      Else
         Return True
      End If                                  ' Endif gestione macchina collegata

   End Function

   ' -------------------------------
   ' Scrittura variabile E su Cn
   '---------------------------------
   Public Overrides Function WriteVarE(ByVal szVar As stVAR, ByVal nValue As Double, Optional ByVal nProcess As Integer = 1) As Boolean

      Dim nValues(0) As Double                 ' Array (lo vuole la libreria Osai) che conterrà il valore letto
      Dim nNumVarToBeWrited As Integer = 1  ' Numero di variabili da leggere (fisso a 1)
      Dim nResult As Integer = -1
      Dim InputStruct As New OpenControl.WriteVarDouble
      Dim RetVal As OpenControl.WriteVarDoubleR

      nValues(0) = nValue

      If (Not (Debug)) Then       ' se macchina collegata e non in debug

         InputStruct.Code = CndexLinkDotNet.Cndex.E_CODE ' 46
         InputStruct.NumVar = 1
         InputStruct.Process = nProcess
         InputStruct.Index = szVar.nAddress
         ReDim InputStruct.Value(0)
         InputStruct.Value(0) = nValues(0)

         RetVal = OsaiServer.WriteVarDouble(InputStruct)

         If (RetVal.retval <= 0) Then
            ErrClass = RetVal.ErrClass
            ErrNum = RetVal.ErrNum
            ErrMsg = "Error on writing Write E DoubleVar variable" & vbCrLf & what_happened()
            Return False      ' In uscita setto variabile a ERRORVALUE errore 
         Else
            Return True
         End If

         ' se sono in debug
      Else
         Return True
      End If                                  ' Endif gestione macchina collegata

   End Function

#End Region

#Region "METODI - Modo CN / PP"

   ' ---------------------------------------------------------------
   ' Attivazione del modo Auto
   '----------------------------------------------------------------
   Public Overrides Function SetAutoMode(Optional ByVal nProcess As Integer = 1) As Boolean
      Dim RetVal As New OpenControl.SetProcessModeR
      Dim Mode As New OpenControl.SetProcessMode

      Mode.Mode = 2
      Mode.ProcNum = nProcess

      RetVal = OsaiServer.SetProcessMode(Mode)
      If RetVal.retval > 0 Then
         Return RetVal.retval

      Else
         'MsgBox("Error on activating Auto mode " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return RetVal.retval
      End If
   End Function

   ' ---------------------------------------------------------------
   ' Lettura modalità selezionata su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetModeSelected(Optional ByVal nProcess As Integer = 1) As Byte
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.modeselect

      Else
         'MsgBox("Error on reading Mode Selected" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

   ' ---------------------------------------------------------------
   ' Attivazione del pp passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function ActivatePP(ByVal szPartPrg As String, Optional ByVal nProcess As UShort = 1) As Short
      Dim RetVal As New OpenControl.SelectPartProgramFromDriveR
      Dim InputStruct As New OpenControl.SelectPartProgramFromDrive

      InputStruct.PPName = szPartPrg
      InputStruct.ProcNum = nProcess

      RetVal = OsaiServer.SelectPartProgramFromDrive(InputStruct)
      If RetVal.retval > 0 Then
         Return RetVal.retval

      Else
         'MsgBox("Error on activating part-program " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return RetVal.retval
      End If
   End Function

   ' ---------------------------------
   ' Lettura Programma corrente
   '----------------------------------
   Public Function GetActivePP(ByVal nProcess As Integer) As String
      Dim InputStruct As New OpenControl.GetActivePartProgram
      Dim RetVal As OpenControl.GetActivePartProgramR

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetActivePartProgram(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.Main

      Else
         'MsgBox("Error on reading Active Program" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return CShort(ERRORVALUE)    ' In uscita setto variabile a ERRORVALUE errore 
      End If

   End Function

   ' ---------------------------------------------------------------
   ' Lettura informazioni CN 
   '----------------------------------------------------------------
   Public Function NcInfo1Open(ByRef ncInfo As OpenControl.GetNcInfo1R, Optional ByVal nProcess As Integer = 1) As OpenControl.GetNcInfo1R
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal

      Else
         'MsgBox("Error on reading NcInfo " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return RetVal
      End If
   End Function

   ' ---------------------------------------------------------------
   ' Ritorna True se lo stato del CN è IDLE
   '----------------------------------------------------------------
   Public Overrides Function IsResetStatus(Optional ByVal nProcess As Integer = 1) As Boolean
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then

         If RetVal.GetInfo.modeselect = 0 Then
            Return True
         Else
            Return False
         End If

      Else
         'MsgBox("Error in IsResetStatus" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

   ' -------------------------------------------------
   ' Lettura stato macchina su processo passato per parametro
   '--------------------------------------------------
   Public Overrides Function GetMachineStatus(Optional ByVal nProcess As Integer = 1) As Byte
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)
      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.status

      Else
         'MsgBox("Error on reading Machine Status" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function


#End Region

#Region "METODI - Utensili / correttori"

   ' ---------------------------------------------------------------
   ' Lettura utensile corrente su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetActualTool(Optional ByVal nProcess As Integer = 1) As Double
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.progrtool

      Else
         'MsgBox("Error on reading Mode Selected" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

   ' ---------------------------------------------------------------
   ' Lettura correttore corrente su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetActualCorr(Optional ByVal nProcess As Integer = 1) As UShort
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.actualtooloff

      Else
         'MsgBox("Error on reading Mode Selected" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

   ' ---------------------------------------------------------------
   ' Lettura lunghezza correttore corrente su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetActualToolLen(ByVal nRecord As UInteger) As Double
      Dim InputStruct As New OpenControl.GetOffsetTabRecordII
      Dim RetVal As OpenControl.GetOffsetTabRecordIIR
      Dim Len() As OpenControl.OFFSETVALII

      InputStruct.RecordNum = nRecord
      RetVal = OsaiServer.GetOffsetTabRecordII(InputStruct)

      If RetVal.retval > 0 Then
         Len = RetVal.RecordBuff.LenVal
         Return Len(0).ValOrig

      Else
         'MsgBox("Error on reading actual tool length" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

   ' ---------------------------------------------------------------
   ' Lettura Diametro correttore corrente su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetActualToolDiam(ByVal nRecord As UInteger) As Double
      Dim InputStruct As New OpenControl.GetOffsetTabRecordII
      Dim RetVal As OpenControl.GetOffsetTabRecordIIR
      Dim Len() As OpenControl.OFFSETVALII

      InputStruct.RecordNum = nRecord
      RetVal = OsaiServer.GetOffsetTabRecordII(InputStruct)

      If RetVal.retval > 0 Then
         Len = RetVal.RecordBuff.DiaVal
         Return Len(0).ValOrig

      Else
         'MsgBox("Error on reading actual tool diameter" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

#End Region

#Region "METODI - Feed / Speed"

   ' ---------------------------------------------------------------
   ' Lettura Feed Override su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetFeedOverride(Optional ByVal nProcess As Integer = 1) As UShort
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.feedov / 100

      Else
         ' MsgBox("Error on reading Feed Override" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

   ' ---------------------------------------------------------------
   ' Lettura Rapid Override su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetRapidOverride(Optional ByVal nProcess As Integer = 1) As UShort
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.rapfeedov / 100

      Else
         'MsgBox("Error on reading Rapid Override" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If

   End Function

   ' ---------------------------------------------------------------
   ' Lettura Speed Override su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetSpeedOverride(Optional ByVal nProcess As Integer = 1) As UShort
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.speedov / 100

      Else
         'MsgBox("Error on reading Speed Override" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If
   End Function

   ' ---------------------------------------------------------------
   ' Lettura velocità rapido programmata su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetProgrammedRapid(Optional ByVal nProcess As Integer = 1) As Double
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.rapidfeed

      Else
         'MsgBox("Error on reading Programmed Rapid " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If
   End Function

   ' ---------------------------------------------------------------
   ' Lettura Feed programmata su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetProgrammedFeed(Optional ByVal nProcess As Integer = 1) As Double
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.progrfeed

      Else
         'MsgBox("Error on reading Programmed Feed " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If
   End Function

   ' ---------------------------------------------------------------
   ' Lettura Speed programmata su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetProgrammedSpeed(Optional ByVal nProcess As Integer = 1) As Double
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.progrspeed

      Else
         'MsgBox("Error on reading Programmed Feed " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return ErrMsg
      End If
   End Function

   ' ---------------------------------------------------------------
   ' Lettura Feed reale su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetActualFeed(Optional ByVal nProcess As Integer = 1) As Double
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.realfeed

      Else
         'MsgBox("Error on reading Actual Feed " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If
   End Function

   ' ---------------------------------------------------------------
   ' Lettura Speed reale su processo passato per parametro
   '----------------------------------------------------------------
   Public Overrides Function GetActualSpeed(Optional ByVal nProcess As Integer = 1) As Double
      Dim InputStruct As New OpenControl.GetNcInfo1
      Dim RetVal As OpenControl.GetNcInfo1R

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetNcInfo1(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.GetInfo.realspeed

      Else
         'MsgBox("Error on reading Actual Speed " & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return 0
      End If
   End Function

#End Region

#Region "METODI - copia files"

    Public Overrides Function CopyFileToNC(ByVal sz_logical_drive As String,
                                 ByVal sz_source_filename As String,
                                 ByVal sz_dest_filename As String) As Short

        Return PutFile(sz_source_filename, "PROGRAMS" & "\\" & sz_dest_filename, ErrClass, ErrNum, 0)

    End Function


    'utilizzabile per la copia di file verso cnc

    Public Overrides Function PutFile(ByRef szPathFile As String, ByVal szTargetPath As String, ByRef nErrClass As UInteger, ByRef nErrNum As UInteger, ByRef FileSize As Integer) As Integer
        Dim oPutFile As New OpenControl.PutFile
        Dim retPutFile As New OpenControl.PutFileR
        Dim szData As String

        oPutFile.PathName = szTargetPath

        szData = ""

        'apro il file in szPathFile
        szData = My.Computer.FileSystem.ReadAllText(szPathFile, System.Text.Encoding.UTF8)

        oPutFile.Data = szData
        oPutFile.Size = FileSize

        'calcolo la dimensione in byte e la metto in oPutFile.Size
        oPutFile.Size = szData.Length

        retPutFile = OsaiServer.PutFile(oPutFile)

        nErrClass = retPutFile.ErrClass
        nErrNum = retPutFile.ErrNum

        PutFile = retPutFile.retval


    End Function

#End Region

    ' ----------------------------------------------------------------------------------------------------------------------------
    ' Lettura Errori CN
    ' ----------------------------------------------------------------------------------------------------------------------------
    'Public Function ReadCurrentErrorMsg(ByVal nProcess As Integer, ByRef MessaggioErr() As String, ByRef nErrClass As UInteger, ByRef nErrNum As UInteger) As Integer

    '    Dim oReadCurrentErrorMsg As New OpenControl.ReadCurrentErrorMsg
    '    Dim retReadCurrentErrorMsg As New OpenControl.ReadCurrentErrorMsgR

    '    oReadCurrentErrorMsg.Process = nProcess
    '    retReadCurrentErrorMsg = OsaiServer.ReadCurrentErrorMsg(oReadCurrentErrorMsg)
    '    'SOAP function call
    '    Dim datoErr As New CndexLinkDotNet.Cndex.MSG_ERROR

    '    Try
    '        datoErr.BootID = CUInt(retReadCurrentErrorMsg.Data.BootID)
    '        datoErr.UnIdSeq = CUInt(retReadCurrentErrorMsg.Data.UnIdSeq)
    '        datoErr.Code_Err = CUInt(retReadCurrentErrorMsg.Data.CodeErr)
    '        datoErr.Comando = CByte(retReadCurrentErrorMsg.Data.Comando)
    '        datoErr.FormatTxt = retReadCurrentErrorMsg.Data.FormatTxt
    '        datoErr.Process = retReadCurrentErrorMsg.Data.Process
    '        datoErr.SubCom = CByte(retReadCurrentErrorMsg.Data.SubCom)

    '        Dim text2 As MESSAGE_TEXT
    '        text2.Line1 = ""
    '        text2.Line2 = ""
    '        text2.Line3 = ""
    '        text2.Line4 = ""

    '        'Invoke native function TranslateErrorMsg() in library OSAI_ErrorMgr.dll
    '        TranslateErrorMsg(datoErr, text2)

    '        ReDim MessaggioErr(4)

    '        'Display error message text
    '        MessaggioErr(1) = text2.Line1
    '        MessaggioErr(2) = text2.Line2
    '        MessaggioErr(3) = text2.Line3
    '        MessaggioErr(4) = text2.Line4
    '    Catch ex As Exception

    '        Return 0
    '        'Dim text2 As MESSAGE_TEXT
    '        'text2.Line1 = ""
    '        'text2.Line2 = ""
    '        'text2.Line3 = ""
    '        'text2.Line4 = ""

    '        ''Invoke native function TranslateErrorMsg() in library OSAI_ErrorMgr.dll
    '        'TranslateErrorMsg(datoErr, text2)

    '        'ReDim MessaggioErr(4)

    '        ''Display error message text
    '        'MessaggioErr(1) = text2.Line1
    '        'MessaggioErr(2) = text2.Line2
    '        'MessaggioErr(3) = text2.Line3
    '        'MessaggioErr(4) = text2.Line4
    '    End Try


    '    ReadCurrentErrorMsg = retReadCurrentErrorMsg.retval

    'End Function

    '------------------------------
    ' Lettura messaggio DIS
    '------------------------------

    Public Overrides Function ReadDisMsg(Optional ByVal nProcess As Integer = 1, Optional ByVal nMac As Integer = 1) As String
      Dim InputStruct As New OpenControl.ReadPartProgramMsg
      Dim RetVal As OpenControl.ReadPartProgramMsgR

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.ReadPartProgramMsg(InputStruct)

      If RetVal.retval > 0 Then
         Return RetVal.PartProgramMsg

      Else
         'MsgBox("Error on reading DIS messages" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return ""
      End If

   End Function

   ' -------------------------------------------------
   ' Lettura Linee programma corrente
   '--------------------------------------------------
   Public Overrides Function ReadPPLines(ByVal nProcess As Integer, ByRef EightLines() As String, ByRef nErrClass As UInteger, ByRef nErrNum As UInteger) As Boolean
      Dim InputStruct As New OpenControl.GetPartProgramLines
      Dim RetVal As OpenControl.GetPartProgramLinesR

      InputStruct.ProcNum = nProcess
      RetVal = OsaiServer.GetPartProgramLines(InputStruct)

      If RetVal.retval > 0 Then
         EightLines(0) = RetVal.PPLine1
         EightLines(1) = RetVal.PPLine2
         EightLines(2) = RetVal.PPLine3
         EightLines(3) = RetVal.PPLine4
         EightLines(4) = RetVal.PPLine5
         EightLines(5) = RetVal.PPLine6
         EightLines(6) = RetVal.PPLine7
         EightLines(7) = RetVal.PPLine8

         Return True

      Else
         'MsgBox("Error on reading PP Lines" & vbCrLf & "Class: " & Str(ErrClass) & "  Number: 0x" & Hex(ErrNum))
         Return False
      End If
   End Function

#Region "METODI - funz. aux ( short <> ushort), ..."

   ' ----------------------------------------
   ' Trasforma un Unsigned Short in un Short
   ' ----------------------------------------
   Public Function UShortToShort(ByVal usValue As UShort) As Short
      Dim dValue As Double
      Dim numBit As Byte

      'numero bit tipo di dato (short -> 16 bit, integer -> 32 bit, long -> 64bit)
      numBit = 16

      If usValue > (2 ^ (numBit - 1) - 1) Then
         dValue = CDbl(usValue)
         dValue = dValue - CDbl(2 ^ numBit)
         UShortToShort = CShort(dValue)
      Else
         UShortToShort = CShort(usValue)
      End If

      Return UShortToShort
   End Function

   ' ----------------------------------------
   ' Trasforma uno Short in un UnisignedShort
   ' ----------------------------------------
   Public Function ShortToUShort(ByVal sValue As Short) As UShort
      Dim dValue As Double
      Dim numBit As Byte

      'numero bit tipo di dato (short -> 16 bit, integer -> 32 bit, long -> 64bit)
      numBit = 16

      If sValue < 0 Then
         dValue = CDbl(sValue)
         dValue = dValue + CDbl(2 ^ numBit)
         ShortToUShort = CUShort(dValue)
      Else
         ShortToUShort = CUShort(sValue)
      End If

      Return ShortToUShort

   End Function

#End Region

#End Region

End Class
